Results 1 to 2 of 2
  1. #1
    alexdlaird is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default 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.

    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) {}
    }
    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.

    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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by alexdlaird View Post
    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.
    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.

    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.
    You need to follow what the tutorials are telling you to do:
    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.

    Secondly, is this a legitimate way of doing simple animation, or am I breaking the rules of Java animation?
    Yes you are breaking rules that shouldn't be broken, but by following the tutorials, fortunately this can all be corrected.

    Much luck and HTH.

Similar Threads

  1. Simple "if" statement problem....compiling error.
    By CYANiDE in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 10:56 PM
  2. Convert " ü " into simple " u "
    By nitinb4u in forum New To Java
    Replies: 4
    Last Post: 02-23-2009, 09:35 AM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Need help with "a simple order entry program"
    By sentica in forum New To Java
    Replies: 6
    Last Post: 10-17-2008, 06:38 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •