Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class SwingAnimation implements Runnable,ActionListener
- {
- Thread th =new Thread(this);;
- volatile boolean animate=true;
- ImageIcon images;
- JFrame frame;
- JLabel jlb;
- int i = 0;
- int j;
- JButton jb,jb2;
- Panel panel3;
- public SwingAnimation()
- {
- frame = new JFrame("Animation Frame");
- jlb = new JLabel();
- panel3= new Panel();
- panel3.add(jlb);
- jb=new JButton("Pause");
- jb.setActionCommand("Pause");
- jb.addActionListener(this);
- panel3.add(jb);
- jb2=new JButton("Resume");
- jb2.setActionCommand("Resume");
- jb2.addActionListener(this);
- panel3.add(jb2);
- frame.add(panel3, BorderLayout.CENTER);
- frame.setSize(400, 400);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- th.start();
- }
- public void run()
- {
- try
- {
- while(true)
- {
- for(i = 1; i <= 3; i++)
- {
- if(animate)
- {
- images = new ImageIcon("images/img" + i + ".gif");
- jlb.setIcon(images);
- th.sleep(400);
- }
- }
- }
- }
- catch(InterruptedException e){}
- }
- public void actionPerformed(ActionEvent ae)
- {
- if((ae.getActionCommand()).equals("Pause"))
- {
- animate=false;
- jb.setEnabled(false);
- jb2.setEnabled(true);
- }
- else if((ae.getActionCommand()).equals("Resume"))
- {
- animate=true;
- jb2.setEnabled(false);
- jb.setEnabled(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment