title: Efficient way of locating and/or selecting graphic object in a fixed grid
body: <p>I have a panel that is filled with a large number of circles (Ellipse2D). The circles are stored in a 2 dimensional array (rows and columns). </p>
  
  <p>My goal is to be able to "paint" the circles as I drag the mouse over them. I will eventually want to use selection shapes that will change the color of all the circles contained within the selection shape. </p>
  
  <p>I'm using a mouse dragging listener that continually scans the entire 2d array and checks is the current point is inside on of the circles. Like so:</p>
  
  <pre><code>        addMouseMotionListener(new MouseAdapter() {
  
          public void mouseDragged(MouseEvent e) {
  
              currentColor = ColorSliderPanel.getRGB();
  
              for (int x = 0; x &lt; numColumns; x++) {
                  for (int y = 0; y &lt; numRows; y++) {
                      if (circle[x][y].contains(e.getX(), e.getY())) {
  
                          circle[x][y].setColor(currentColor);
                          repaint();
                      }
                  }
              }
  
          }
      });
  </code></pre>
  
  <p>The above code works, but it's really slow (1000+ circles), as it is checking every single object. </p>
  
  <p>There must be a better way. I've read a little about a Quadtree, but I'm not sure if a quadtree is more horsepower than I need. </p>
  
  <p>Thanks</p>
  
  <p>I made the following changes based on some of the comments below. Circles is now a linear ArrayList. The draw method simply fill the circle. Making this change improved the speed by two orders of magnitude. It works much better now. Although, I can still sweep across the panel with moderate speed and miss a few circles. So I may need to optimize futher.</p>
  
  <pre><code>  Graphics2D g2d = (Graphics2D) getGraphics();
  
              for (Circle2D c : circles) {
                  if (c.contains(p)) {
                      c.setColor(currentColor);
                     //Graphics2D g2d = (Graphics2D) getGraphics(); (moved)
                      c.draw(g2d);
                  }
              }
  </code></pre>