aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help with Paint commands in an animation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with Paint commands in an animation" Watch "Help with Paint commands in an animation" New topic
Author

Help with Paint commands in an animation

Nick Sharp
Greenhorn

Joined: Jul 14, 2009
Posts: 8
I am trying to construct a simple animation in which several boxes float around the window and bounce of the edges (like a screen saver). I'm made it work with a single box, but ran into trouble adding more boxes. The parts of my code that assign the position of the box seem to work fine, but when I try to paint it (should I be calling the paintComponent() of the box or repainting the frame?) it paints one box four times, instead of four boxes once each. I can post the rest of my code if necessary.

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Can we see the part of the code where the ArrayList rectangles gets populated, and also your paint routine and your rectangle class.

Whatever the problem is, there isn't enough info in what you've shown to determin an answer.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Don't do excessive work on the event dispatcher thread (EDT). Read these:
http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

In short, you'll need a different thread to do the work, then do updates on the EDT. You can use EventQueue.invokeLater or even better SwingWorker.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Rob, I can't see how that explains the apparent problem of one box being painted four times, instead of four boxes one time, but maybe I miss something. How does mywaiter.wait(500); influence your answer?
Nick Sharp
Greenhorn

Joined: Jul 14, 2009
Posts: 8
Here is a bunch more code, in its current form:







The relevant methods from the MovingRectangle class

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I think your problem is with incorrect use of Math.random(). Your routine doesn't paint the same box four times, you have essectially created four identical boxes.

I think. I would look at creating a Random object, say Random rand = new Random();

then use things like rand.nextInt() or rand.nextDouble() or whatever is appropriate. The thing is, you only want to create the Random() object once and keep using that object to generate new random values as you need them.

Anyways, I'm not 100% certain, but I think that's what it is.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
Well with all the random bits of code I'm not sure whats going on either. I dont' know what your MovingRectangle class does, but the paintComponent() code doesn't look right.



That does change where the component is painted relative to its parent. It changes where the rectangle is painted on itself, which doesn't make sense to me.

You don't need to create a custom class. To create a rectangle all you need to do is:



Then you add the panel to the parent container. When you want to move the rectangle you use the setLocation(...) method.

Also, you should be using a Timer to control the animation, not a while loop with a Thread.sleep().

Here is some old code I have lying around:


Note how much easier it is to understand whats going on when you have the whole picture (ie executable code)?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

One thing I'm missing is the call to super.paintComponent(g). Without this, the panel will not remove the previous painting, and your previous drawn rectangle remains.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I withdraw my answer about Math.random(). Subsequent research does not support my initial conclusion.
Nick Sharp
Greenhorn

Joined: Jul 14, 2009
Posts: 8
ok. thanks for all the help guys, i'm trying to teach myself using a book, and i keep on getting bored with the book and trying to do my own things. The applications of threads still very much confuse me.



From what I've read from you I think my problem is that i've tried to draw the box by creating a JPanel for each block and drawing the box there, instead I need to create a single panel, place it in my frame, and proceed to draw boxes in that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Would seem easier. That way you have no LayoutManager that tries to place your boxes in positions you don't want them to be in.
 
 
subject: Help with Paint commands in an animation