Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: How do I get an animated gif to reset to the biginning frame?

  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default How do I get an animated gif to reset to the biginning frame?

    I've gotten the animation to play the first time, but when I try to run it again (without closing the app) it is just stuck on the last frame. I've tried a ton of things and nothing seems to work. I know there's probably a really simple solution, but I haven't found it yet. Here's the whole darn mess. Oh yeah, I'm trying to play a .wav file at the same time the animation runs. All help is appreciated.

    public class AnimalCow extends JFrame {
    JPanel contentPane;
    JLabel imageLabel = new JLabel();
    JLabel headerLabel = new JLabel();
    JLabel imageLabelTwo = new JLabel();
    AudioClip sound;
    File wavFile = new File("moo.wav");
    ImageIcon ii = new ImageIcon(this.getClass().getResource(
    "cow1.gif"));
    ImageIcon ia = new ImageIcon(this.getClass().getResource(
    "cowmooanimation.gif"));

    int count = 2;
    public AnimalCow() {
    try {
    setBounds(100, 100, 450, 300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    contentPane = (JPanel) getContentPane();
    setSize(new Dimension(400, 400));
    setTitle("Cow");
    getContentPane().setLayout(null);
    headerLabel.setBounds(0, 0, 392, 34);
    // add the header label
    headerLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 24));
    headerLabel.setText(" I am a Cow. ");
    contentPane.add(headerLabel);
    // add the image label
    imageLabel.setBounds(0, 34, 241, 332);
    imageLabel.setIcon(ii);
    imageLabel.setVisible(true);
    contentPane.add(imageLabel);

    imageLabelTwo.setBounds(0, 34, 241, 332);
    imageLabelTwo.setIcon(ia);
    imageLabelTwo.setVisible(false);
    contentPane.add(imageLabelTwo);

    final JButton btnNewButton = new JButton("\"Click Me\"");
    btnNewButton.addActionListener(new ActionListener() {
    @SuppressWarnings("deprecation")
    public void actionPerformed(ActionEvent arg0) {
    try {
    sound = Applet.newAudioClip(wavFile.toURL());
    } catch (Exception e){
    e.printStackTrace();
    }
    if (imageLabel.isVisible()){
    imageLabel.setVisible(false);
    imageLabelTwo.setVisible(true);
    btnNewButton.setText("\"Reset\"");
    }
    else {
    imageLabel.setVisible(true);
    imageLabelTwo.setVisible(false);
    btnNewButton.setText("\"Click Me\"");
    }
    }
    });
    btnNewButton.setBackground(Color.LIGHT_GRAY);
    btnNewButton.setBounds(174, 199, 187, 61);
    btnNewButton.setFont(new Font("Comic Sans MS", Font.BOLD, 18));
    getContentPane().add(btnNewButton);


    // show it
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    } catch (Exception exception) {
    exception.printStackTrace();
    }

    }

    public static void main(String[] args) {
    new AnimalCow();

    }
    }
    Last edited by NotTooOld; 04-02-2013 at 03:37 PM.

  2. #2
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: How do I get an animated gif to reset to the beginning frame?

    anyone?

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

    Default Re: How do I get an animated gif to reset to the biginning frame?

    ImageIcon.getImage().flush()
    That resets the underlying image as if it were newly loaded.
    NotTooOld likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: How do I get an animated gif to reset to the biginning frame?

    That's why you're the mod! Works great!!

    Thanks so much.

Similar Threads

  1. Animated GIF iterations
    By pepinliria in forum Java 2D
    Replies: 3
    Last Post: 12-21-2011, 11:29 PM
  2. Animated gif
    By tamoti in forum Java 2D
    Replies: 4
    Last Post: 11-04-2011, 04:29 PM
  3. reset an animated gif?
    By totj in forum New To Java
    Replies: 0
    Last Post: 06-30-2011, 06:56 AM
  4. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 05:26 PM
  5. Playing an Animated GIF
    By c0m4ndo45 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2009, 11:47 AM

Posting Permissions

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