Results 1 to 5 of 5
  1. #1
    nolsen01 is offline Member
    Join Date
    Jul 2009
    Posts
    18
    Rep Power
    0

    Default Simple animation won't work

    I'm following a tutorial. The tutorial is using AWT but I chose to use swing instead so that may have something to do with it. Its very simple code so I'll post it here.

    The idea is to just move a 2D ball from left to right across the screen. However, instead of repainting the screen, it drags the ball across the screen and leaves a thick red line where the ball was moving.

    Java Code:
    package org.me.hello;
    
    /**
     *
     * @author Nick
     */
    import java.awt.Color;
    import javax.swing.*;
    import java.awt.Graphics;
    
    
    public class MyApplet extends JApplet implements Runnable{
    
        private int x_pos = 10;
        private int y_pos = 100;
        private int radius = 20;
    
        
        @Override
        public void init(){
            setBackground(Color.blue);
        }
    
        @Override
        public void start(){
            //Thread.start runs the run() method of the target class
            //in this case, "this"
            Thread th = new Thread(this);
            th.start();
        }
    
        @Override
        public void stop(){
    
        }
    
        @Override
        public void destroy(){
    
        }
    
        @Override
        public void paint(Graphics g){
            g.setColor(Color.red);
            g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
        }
    
    
    
    
        public void run() {
    
            //set currentThread to lowest priority
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
            while(true){
                
                x_pos++;
    
                repaint();
    
                //make this thread sleep for 20 milliseconds
                try{
                    Thread.sleep(20);
                }catch(InterruptedException ex){
                    //do nothing
                }
                
                Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            }
        }
    
    
    
    
    }
    Last edited by nolsen01; 07-08-2009 at 09:45 PM.

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

    Default

    To fully repaint the screen you need to have the super paint method called as the first method of the paint method. In other words, instead of this:
    Java Code:
        public void paint(Graphics g){
            g.setColor(Color.red);
            g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
        }
    use this:
    Java Code:
        public void paint(Graphics g){
            super.paint(g); // repaints the background of the component
            g.setColor(Color.red);
            g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
        }

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

    Default

    Having said this, if you really want to do Swing animation, you'll make other changes:

    1) Draw in a JPanel, not directly in the JApplet itself.
    2) Override the JPanel's paintComponent method, not the paint method.
    3) Use a Swing Timer instead of a while (true) loop.
    4) Call the Swing code using SwingUtilities invoke and wait for thread safety.

    For instance:
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class SwingAnimation2 extends JApplet {
      @Override
      public void init() {
        try {
          // start Swing in a thread-safe way
          SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
              // add an animation panel object to the applet's contentPane
              getContentPane().add(new AnimationPanel());
            }
          });
        } 
        // to the purist's out there, sorry for catching a naked Exception... 
        catch (Exception e) {
          System.err.println("Applet failed to initialize");
        }
      }
    }
    
    class AnimationPanel extends JPanel {
      private static final int RADIUS = 20;
      private static final int TIMER_DELAY = 20;
      private int  xPos = 10;
      private int yPos = 100;
    
      public AnimationPanel() {
        setBackground(Color.blue);
        javax.swing.Timer timer = new javax.swing.Timer(TIMER_DELAY, new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            xPos++;
            AnimationPanel.this.repaint();
          }
        });
        timer.start();
      }
      
      @Override
      // override a Swing JComponent's paintComponent, not the paint method
      protected void paintComponent(Graphics g) {
        super.paintComponent(g); // again to repaint the screen
        
        // set antialiasing rendering hints to smooth out the circle
        // this is not necessary but makes for prettier animation
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
        
        g.setColor(Color.red);
        g.fillOval(xPos  - RADIUS, yPos  - RADIUS, 2 * RADIUS, 2 * RADIUS);
      }
    }
    Last edited by Fubarable; 07-08-2009 at 11:59 PM.

  4. #4
    nolsen01 is offline Member
    Join Date
    Jul 2009
    Posts
    18
    Rep Power
    0

    Default

    oh i see. I guess because JApplet overloads the paint method from the original Applet class?

    Thank you.

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

    Default

    You really should not draw directly in a JApplet or JFrame. Much better is to draw in a JPanel or JComponent (see my example code above). The Sun Swing tutorials are very helpful here and it would likely help you a great deal if you had a look at the Swing graphics and 2D graphics sections.

Similar Threads

  1. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 04:26 PM
  2. Replies: 3
    Last Post: 05-06-2009, 12:11 AM
  3. JTextArea Animation
    By hoglu in forum New To Java
    Replies: 1
    Last Post: 12-18-2008, 11:44 AM
  4. Text animation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:44 PM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 AM

Posting Permissions

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