title: Convert JLabel matrix to Graphics Display
body: <p>I recently finished a project involving a JPanel containing a matrix of JLabels, each of which had their own MouseListener added to them in order to become active/inactive as the user wishes. The project worked, but I received complaints that the project was very slow on older computers, and I understand that the way I did it, while easy, was horribly inefficient. Here is a simplified version of the code that I wrote:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import java.awt.event.*;
  public class LifeDriver {
     public static void main(String[] args) {
        JFrame frame = new JFrame("Test");
        frame.setSize(950, 850);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new LifePanel(40, 50));
        frame.setVisible(true);
     }
  }
  class LifePanel extends JPanel {
     private Cell[][] grid;
     private static int nR, nC;
     public LifePanel(int row, int column) {
        nR = row;
        nC = column;
        grid = new Cell[row][column];
        setLayout(new GridLayout(row, column, 1, 1));
        for (int r = 0; r &lt; row; r++)
           for (int c = 0; c &lt; column; c++)
           {
             grid[r][c] = new Cell();
             add(grid[r][c]);
           }
     }
  }
  class Cell extends JLabel
  {
     private boolean alive;
     private static boolean mouseDown;
     public Cell() {
        setOpaque(true);
        addMouseListener(new Mouse());
        kill();
     }
     public void revive() {
        setAlive(true);
     }
     public void kill() {
        setAlive(false);
     }
     public void setAlive(boolean arg) {
        alive = arg;
        if (arg)
           setBackground(Color.RED);
        else
           setBackground(Color.WHITE);
     }
     private class Mouse extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
           mouseDown = true;
           if (alive)
              kill();
           else 
              revive();
        }
  
        public void mouseReleased(MouseEvent e) {
           mouseDown = false;
        }
  
        public void mouseEntered(MouseEvent e) {
           if (mouseDown)
              if (alive)
                 kill();
              else 
                 revive();
        }
     }
  }
  </code></pre>
  
  <p>I want to go back and change this so that I have one MouseListener in <code>LifePanel</code>, and using <code>getX()</code> and <code>getY()</code>, determine which "Cell" has been clicked (no longer extending JLabel) and have that Cell change its color, then paint itself on the panel. Something like:</p>
  
  <pre><code>class LifePanel extends JPanel {
     BufferedImage myImage;
     Graphics g;
     ...
     class Mouse extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
           grid[someRow][someColumn].draw(g) //use e.getX() and e.getY()
        }
        public void mouseEntered(MouseEvent e) {
           //similar to above
        }
     }
  }
  
  class Cell {
     //other methods
     public void draw(Graphics g) {
        g.setColor(myColor);
        g.fillRect(someX, someY, someWidth, someHeight);
     }
  }
  </code></pre>
  
  <p>How would I determine the proper values to use in these two situations, so that mouse clicks would result in the same outcome as the original code?</p>