Hello!

I am new to Java (having used it for only a few weeks), and this is my first post. In short, I am having some trouble making a rectangle move when a button is pressed. I understand the concept of an actionListener and how it is used, but I am having trouble calling 'repaint'.

Here is my code for class 'LeftButton': (This button should cause the rectangle's x-coordinate to decrease by 5 and repaint it on the screen.)

package Test;

public class LeftButton extends javax.swing.JButton {
private ColorRectangle _rectangle;
private DrawingPanel _dp;

public LeftButton(ColorRectangle rectangle, DrawingPanel dp) {
super("Left");
_rectangle = rectangle;
_dp = dp;
this.addActionListener(new LeftListener());
}

private class LeftListener implements java.awt.event.ActionListener {
public void actionPerformed(java.awt.event.ActionEvent e) {
_rectangle.moveLeft();
_dp.repaint();
System.out.println("xpos = " + _rectangle.getX());
}
}
}

I know that the actionPerformed method is being called since the println statement does indeed return the correct value for '_rectangle.getX()'. The 'moveLeft' method simply reduces the x-location of the rectangle by 5.

Here is my code for class 'DrawingPanel':

package Test;

import java.awt.*;
import javax.swing.*;

public class DrawingPanel extends JPanel {
private ColorRectangle _rectangle;

public DrawingPanel() {
super();
Dimension size = new Dimension(500,300);
this.setPreferredSize(size);
this.setSize(size);
this.setBackground(java.awt.Color.WHITE);
_rectangle = new ColorRectangle();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D brush = (Graphics2D) g;
_rectangle.paint(brush);
}

}

Since I have defined the method 'paintComponent', inherited from JPanel, shouldn't calling 'repaint()' on this class call 'paintComponent' for me?

Although I'm fairly certain my problem does not lie within my ColorRectangle class, I have included it below for reference, along with its superclass ColorShape:

package Test;

public class ColorRectangle extends ColorShape {

public ColorRectangle() {
super(new java.awt.geom.Rectangle2D.Double());
}
}

package Test;

import java.awt.*;
import java.awt.geom.*;

public abstract class ColorShape {
private RectangularShape _shape;
private Color _borderColor, _fillColor;

public ColorShape(RectangularShape shape) {
_shape = shape;
_shape.setFrame(50,50,100,100);
this.setBorderColor(Color.BLACK);
this.setFillColor(Color.BLUE);
}

public double getX() {
return _shape.getX();
}

public double getY() {
return _shape.getY();
}

public double getWidth() {
return _shape.getWidth();
}

public double getHeight() {
return _shape.getHeight();
}

public void setLocation(double xpos, double ypos) {
_shape.setFrame(xpos, ypos, _shape.getWidth(), _shape.getHeight());
}

public void setSize(double width, double height) {
_shape.setFrame(_shape.getX(), _shape.getY(), width, height);
}

public void setFillColor(Color color) {
_fillColor = color;
}

public Color getFillColor() {
return _fillColor;
}

public void setBorderColor(Color color) {
_borderColor = color;
}

public Color getBorderColor() {
return _borderColor;
}

public void setColor(Color color) {
_fillColor = color;
_borderColor = color;
}

public void moveLeft() {
this.setLocation(_shape.getX()-5,_shape.getY());
}

public void moveRight() {
this.setLocation(_shape.getX()+5,_shape.getY());
}

public void paint(Graphics2D brush) {
brush.setColor(_borderColor);
brush.draw(_shape);
brush.setColor(_fillColor);
brush.fill(_shape);
}
}

I really appreciate any help! :)