title: Variable not passing over to method paint
body: <p>I have been searching google for the past day and a half trying to figure this out.
  As you can see I am trying to make the mouseClicked update the variables clickcolumnindex and clickrowindex in order to make the replaint() method in the mouseClicked method update and thus show the counter created by paint display within another column/row.</p>
  
  <p>I feel this may be a really simple fix after my trying loads of things, I have kinda lost my path of thought.</p>
  
  <p>Help would be greatly appreciated.</p>
  
  <pre><code>import java.awt.*;
  import java.awt.geom.*;
  import javax.swing.*;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseEvent;
  
              class GridPanel extends JPanel implements MouseListener
              {
                  int numCols;
                  int numRows;
                  int w;
                  int h;
                  int x;
                  int y;
                  int clickcolumnindex;
                  int clickrowindex;
  
  
                  public void mouseReleased(MouseEvent event)
                  {
                      System.out.println("Mouse released.");
                  }
                  public void mousePressed(MouseEvent event)
                  {
                      System.out.println("Mouse Pressed");
                  }
                  public void mouseClicked(MouseEvent event)
                  {
                      int clickcolumnindex = 7 * x / getWidth();
                      int clickrowindex = 5 * y / getHeight();
                      int x = event.getX();
                      int y = event.getY();
                      System.out.println("x position of click is: " +x);
                      System.out.println("y position of click is: " +y);
                      System.out.println("Click is in column " +clickcolumnindex);
                      System.out.println("Click is in row " +clickrowindex);
                      repaint();
  
                  }
  
  
     public void mouseReleased(MouseEvent event) {
        System.out.println("Mouse released.");
     }
  
     public void mousePressed(MouseEvent event) {
        System.out.println("Mouse Pressed");
     }
  
     public void mouseClicked(MouseEvent event) {
        int x = event.getX();
        int y = event.getY();
        System.out.println("x position of click is: " + x);
        System.out.println("y position of click is: " + y);
        System.out.println("Click is in column " + clickcolumnindex);
        System.out.println("Click is in row " + clickrowindex);
        repaint();
     }
  
     public void mouseEntered(MouseEvent event) {
        System.out.println("Mouse entered.");
     }
  
     public void mouseExited(MouseEvent event) {
        System.out.println("Mouse exited.");
     }
  
     public GridPanel(int nc, int nr) {
        numCols = nc;
        numRows = nr;
        addMouseListener(this);
     }
  
     Rectangle getRect(int thisCol, int thisRow) {
        // if input is out of range, return "null"
        if (thisCol &lt; 0 || thisRow &lt; 0)
           return null;
        if (thisCol &gt;= numCols || thisRow &gt;= numRows)
           return null;
        // otherwise, make and return the Rectangle
        int w = getWidth() / numCols;
        int h = getHeight() / numRows;
        int x = thisCol * w;
        int y = thisRow * h;
        Rectangle myRect = new Rectangle(x, y, w, h);
        return myRect;
     }
  
     public void paint(Graphics g) {
        g.setColor(Color.gray);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.black);
        Graphics2D g2 = (Graphics2D) g;
        // we'll use Graphics2D for it's "draw" method -
        // neater than the Graphics "drawRect" suppled
        // (which you could also use)
        for (int i = 0; i &lt; numCols; i++) {
           for (int j = 0; j &lt; numRows; j++) {
              Rectangle r = getRect(i, j);
              g2.draw(r);
           }
        }
        g2.setColor(Color.blue);
        Rectangle r1 = getRect(clickcolumnindex, clickrowindex);
        g2.fillOval(r1.x, r1.y, r1.width, r1.height);
        System.out.println("variable updates " + clickcolumnindex + clickrowindex);
     }
  
     // copied from the W2MouseEvents for convenience
     // (so we can run the program from GridPanel class too!)
     public static void main(String[] args) {
        W2MouseEvents w = new W2MouseEvents();
        w.setVisible(true);
     }
  }
  </code></pre>
  
  <p>W2MouseEvents.java:</p>
  
  <pre><code>import javax.swing.JFrame;
  
  public class W2MouseEvents extends JFrame {
     GridPanel myGridPanel;
  
     public static void main(String[] args) {
        W2MouseEvents w = new W2MouseEvents();
        w.setVisible(true);
     }
  
     public W2MouseEvents() {
        setTitle("Workshop 2 (Mouse Events): starting code");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 400);
        setLocation(300, 300);
        myGridPanel = new GridPanel(7, 5);
        add(myGridPanel);
     }
  
  }
  </code></pre>