title: Java how to paint rectangles on mouseclick
body: <p>As the title says, I'm having a hard time trying to draw some rectangles (filled) in JApplet.
  The exact goal is to have a 50x50 table and when you click on a targeted cell, to make it filled (possibly done by drawing a filled rectangle). I have done the maths about the coordinates of the starting point, but for some reason I can't draw the new rectangle in the MouseClicked method. Any suggestions?</p>
  
  <pre><code>public class Main extends JApplet {
  
  public static final int DIMX = 800;
  public static final int DIMY = 800;
  public static final int ratio = 16;
  Graphics g;
  boolean drawing;
  public int cX;
  public int cY;
  
  public Main() {
      JPanel MainFrame = new JPanel();
      MainFrame.setPreferredSize(new Dimension(400, 800));
      MainFrame.setBackground(Color.LIGHT_GRAY);
      JPanel Table = new JPanel();
      Table.setPreferredSize(new Dimension(800, 800));
      Table.setBackground(Color.LIGHT_GRAY);
      add(MainFrame, BorderLayout.EAST);
      add(Table, BorderLayout.WEST);
      addMouseListener(new clicked());
  }
  
  
  
  public void paint(Graphics g) {
      super.paintComponents(g);
      g.setColor(Color.black);
      for (int i = 0; i &lt;= 800; i += 16) {
          g.drawLine(0, i, 800, i);
          g.drawLine(i, 0, i, 800);
  //            g.fillRect(cX, cY, 16, 16);
      }
  }
  
  public static void main(String[] args) {
      JFrame win = new JFrame("Retarded Bullshit");
      win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      win.setPreferredSize(new Dimension(1216, 840));
      win.setContentPane(new Main());
      win.pack();
      win.setVisible(true);
  
  }
  
  public class clicked extends JApplet implements MouseListener {
  
  public int cX;
  public int cY;
  Graphics g;
  
  @Override
  public void mouseClicked(MouseEvent e) {
  //            Point a = e.getLocationOnScreen();
      int cellX = e.getX();
      int cellY = e.getY();
      if (cellX &lt; 800 &amp;&amp; cellX &gt; 0 &amp;&amp; cellY &lt; 800 &amp;&amp; cellY &gt; 0) {
          cX = cellX / 16 + 1;
          cY = cellY / 16 + 1;
  
          JOptionPane.showMessageDialog(null, "" + cX + " " + cY);
      }
  </code></pre>