views:

52

answers:

1

Hi Guys,

I need ideas about how to move an object (a circle which represents a robot in my application).

The surface the object will be moving on consists of Tiles of BufferedImage of 3 rows and 3 columns ( represented in arrays). All the Tiles are equal in sizes (160 X 160).The 3rd row and the 3rd column are the track rails on which the object must be moving on. It means that the object(robot) can move horizontal (forward and backward) and vertical (upwards and downwards). The Tile at the position [2][2] (please am counting from top. so the top row will be 0 the next afterward is 1 etc.. ) is a crossing which the robot will use to change to the vertical track rails or to the horizontal track rails.

My problem now is how to move the object to a specific Tile after the crossing has turned. For instance the robot will be in the Tile at position [2][1] and want to move to the tile at position [1][2] after the crossing is turned and then move further upwards. Or it can be at [1][2] and want to move to [2][1] after the crossing is turned and then move further backwards .

How can i move the robot from one Tile to another Tile? Which way can i refer to a specific Tile in the BufferedImage that i can place the object. All what i want is give me the ideas how i can do it.

Please this is my first time of doing such project so forgive me if my question is too elementary. With your explanation and help i will learn more from it.

Thank you very much.

A: 

In order to display your image you need to figure out the bounds of the grid you want to put your image into. I usually create two helper methods, one to translate grid coordinates into display coordinates and the other one to go the other direction.

private Point convertGridToDisplay(int x, int y) {
  return new Point(x * 160, y * 160);
}

private Point convertDisplayToGrid(int x, int y) {
  return new Point(x / 160, y / 160);
}

convertGridToDisplay() will give you the upper left hand coordinate where you should draw your image to.

For example:

Point point = convertGridToDisplay(2, 1);
graphics.drawImage(img, null, point.x, point.y)

will draw your image at grid (2, 1).

convertDisplayToGrid() will come in handy when you want to figure out which grid a mouse click was made in.

rancidfishbreath