Codex

Animation

Jul 20th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class SwingAnimation implements Runnable,ActionListener
  6. {
  7.     Thread th =new Thread(this);;
  8.     volatile boolean animate=true;
  9.     ImageIcon images;
  10.     JFrame frame;
  11.     JLabel jlb;
  12.     int i = 0;
  13.     int j;
  14.         JButton jb,jb2;
  15.     Panel panel3;
  16.         public SwingAnimation()
  17.         {
  18.         frame = new JFrame("Animation Frame");
  19.        
  20.         jlb = new JLabel();
  21.         panel3= new Panel();
  22.         panel3.add(jlb);
  23.         jb=new JButton("Pause");
  24.         jb.setActionCommand("Pause");
  25.         jb.addActionListener(this);
  26.         panel3.add(jb);
  27.         jb2=new JButton("Resume");
  28.         jb2.setActionCommand("Resume");
  29.         jb2.addActionListener(this);
  30.         panel3.add(jb2);
  31.         frame.add(panel3, BorderLayout.CENTER);
  32.         frame.setSize(400, 400);
  33.         frame.setVisible(true);
  34.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.         th.start();
  36.     }
  37.                 public void run()
  38.         {
  39.         try
  40.                    {
  41.             while(true)
  42.             {
  43.            
  44.             for(i = 1; i <= 3; i++)
  45.                 {
  46.                 if(animate)
  47.                 {
  48.                            images = new ImageIcon("images/img" + i + ".gif");
  49.                    jlb.setIcon(images);
  50.                    th.sleep(400);
  51.                             }
  52.                 }
  53.                        }  
  54.                   }
  55.                   catch(InterruptedException e){}
  56.     }
  57.  
  58.    
  59.     public void actionPerformed(ActionEvent ae)
  60.     {
  61.      if((ae.getActionCommand()).equals("Pause"))
  62.      {
  63.          animate=false;
  64.          jb.setEnabled(false);
  65.          jb2.setEnabled(true);
  66.         }
  67.      else if((ae.getActionCommand()).equals("Resume"))
  68.      {
  69.        animate=true;
  70.        jb2.setEnabled(false);
  71.        jb.setEnabled(true);
  72.        }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment