Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 505739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:37:45+00:00 2026-05-13T06:37:45+00:00

I am animating a series of images in java. So far I able to

  • 0

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.

I not sure is it the threading problem or a painting problem, as I have tried a few methods and none of them worked.

Following is my code:


public class SwingAnimation extends JPanel implements ActionListener {

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 <= 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);
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:37:46+00:00Added an answer on May 13, 2026 at 6:37 am

    Quick Fix:

    Change ImageAnimator() like this:

            lblDisplay.setIcon(images);
            lblDisplay.paintImmediately(getBounds());
            th.sleep(25);
    

    The problem you’re having is that setting an icon does not automatically cause Swing to repaint the component onscreen. Calling paintImmediately will do that.

    General advice:

    Animation in swing is normally done using the Swing Timer. You’ll notice at the moment that your UI is unresponsive – once you’ve started the animation, you won’t be able to stop it until its over. That’s because all Swing events happen on a single thread – the Event Dispatch Thread. Running a loop with Thread.sleep(…) in the middle ties this thread up, leaving it unavailable to process other input (such as pressing the stop button)

    This article helped me immeasurably when I was trying to understand how Swing handles concurrency, and The Swing Trail has lots of advice on using Swing effectively, painting custom components etc.

    I’d also plug the demo code on Filthy Rich Clients, I’m working through the book at the moment and it’s worth a look.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I make an UIImageView animate a series of pictures in a view under control
I m un-animating a list using this code... function unanimate_li() { $li.filter(':last') .animate({ height:
I want to animate a series of items in jquery 1.3, with each next
I have a series of words animating, using css3. A background image of the
I want to display series of images one by one when the app loads.
I have a series of approximately 50 images each approximately 100kb each that needs
I need to do series of animation on 4 different images. The images are
I am trying to simulated animation based on a series of 54 images. I
On a site I am working on I load up a series of images
How does one animate path data in wpf? We have a series of templated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.