java - Get clicked item of array -
java - Get clicked item of array -
i've created grid of objects called cell. each cell has next properties:
x coordinate y coordinate width height
the width of cell equal it's height. every cell has got same width/height. there 9*9 cells, 1 beside another, created algorithm:
cells = new cell[9][0]; for(int = 0; < 9; i++) { for(int j = 0; j < 9; j++) { cells[i][j] = new cell(i*cellwidth, j*cellheight); } }
the cell's constructor asks x , y coordinates. got grid of cells. when touch screen, know x , y coordinates touched screen. cell touched should run method called istouched.
how can find out cell i've touched?
i've tried algorithm:
public boolean istouched(int zx, int zy) { if((zx >= x && zx <= x+cellsize) && (zy >= y && zy <= y+cellsize)) { homecoming true; }else { homecoming false; } }
zx , zy touched coordinates. checks whether touched x axis bigger or equal cell's own x coordinate , if it's smaller cell's x coordinate + cell's width. same thing y coordinate.
it won't work when tap on cell of first row, first element in first row gets selected. when tapping on element in 5th row, 5th element in 5th row gets selected although i've pressed on cell.
here's screenshot
i can't find mistake, suggestions? in advance
it more efficient , easier compute cell contains touch point.
cell touchedcell(int x, int y) { = x / cellwidth; j = y / cellheight; if (i >= 9 || j >= 9 || < 0 || j < 0) homecoming null; else homecoming cell[i][j]; }
the array size off:
cells = new cell[9][0];
this whats causing unusual effect there 9 cells in array, since j multiplied 0 when indexing cell[i][j] == cell[i][0]
no matter j.
should be
cells = new cell[9][9];
java arrays algorithm
Comments
Post a Comment