I'm trying to draw a game map using many rectangular shapes(just like a screen contains many pixels). I'm using graphics 2D and fillRect(int x, int y, width, height)
method. I make the rectangular so small with 10 widths
and 10 height
.
How can I detect if someone clicks a rectangle? I have read other questions but I am still confused by how the mouse listener detects my small rectangle?
Can someone give me any other solution instead of making pixels with a rectangular shape? maybe like buttons or something?
Here is my example code of making multiple rectangle :
public void paint (Graphics g){
Graphics2D g2D = (Graphics2D) g;
for (int j = 0; j<Map.WIDTH; j++){
if(j==100){
if(map[i][j]=='#'){
x=50;
y+=10;
g2D.setColor(Color.BLACK);
g2D.fillRect(x, y, 10, 10);
x+=10;
y+=0;
}
note: just ignore the iteration because i'm iterate from some input file(a template)