Loading images on a square on grid.

C

CelticFC

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.
 
K

Knute Johnson

CelticFC said:
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.

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.
 
C

CelticFC

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
 
O

Oliver Wong

CelticFC said:
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
}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top