aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes paintComponent() not drawing graphics correctly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "paintComponent() not drawing graphics correctly" Watch "paintComponent() not drawing graphics correctly" New topic
Author

paintComponent() not drawing graphics correctly

Ammar Karimi
Greenhorn

Joined: Apr 03, 2005
Posts: 5
Hi. I'm having a problem with painting some graphics. My program is basically a Little Man Computer simulation showing a character walking around the screen. When he reaches any one of the pre-determined points in the panel (i.e. a certain y-coordinate), I want to use an if statement in paintComponent() to determine that coordinate and draw a rectangle with some text, pause and then erase it. The movement of the character is taken care of by a thread, but the problem is that when he reaches a point, the graphics are not drawn in the order in which I have specified; rather, they all draw immediately at the end of the if statement.
Secondly, if I move another window over the panel, the rectangle and text gets erased (although the image of the character does not). I'm guessing this has something to do with the if statement.
Here is my code. I know there's a lot, so I've commented the parts which are unimportant (which is most of the code). The problematic part is in paintComponent(). Hope someone can help. Thanks.
PS: To confirm that the part where I commented THESE GRAPHICS ARE NOT DRAWN AT ALL! truly does not work, simply change the current values from 510, 220 to 260, 400 where the walk method is called (in public void run()).

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Ammar,

Welcome to JavaRanch!

You can't draw something, sleep, then draw something else, sleep, etc. within a single invocation of paintComponent(). Most of the time, Swing painting is done in "double-buffered" mode, meaning that everything in paintComponent() is directed to an off-screen area, and then that offscreen area is instantly swapped with the on-screen area so that the whole drawing appears at once. This avoid the unpleasant flickering that would otherwise occur as things are erased and redrawn.

Instead of trying to have changing drawings in a single frame, you need to have a frame counter which is incremented each time paintComponent() is called, and base your drawing on that frame counter -- i.e., when the frame counter is 0, draw only up to your "pause", and when it's 1, draw some more, and when it's 2, draw even more, etc. That's just how it's done.


[Jess in Action][AskingGoodQuestions]
Ammar Karimi
Greenhorn

Joined: Apr 03, 2005
Posts: 5
I couldn't have asked for a nicer welcome than the one you gave me I removed the pause statements from within paintComponent() and the animation is finally working! However, I'm still having the problem of other windows overwriting my rectangles and text. Here's a snippet of code to show the changes I made. I used a global variable tracker, and in my walk method (the one which controls the animation), I used an if statement so that when I reach a coordinate, I draw the image, pause, draw my rectangle, pause, etc.


Here's the part in paintComponent() responsible for handling the above code:


I had first added tracker = 0 and comment = false within paintComponent() (shown commented out at the end), but then moved it to within the part where I'm controlling my animation. But the rectangle and text are still being wiped out by other windows, eventhough I keep comment true till the end.
Hoping for another helpful reply
Ammar Karimi
Greenhorn

Joined: Apr 03, 2005
Posts: 5
Oh it's ok, I figured out my mistake I had written:


when I should have written:


With the former, as tracker was increased before the pause statement, if any window overlapped the text, repaint was called with the new value of tracker, which caused the text to be cleared. Placing the pause statement before increasing tracker meant that even when a window overlapped, repaint was called with the old tracker value and so the text was drawn again.
Cool, no replies and this forum still helped
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes, your change makes sense. Great, glad we could help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: paintComponent() not drawing graphics correctly