Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default trying to animate jpanel with button

    I'm trying to make an animation with a jpanel oval on a jframe that is triggered by a button i press made from jbutton. when i do this, I keep getting a big movement(jump) rather than a smear. with out the button i get the smear... can anyone help me out...

    i posted this in the new to java thread, but no one could help me.


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class AnimatedB extends JFrame implements ActionListener
    {
    JFrame frame;
    Ken4Funny ken;
    JButton button;
    int x = 20;
    int plusx = 5;
    int y = 50;
    int plusy = 5;
    int xx;
    public static void main(String[] args)
    {
    AnimatedB animate = new AnimatedB();
    animate.go();
    }
    
    public void go()
    {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ken = new Ken4Funny();
    button = new JButton("animate");
    button.addActionListener(this);
    frame.getContentPane().add( ken);
    frame.getContentPane().add(BorderLayout.SOUTH, button);
    
    frame.setSize(500,500);
    frame.setVisible(true);
    
    
    }
    class Ken4Funny extends JPanel 
    {
    
    public void paintComponent(Graphics g)
    {
    
    
    g.setColor(Color.orange);
    g.fillOval(x, y, 100, 100);
    
    }
    
    
    }
    public void actionPerformed(ActionEvent event)
    {
    for(xx = 0; xx <=100; xx++)
    {
    x++;
    y++;
    ken.repaint();
    try{
    Thread.sleep(50);
    } catch(Exception ex) {}
    
    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Your actionPerformed( ... ) method runs in Swing's EDT (Event DisPatch Thread). That same thread is used for painting requests (such as repaint())
    The EDT is busy so it can't draw anything; when that loop is ready everything is drawn quickly, one oval after another one; you see that very fast movement as one big move.

    Take the code in your actionPerformed( ... ) method to another thread and all will be fine.

    kind regards,

    Jos

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

    Default

    For simple animations like this one where you don't have a time or cpu-intensive process going on, I wouldn't even use a background thread but instead would use a Swing Timer. For e.g.,

    Java Code:
    class AnimatedB extends JFrame {
      private static final int TIMER_DELAY = 50;
      private static final int X_Max = 200;
      //...
    
      //....
    
      public void go() {
        //....
        button = new JButton("animate");
        button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            buttonActionPerformed(e);
          }
        });
        //......
      }
    
      private void buttonActionPerformed(ActionEvent e) {
        new Timer(TIMER_DELAY, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            timerActionPerformed(e);
          }
        }).start();
      }
    
      private void timerActionPerformed(ActionEvent e) {
        if (x < X_Max) {
          x++;
          y++;
          ken.repaint();
        }
      }
    
      class Ken4Funny extends JPanel {
        public void paintComponent(Graphics g) {
          super.paintComponent(g); // *** don't forget this!! ***
          g.setColor(Color.orange);
          g.fillOval(x, y, 100, 100);
    
        }
      }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Strictly speaking, though, isn't the Timer a background thread?
    At least the timing parts...:)

  5. #5
    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 Tolls View Post
    Strictly speaking, though, isn't the Timer a background thread?
    At least the timing parts...:)
    True, true. And you may even consider it "syntactic sugar" since its functionality can be recreated by background threads, but it sure simplifies things at times.

  6. #6
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    thanks all

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

Similar Threads

  1. .add to a JPanel
    By harrier in forum NetBeans
    Replies: 11
    Last Post: 07-13-2010, 11:24 AM
  2. I need some help with JPanel
    By bantes in forum AWT / Swing
    Replies: 7
    Last Post: 11-03-2009, 08:58 PM
  3. can't get anything to display on my JPanel
    By v1nsai in forum AWT / Swing
    Replies: 9
    Last Post: 08-25-2009, 09:25 AM
  4. How to use Jpanel
    By Manfizy in forum NetBeans
    Replies: 0
    Last Post: 02-19-2009, 01:34 PM
  5. [SOLVED] Help with JPanel
    By Azndaddy in forum New To Java
    Replies: 4
    Last Post: 05-04-2008, 12:57 PM

Posting Permissions

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