Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Loading images on a square on grid.

Thread view: 
CelticFC - 18 Apr 2006 15:37 GMT
The following is code for a grid within an Applet.

public void drawBox(Graphics g){
    for(int i=10;i<=350;i+=40){
   g.drawLine(i,10,i,330);
   g.drawLine(10,i,330,i);
   }

What I would like to do is to be able to load an image within a square
on the grid, so that the image is locked within the square on the grid
and no other image would be able to share the same square.
At the moment I can load an image in the grid, but without locking it
to any square within the grid.

the following is how the image is loaded.
for(int i = 0; i < point.size(); i++){
           icon = (Point)point.get(i);
           g.drawImage(pawn, icon.x, icon.y, 25, 25, this);
       }
And this is the method used.

public void mousePressed(MouseEvent mouseevent){
       setBackground(new Color(210,255,210)); //Light Green Background
       x = mouseevent.getX(); //returns value of x coordinate
       y = mouseevent.getY(); //returns value of y coordinate
       if(x > 19 && x < 312 && y > 19 && y < 312){ //boundary of
grid(board)
           pawncount = pawncount + 1; //ensures other conditions can
be met within the game
           point.add(mouseevent.getPoint()); //adds the value to the
Arraylist
           }

Hope this makes sense to somebody.
thanks in advance for any help.
Knute Johnson - 18 Apr 2006 17:37 GMT
> The following is code for a grid within an Applet.
>
[quoted text clipped - 31 lines]
> Hope this makes sense to somebody.
> thanks in advance for any help.

I assume that you are trying to make a chess game applet?  Here are a
few thoughts on how I would do that;

1) Draw the chess board directly on the applet in the paint method

2) Put images of your pieces in your jar file and draw those on top of
   the chess board from the paint()

3) Put your control logic in the run method or in event handlers (like
   the mouse event handler you have)

4) Scale your drawing depending on the size of the applet

5) You can use images that have transparency or a transparent background
   to draw over your board to make them look better

6) You can double buffer to make your display look better when you move
   the pieces around.

Signature

Knute Johnson
email s/nospam/knute/

CelticFC - 18 Apr 2006 18:16 GMT
Thanks for the reply.

The game i am attempting to develop is a puzzle.
There are two static pawns placed on the middle of the board. The
object is to place the remaining 14 pawns on the board so that  no 3
pawns shall be in a straight line in any possible direction.

I am pretty new to java (only been working with it for about 3months).
I really only need to lock any single pawn image to any single square
on the grid.

I have an undo button within the program that allows me to remove them
if placed wrongly.

Thanks Again.
celticFC
Oliver Wong - 18 Apr 2006 18:37 GMT
> The following is code for a grid within an Applet.
>
[quoted text clipped - 28 lines]
> Arraylist
>            }

   I'm assuming your question has something to do with snapping to the
grid. That is, you have a grid where each tile is, for example, 25 by 25
pixels, and if the player click at coordinate (14,57), you want to treat
that as a click at (0,50).

   Basically, the trick is to do an integer-divide, and then re-multiply.

   14 divided by 25 gives 0 in integer division. Then you multiply 0 by 25
to get 0 again.
   57 divided by 25 gives 2 in integer division. Then you multiply 2 by 25
to get 50.

   It looks like in your particular case, you have an offset of 19, so you
may need to add 19 to the final results (e.g. (0+19,50+19) = (19,69))

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.