• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Paint commands in an animation

 
Nick Sharp
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a bunch more code, in its current form:







The relevant methods from the MovingRectangle class

 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I withdraw my answer about Math.random(). Subsequent research does not support my initial conclusion.
 
Nick Sharp
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic