View Full Version : Java painting?

Mar 25, 2009, 05:14 PM
Sorry, I ask for so much help... :(

Ok, so I'm doing my first graphics! I've progressed so far =')

So, I'm trying to start early with dirty rectangles, which you can read about by Googling it. I picked up this skill from Python :D

However, I'm clueless on how to implement this in Java. My current code for an instance of a class that is a rectangle that can move around is:

public void redraw(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics;
Rectangle2D.Double eraserRect = new Rectangle2D.Double( (int) oldX - width/2 + 0.5, (int) oldY - width/2 + 0.5, width, height);
Rectangle2D.Double rect = new Rectangle2D.Double( (int) x - width/2 + 0.5, (int) y - height/2 + 0.5, width, height);

Problem is, when I only render the background the first paint(), the next time I repaint it it makes the background grey and a box the color of the background, where the old box used to be. It also has a new box in the new location. So basically, it works, if I only rendered the changing parts. It's not rendering the background a second time, but it's updating the whole Canvas, so it leaves the background gone when I repaint().

Can anyone show me how to implement dirty rectangles in Java? It will be a great help. Thanks :D
I use a Canvas as my drawing area. Can someone show me how? Thanks.