using button click event to update variables in java
<p>i have drawn a graph and placed a 20*20 pixel box in it and i am trying to move the box around with a bunch of buttons. the trouble is, i cant seem to change the X-axis and Y-axis values of the box from within the action listener. this is my first graphics application, help!</p>

<p>GrapMain.java</p>

<pre><code>import java.awt.BorderLayout;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JTextField;

public class GraphMain {

    public static void main(String [] args){

        Display nashDisplay = new Display();
        nashDisplay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
</code></pre>

<p>Graph.java</p>

<pre><code>import java.awt.*;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Graph extends JPanel {
    int x = 5, y = 100;
    Graph (int x, int y){
        this.x = x;
        this.y = y;
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.WHITE);

        //horizontal lines
        g.setColor(Color.RED);
        for (int height=20; height&lt;200; height+=20){
            g.drawLine(5, height, 480, height);
        }
        //verticle lines
        g.setColor(Color.RED);
        for (int height=5; height&lt;400; height+=20){
            g.drawLine(height,200,height,10);
        }
        //the box
        g.setColor(Color.BLACK);
        g.fillRect(this.x, this.y, 20, 20);
        //g.fillRect((+20 to move right one box),(+20 to move down one box), 20, 20);
        //g.fillRect((-20 to move left one box),(-20 to move up one box), 20, 20);
    }
}
</code></pre>

<p>Display.java</p>

<pre><code>import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Display extends JFrame {

    int X = 5, Y = 100;
    private JButton left, right, up, down;
    Graph dude = new Graph(X, Y);

    public Display() {
        super("nash's graph(moving the box!)");

        JButton left = new JButton("move left");
        add(left, BorderLayout.WEST);
        left.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                X = X - 20;
            }
        });
        JButton right = new JButton("move right");
        add(right, BorderLayout.EAST);
        right.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                X = X + 20;
            }
        });
        JButton up = new JButton("move up");
        add(up, BorderLayout.NORTH);
        up.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Y = Y - 20;
            }
        });
        JButton down = new JButton("move down");
        add(down, BorderLayout.SOUTH);
        down.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Y = Y + 20;
            }
        });
        setSize(500, 300);
        setVisible(true);
        add(dude, BorderLayout.CENTER);
    }
}
</code></pre>
