Results 1 to 1 of 1
- 10-24-2011, 09:26 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
Help with using Swing's 'paintComponent' and 'repaint' methods
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! :)
Similar Threads
-
paintComponent problem
By luke in forum New To JavaReplies: 5Last Post: 04-02-2011, 02:07 PM -
repaint() does not recall an override method paintComponent(), why is that?
By ilovevcoffe in forum AWT / SwingReplies: 3Last Post: 02-04-2011, 06:30 AM -
Help with paintComponent!
By joeyea in forum Java 2DReplies: 6Last Post: 12-27-2010, 01:59 PM -
Java Swing repaint
By sz20b in forum Advanced JavaReplies: 1Last Post: 10-04-2010, 02:59 PM -
paint() and paintComponent()
By goldhouse in forum Java 2DReplies: 1Last Post: 07-17-2007, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks