Results 1 to 2 of 2
Thread: Simple Animation "Blur"
- 11-14-2009, 07:10 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
Simple Animation "Blur"
I have a vehicle specified to move a number of spaces on a game board in a particular direction. For this example, the vehicle is moving left. Since the animation is short and simple (and this is the only animation in the game), I'm trying to avoid launching a whole new thread, overwriting paint, etc. as most of the tutorials show. This code successfully moves the car and repaints at each new position.
However, the ISSUE itself is that it repaints at each step ... Without unpainting the old car image ;). Thus, I get an amoebo-like car moving across the screen. Strangely, once this loop completes and the car gets to its new position, all other images are disposed and only the correct image remains.Java Code:for(int i = oldLoc.x; i > newLoc.x; i -= PIXEL_JUMP) { vehicle.setLocation(i, newLoc.y); boardPanel.repaint(); try { boardPanel.update(boardPanel.getGraphics()); Thread.sleep(ANIMATION_SLEEP); } catch(NullPointerException e) {} catch(InterruptedException e) {} }
QUESTION: What is it that I need to be calling to dispose of the old image before repainting my current new one as the car moves across the board? Secondly, is this a legitimate way of doing simple animation, or am I breaking the rules of Java animation?
I've been programming Java for years, but this is my first experience with animation. Thanks for your help!
-
but by calling thread.sleep on the EDT, you are breaking a serious Swing rule, and by not overriding a painting method, you are breaking other important and necessary rules.
You need to follow what the tutorials are telling you to do:However, the ISSUE itself is that it repaints at each step ... Without unpainting the old car image ;). Thus, I get an amoebo-like car moving across the screen. Strangely, once this loop completes and the car gets to its new position, all other images are disposed and only the correct image remains.
1) Use a Swing Timer, not Thread.sleep
2) Override paintComponent on a JPanel or JComponent
3) Call super.paintComponent(g) as the first method of the paintComponent method override -- to erase the old images.
Yes you are breaking rules that shouldn't be broken, but by following the tutorials, fortunately this can all be corrected.Secondly, is this a legitimate way of doing simple animation, or am I breaking the rules of Java animation?
Much luck and HTH.
Similar Threads
-
Simple "if" statement problem....compiling error.
By CYANiDE in forum New To JavaReplies: 4Last Post: 10-14-2009, 09:56 PM -
Convert " ü " into simple " u "
By nitinb4u in forum New To JavaReplies: 4Last Post: 02-23-2009, 08:35 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
Need help with "a simple order entry program"
By sentica in forum New To JavaReplies: 6Last Post: 10-17-2008, 05:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks