title: Mouse Listener on Rectangle
body: <p>I'm trying to draw a game map using many rectangular shapes(just like a screen contains many pixels). I'm using <strong>graphics 2D</strong> and <code>fillRect(int x, int y, width, height)</code> method. I make the rectangular so small with <code>10 widths</code> and <code>10 height</code>.</p>
  
  <p>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?</p>
  
  <p>Can someone give me any other solution instead of making pixels with a rectangular shape? maybe like buttons or something?</p>
  
  <p>Here is my example code of making multiple rectangle :</p>
  
  <pre><code>public void paint (Graphics g){
  
  Graphics2D g2D = (Graphics2D) g;
  for (int j = 0; j&lt;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;   
  
                      }
  </code></pre>
  
  <p>note: just ignore the iteration because i'm iterate from some input file(a template)</p>