java - how to get element position in 2D array when mouse clicked? -
im building board using gui(may board game). have created 2d array make cells gomoku game. want make function: whenever click cell, cell position displayed.
private cell[][] cells; private in row; private int col; update: ....... jpanel pn = new jpanel(new gridlayout(row, col, 0, 0)); cells = new cell[row][col]; (int = 0; < size; i++) { (int j = 0; j < size; j++) { pn.add(cells[i][j] = new cell()); } ...... private class mouselistener extends mouseadapter { public void mouseclicked(mouseevent e) { (int = 0; < size; i++) { (int j = 0; j < size; j++) { if ( ) { system.out.println("x: " + + ", y: " + j); } } } }
i cant make work although i've tried several times , different conditions in if
you can add mouselistener
directly cells , use e.getsource()
clicked cell.
Comments
Post a Comment