# Java Game How to find the x and y for a tile?

Hello I have a java game where you build houses I want to be able to dag my mouse form point a to point b and I want to fill that area with tiles when I release the mouse. What I cant do is get the first x and y coordinate of the selected area then put the tile there then move on to the next area I tried to do something like this:

    bx = Math.min((dragX / 20) * 20, (curX / 20) * 20);
by = Math.min((dragY / 20) * 20, (curY / 20) * 20);
w = Math.abs((curX / 20) * 20 - (dragX / 20) * 20);
h = Math.abs((curY / 20) * 20 - (dragY / 20) * 20);

if (dragging & w >= 20 && h >= 20) {
g.setColor(new Color(26, 216, 26, 128));
g.fillRect(bx, by, w, h);
g.setColor(new Color(17, 142, 17, 255));
g.drawRect(bx, by, w, h);

int posx = play.bx * 20;
int posy = play.by * 20;
for (int i = 0; i <(play.w * play.h / 20) / 20 ; i++) {

posx +=20;
posy+=20;
}
}


dragX and dragY is where it starts and curx and cury is where it is currently the tiles are 20x20 pixels so I place them every 20 pixels on the screen so it is like a grid. if I select an area that is 2 tile wide and 2 tiles high and each tile is 20 x 20 pixels that is 1600 pixels I divide that number by 20 and get 80 and then divide that by 20 again to find out how many tiles I need to place in total which in this case is four. what I don't know is how to find the x and y that I need to place that tile then I don't know how to move on to the next one. Please help me. Thx to everyone who reads this.

It's pretty simple. All you need is something like this:

// It's always good to keep variables around instead of inlining
// magic numbers.
int gridSizeX = 20;
int gridSizeY = 20;

// The rectangle starts getting dragged at startX, startY,
// ends dragging at endX, endY
// Like this:
// +-------------------------+
// | startX, startY          |
// |                         |
// |               endX, endY|
// +-------------------------+
// Note that since the player can drag up and to the left,
// its possible that startX > endX and startY > endY.
int startX, startY, endX, endY;

// Convert pixel to grid coordinates.
int startGridX = startX / gridSizeX;
int startGridY = startX / gridSizeY;
int endGridX= endX / gridSizeX;
int endGridY= endY / gridSizeY;

// Find the bounds in grid coordinates of the start
// and end. (minX, minY) will be in the top left, and
// (maxX, maxY) will be in the bottom right.
int minX = min(startGridX, endGridX);
int maxX = max(startGridX, endGridX);
int minY = min(startGridY, endGridY);
int maxY = max(startGridY, endGridY);

// Iterate through all the grid coordinates.
for (int x = minX; x <= maxX; x++)
{
for (int y = minY; y <= maxY; y++)
{
// Place a tile at the given grid coordinate.
// The arguments to this function should be in pixels.
CreateTile(x * gridSizeX, y * gridSizeY);
}
}

• where it says int minx = min(startGridX, endgridX); should it be Math.min instead of just min? – user50369 Mar 25 '15 at 20:26
• this dosnt seem to work for me – user50369 Mar 26 '15 at 17:34
• if I start dragging at the very top corner it works but nothing else does – user50369 Mar 26 '15 at 17:54
• this is just pseudocode, so whatever your framework needs. Also, It's not clear to me what "curX" and "dragX" are. The comment explains what I think these mean. – mklingen Mar 26 '15 at 18:11
• I updated the code to very explicitly say what I think are the start and end. – mklingen Mar 26 '15 at 18:12