Search code examples
javaloopsanimated-gif

the gifs are not looping


I am creating a little game and I have a problem. I putted 2 gifs and when the gif finish, the image stops and don't loop. I want a way to loop this gif because out of the program it works well. This is the code:

import java.io.File;

  import javax.imageio.ImageIO;
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JLayeredPane;
  import javax.swing.WindowConstants;

  public class EjemploJLayeredPane {
    public static void main(String[] args) {

        JFrame v = new JFrame("Ejemplo de JLayeredPane");
        JLayeredPane layered = new JLayeredPane();


        JLabel fondo = new JLabel();
        ImageIcon imagen = new ImageIcon("fondo batalla.png");
        fondo.setIcon(imagen);
        fondo.setSize(imagen.getIconWidth(), imagen.getIconHeight());


        JLabel primerPlano1 = new JLabel();
        ImageIcon imagen1 = new ImageIcon("pikachufrente.gif");
        primerPlano1.setIcon(imagen1);
        primerPlano1.setBounds(275,-80,400,400);


        JLabel primerPlano2 = new JLabel();
        ImageIcon imagen2 = new ImageIcon("charmanderespalda.gif");
        primerPlano2.setIcon(imagen2);
        primerPlano2.setBounds(85,20,400,400);


        layered.add(fondo, new Integer(1));
        layered.add(primerPlano1, new Integer(2));
        layered.add(primerPlano2, new Integer(2));


        v.getContentPane().add(layered);
        v.setSize(imagen.getIconWidth(), imagen.getIconHeight() + 20);
        v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        v.setVisible(true);
    }

}

Solution

  • This works for me - no bounds no nothing:

      JFrame f=new JFrame();
      f.setSize(500, 500);
      JLabel l=new JLabel();
      ImageIcon icon=new ImageIcon("bird.gif");
      l.setIcon(icon);
      f.add(l);
      f.setVisible(true);