Note that there are some explanatory texts on larger screens.

plurals
  1. POAnimating a series of images
    text
    copied!<p>I am animating a series of images in java. So far I able to animate without any problem. The problem occur only when I add in control(Start, Stop, etc..). When I press Start in my GUI the GUI does not show the animation only the last frame is shown after the animation has ended.</p> <p>I not sure is it the threading problem or a painting problem, as I have tried a few methods and none of them worked.</p> <p>Following is my code:</p> <p><code> public class SwingAnimation extends JPanel implements ActionListener {</p> <pre><code>protected JFrame frame; protected JLabel lblDisplay; protected JButton BtnStart, BtnStop, BtnPause; protected JCheckBox chkLoop; protected Thread th; public static void main(String[] args) { SwingAnimation sa = new SwingAnimation(); } public SwingAnimation() { frame = new JFrame("Animation"); Panel panel = new Panel(); lblDisplay = new JLabel(); BtnStart = new JButton("Start"); BtnStop = new JButton("Stop"); BtnPause = new JButton("Pause"); chkLoop = new JCheckBox("Loop"); BtnStop.setEnabled(false); BtnPause.setEnabled(false); BtnStart.setActionCommand("start"); BtnStop.setActionCommand("stop"); BtnPause.setActionCommand("pause"); panel.add(lblDisplay); panel.add(BtnStart); panel.add(BtnPause); panel.add(BtnStop); panel.add(chkLoop); frame.add(panel, BorderLayout.CENTER); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the frame in the center of the screen Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); int screen_x = (screensize.width - frame.getWidth()) / 2; int screen_y = (screensize.height - frame.getHeight()) / 2; frame.setLocation(screen_x, screen_y); BtnStart.addActionListener(this); BtnStop.addActionListener(this); BtnPause.addActionListener(this); chkLoop.addActionListener(this); th = new Thread(); //ImageAnimator(); } public void ImageAnimator() { try { for (int i = 0; i &lt;= 299; i++) { ImageIcon images = new ImageIcon("C:\\Users\\Desktop\\Images\\Snap" + i + ".jpg"); lblDisplay.setIcon(images); th.sleep(25); } } catch (InterruptedException e) { } } public void actionPerformed(ActionEvent e) { if ("start".equals(e.getActionCommand())) { BtnStart.setEnabled(false); BtnStop.setEnabled(true); BtnPause.setEnabled(true); lblDisplay.setVisible(true); ImageAnimator(); } else if ("stop".equals(e.getActionCommand())) { BtnStart.setText("Start"); BtnStart.setEnabled(true); BtnStop.setEnabled(false); BtnPause.setEnabled(false); lblDisplay.setVisible(false); th = null; } else if ("pause".equals(e.getActionCommand())) { BtnStart.setText("Resume"); BtnStart.setEnabled(true); BtnStop.setEnabled(true); BtnPause.setEnabled(false); } } </code></pre> <p>} </code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload