Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Simple animation. Strange behavior

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple animation. Strange behavior

    When I run the code below with the "thread.sleep", the ball runs from left corner to right corner, leaving a trail behind it as expected.

    but when I remove the thread.sleep part, the balls appears directly at the right side corner, without a trail (similar to the behavior after adding a "fill.rec", but there is no such thing is this code".)

    So I ran the code without "thread.sleep" in DEBUG mode. Then it left a trail as expected. The same code, but in debug mode. What is going on here?

    package mypong;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    /**
     *
     * @author Administrator
     */
     
     
    public class MyPong extends JFrame
    {
        int x=0; //ball X coordinate at the beginning
        int y=0; //ball's Y coordinate at the beginning
        int xPos=100;
        int yPos=100;
        int circ=20; //width height of the ball
     
         //initialize the frame
     
         public void go() 
         {          
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            MyDrawPanel drawPanel = new MyDrawPanel();     
     
            frame.getContentPane().add(drawPanel);
            frame.setSize(480,500);
            frame.setVisible(true);                
     
     
     
            while (true)
            {     
     
               if (x<440) 
               { 
     
                    x+=1;
                    y+=1; 
               }
     
                 drawPanel.repaint();
     
    //        try
    //        {
    //            Thread.sleep(10);
    //        }
    //        catch(Exception x)
    //        {
    //           x.printStackTrace();
    //        }
            }//end for
     
         }//end go
     
         public static void main(String[] args)
         {
             MyPong mypong=new MyPong();
             mypong.go();
         }
     
        //inner class drawpanel
     
        class MyDrawPanel extends JPanel
        {
            @Override
            public void  paintComponent(Graphics g)
            {
     
                g.setColor(Color.black);
                g.fillOval(x, y, circ, circ);
     
     
            }
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple animation. Strange behavior

    The call to repaint() is only a *request* for the JPanel to repaint itself. Multiple calls to repaint() will be collapsed into a single paint event. Removing the call to Thread.sleep() causes you to call repaint() a ton of times in a short amount of time, which causes them to be collapsed into a single paint event.

    You should also use a new Thread directly, as right now you're tying up the main thread instead of creating your own. That might work for your current setup, but it won't work when you eventually want to start an animation based on user action.

    You might also consider using a data structure such as a List of positions to draw, or a buffer image that you draw to directly, instead of relying on repaint() to keep track of old frames. Those old frames can be cleared out any time (such as when your program is minimized or behind another component), so relying on them is not a good idea.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple animation. Strange behavior

    The first call in a paintComponent() method should be:

    super.paintComponent( g );

    That will correctly remove the trail. If you want to include a trail effect, then you should do as KW suggested and add the last (some number) of balls to a data structure, perhaps with decreasing opacity, perhaps only saving every Xth ball depending on the ball's velocity, and print the trail or history in the paintComponent() method.

Similar Threads

  1. Java strange thread behavior
    By Bingo90 in forum Threads
    Replies: 7
    Last Post: October 19th, 2013, 10:53 AM
  2. Simple Game Physics & Animation (Air Hockey)
    By Spaces Allowed? in forum AWT / Java Swing
    Replies: 6
    Last Post: November 2nd, 2011, 04:48 AM
  3. Having trouble with a simple animation
    By knightmetal in forum AWT / Java Swing
    Replies: 2
    Last Post: September 24th, 2011, 02:40 PM
  4. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  5. Simple GUI/Animation question...
    By teslatrain in forum AWT / Java Swing
    Replies: 3
    Last Post: April 29th, 2011, 04:10 PM

Tags for this Thread