aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Gui animation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Gui animation" Watch "Gui animation" New topic
Author

Gui animation

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I have a problem with my animation, below shows some pseudo code that moves a car across the screen.
The code is all run inside a new Thread and try catch blocks omitted for clarity.


My problem is on the first iteration after the car has moved 100, it does not display this. I'm not sure what I'm doing wrong.
Should I open a new thread for every repaint()? Doesn't sound right, can anyone guide me, thanks.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
Use a Swing Timer to schedule the animation.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, I got it to work, and the code is much clearer now, no while loops needed or Thread.sleep(..).

I'm still running Timer in a Thread, I think I need to do this so the gui remains responsive, is this right? Thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
I'm still running Timer in a Thread, I think I need to do this so the gui remains responsive, is this right?


No the Timer itself does not need to be in a Thread. When the Timer fires the code in the actionPerformed() method is executed on the EDT (just like you clicked a JButton). So if that code is time consuming then it needs to be wrapped in a SwingUtilities.invokeLater. But based you your code which is just changing the location of a component, this is not necessary in your case and as a general rule it will not be the case since animation code needs to be efficient are part of the animation.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hmm, I've still got a problem with my program...

Basically what I do in the actionlistener for the swing timer:

1.Math Calculations
2.Draw to screen
3.More calculations to see if timer needs to be stopped (inside a SwingUtilities.invokeLater block)

Then I have the timer set on repeats for a short interval.

After timer stops the image on screen is crooked, so I'm pretty sure some of the math calculations cant have been performed as the data is not as it should be.

So I'm guessing the timers delay is shorter than the time needed to complete steps 1,2,3.
I don't get how the Timer works, is it possible for it to wait the time delay given and then a little more if needed.

Thanks, but I might be totally wrong on what the problem is too.

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Ok, well what I've done is set timer to non-repeat.

Then

1. as before
2. as before
3. as before
4. timer.start() (inside same InvokeLater block as 3)

so that its sort of recursive, and this seems to work fine, but is it good programming style?

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
Sounds like you are making the code far too complex. When the Timer fires you calculat the cars location and repaint the car.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, the car thing was an example. Its actually a rubiks cube.

Its practically impossible to do a short example, cause of the amount of code.

I got it working 99.9999% accuratley, but noticed it still gave 1 misalignment when the timer was set to 0.

I can't seem to replicate that bug now, so I'll just have to avoid having 0 time as an option I guess.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
but noticed it still gave 1 misalignment when the timer was set to 0.


Well, I've never set a Timer to 0. You need to specify some reasonable interval.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Well I agree with that, using 0 defeats the purpose of using a timer in the first place.

However, I don't understand why I get these tiny misalignments, some cube slices don't seem to complete their twist, leaving small cracks in the cube. Surely even with no timer this would not happen.

Its possible I have a bug in the code due to something unrelated to timer, but I haven't found it yet, and its very awquard to replicate this bug, except when I don't use that recursive method, it crops up more frequently then.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Ok, back again, I can get the misalignment to show up using:

Inside ActionPerformed method of the AbstractAction used with Swing.Timer:

timer.setRepeats(false), then start the timer

1. Some math calculations
2. draw to screen
3. for(int lp=0;lp<100000000;lp++);
4. more calcs to see if timer is stopped (inside InvokeLater block)
5. timer.start() (if need to continue)


So there is a loop (3) which kinda slows down exectution, but i dont see why things should be different by including that apart from the slowness. I also have the whole thing running in a separate thread, but doubt that is the reason.

Any thoughts?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
colin shuker wrote:
Its practically impossible to do a short example, cause of the amount of code.

I will bet you that with a bit of effort, you will be able to do this, and then we'd be able to understand your problem a whole lot better. Please give it a go.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I've figured out the problem, I basically has a counter++ after the draw method, so if a new twist (on same axis of rotation) gets added (say during the for(int lp=0;lp<1000000000;lp++); loop) it then misses its first step of rotation. I was assuming it missed the last step cause the cube slices didnt always turn a full quarter turn. It seems to be fixed now, thanks.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
While I'm on same topic, I have another question...

I use swing Timer to twist the cube by rotating then repainting N times.
I now want to be able to trigger a sequence of twists, not necessarily one after the completion of the previous (since cube can perform simultaneous twists about any 1 of its 3 axes of rotation), but with say half a second between them.

I'm not sure if util Timer is better to use than Swing Timer this time, remember this timer will then have the other swing timer started as mentioned above.
I'm guessing I need Swing Timer again, but I'm not sure, any thoughts?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
colin shuker wrote:I'm not sure if util Timer is better to use than Swing Timer this time, remember this timer will then have the other swing timer started as mentioned above.
I'm guessing I need Swing Timer again, but I'm not sure, any thoughts?


My take on this: If you want the code that the timer calls intermittently to be called on the Swing Event Dispatch Thread (the EDT), then use a Swing Timer. If on the other hand you are calling this timer from within a background thread and you are trying to keep all the actions done in the background thread, then use a java.util.Timer.

My guess is that you will want to use a Swing Timer here since the Timer will trigger code that will change both the state of your model (this might possibly be done in a background thread) and the visual representation of this model.
 
Don't get me started about those stupid light bulbs.
 
subject: Gui animation