Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Awesomeness

macrumors member
Original poster
Feb 12, 2009
73
0
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:

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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.