¿Cómo mostrar GIF animado en la aplicación Java?Mostrar GIF animado
Respuesta
Usando oscilación simplemente podría utilizar un JLabel
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("<URL to your Animated GIF>");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
JFrame f = new JFrame("Animation");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
Enlace muy útil e interesante, gracias. – Benj
Para gifs de carga animada almacenados en un paquete fuente (en el código fuente), esto funcionó para mí:
URL url = MyClass.class.getResource("/res/images/animated.gif");
ImageIcon imageIcon = new ImageIcon(url);
JLabel label = new JLabel(imageIcon);
Esto es exactamente lo que no funciona para mí. La imagen está cargada, pero solo se muestra el primer cuadro, sin animación. – lukfi
public class aiubMain {
public static void main(String args[]) throws MalformedURLException{
//home frame = new home();
java.net.URL imgUrl2 = home.class.getResource("Campus.gif");
Icon icon = new ImageIcon(imgUrl2);
JLabel label = new JLabel(icon);
JFrame f = new JFrame("Animation");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
¡Esto funciona para mí!
public void showLoader(){
URL url = this.getClass().getResource("images/ajax-loader.gif");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
frameLoader.setUndecorated(true);
frameLoader.getContentPane().add(label);
frameLoader.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameLoader.pack();
frameLoader.setLocationRelativeTo(null);
frameLoader.setVisible(true);
}
Es bueno agregar alguna explicación sobre su código. ¿Cómo es diferente? ¿Por qué funciona? Gracias (moderador). –
Vine aquí en busca de la misma respuesta, pero según las primeras respuestas, se me ocurrió un código más fácil. Espero que esto ayude en futuras búsquedas.
Icon icon = new ImageIcon("src/path.gif");
try {
mainframe.setContentPane(new JLabel(icon));
} catch (Exception e) {
}
//Class Name
public class ClassName {
//Make it runnable
public static void main(String args[]) throws MalformedURLException{
//Get the URL
URL img = this.getClass().getResource("src/Name.gif");
//Make it to a Icon
Icon icon = new ImageIcon(img);
//Make a new JLabel that shows "icon"
JLabel Gif = new JLabel(icon);
//Make a new Window
JFrame main = new JFrame("gif");
//adds the JLabel to the Window
main.getContentPane().add(Gif);
//Shows where and how big the Window is
main.setBounds(x, y, H, W);
//set the Default Close Operation to Exit everything on Close
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Open the Window
main.setVisible(true);
}
}
quería poner el archivo .gif en una interfaz gráfica de usuario, pero muestra con otros elementos. Y el archivo .gif se tomaría del proyecto Java y no de una URL.
1 - Parte superior de la interfaz sería una lista de elementos en la que podemos elegir uno
2 - Centro sería el GIF animado
3 - Abajo se mostrará el elemento seleccionado de la lista
Aquí está mi código (necesito 2 archivos java, la primera (Interf.java) llama a la segunda (Display.java)):
1 - Interf.java
public class Interface_for {
public static void main(String[] args) {
Display Fr = new Display();
}
}
2 - Display.java
INFO: Sea Shure para crear una nueva carpeta de origen (NUEVA carpeta de origen>) en su proyecto de Java y poner el .gif interior para que sea visto como un archivo.
Obtengo el archivo gif con el siguiente código, por lo que puedo exportarlo en un proyecto jar (luego está animado).
URL url = getClass(). GetClassLoader(). GetResource ("fire.gif");
public class Display extends JFrame {
private JPanel container = new JPanel();
private JComboBox combo = new JComboBox();
private JLabel label = new JLabel("A list");
private JLabel label_2 = new JLabel ("Selection");
public Display(){
this.setTitle("Animation");
this.setSize(400, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setLayout(new BorderLayout());
combo.setPreferredSize(new Dimension(190, 20));
//We create te list of elements for the top of the GUI
String[] tab = {"Option 1","Option 2","Option 3","Option 4","Option 5"};
combo = new JComboBox(tab);
//Listener for the selected option
combo.addActionListener(new ItemAction());
//We add elements from the top of the interface
JPanel top = new JPanel();
top.add(label);
top.add(combo);
container.add(top, BorderLayout.NORTH);
//We add elements from the center of the interface
URL url = getClass().getClassLoader().getResource("fire.gif");
Icon icon = new ImageIcon(url);
JLabel center = new JLabel(icon);
container.add(center, BorderLayout.CENTER);
//We add elements from the bottom of the interface
JPanel down = new JPanel();
down.add(label_2);
container.add(down,BorderLayout.SOUTH);
this.setContentPane(container);
this.setVisible(true);
this.setResizable(false);
}
class ItemAction implements ActionListener{
public void actionPerformed(ActionEvent e){
label_2.setText("Chosen option: "+combo.getSelectedItem().toString());
}
}
}
- 1. GIF animado en NSImageView
- 2. Gif animado no animado en enviar
- 3. Silverlight 3 y gif animado
- 4. Android: Gif animado
- 5. Imagemagick optimización gif animado tamaño
- 6. Display gif animado en JPanel
- 7. Cómo usar Gif animado en forma delphi
- 8. GIF animado en canvas de HTML5
- 9. Creando un GIF animado con ImageIO?
- 10. GIF animado en superposición sobre MapView
- 11. ¿Exportar de PowerPoint a un gif animado?
- 12. Gif animado a avi en linux
- 13. Crear un .gif animado desde .jpeg/png
- 14. Generar GIF animado con canvas HTML5
- 15. cómo crear un gif animado en .net
- 16. Obteniendo la duración del marco de un GIF animado?
- 17. jQuery autocompletar: Cómo mostrar una imagen de carga de gif animado
- 18. Mostrar animación Gif en java
- 19. gif animado codificado en base64 como fondo css?
- 20. animado carga de imágenes GIF como fondo repetida
- 21. ¿Cómo generar un GIF animado de un Manipulate? 8.0.1
- 22. Reproducción de un archivo de imagen GIF animado en imageview
- 23. ¿Cómo puedo crear un gif animado en Java?
- 24. Cargando un archivo .GIF animado del archivo JAR en ImageIcon
- 25. Extraer cuadros individuales de un GIF animado a lienzo
- 26. Hacer un GIF animado con ImageMagick API de PHP
- 27. ¿Cómo uso un .gif animado en mi aplicación de iPhone?
- 28. ¿Cómo cambiar el tamaño del archivo gif animado usando C#?
- 29. Crea un gif animado usando la biblioteca de GD
- 30. GIF animado que no funciona en FireFox después del caché
Por alguna razón, si usted consigue su objeto 'ImageIcon' con algo como esto' icono Icono = new ImageIcon (ImageIO.read (getClass() getResourceAsStream ("iconasresource.gif")).); 'Su GIF no se animará –
De hecho, la creación de un ImageIcon con ImageIO.read no anima el gif por algún motivo. Quizás sea obvio, pero puede obtener la URL del recurso con: 'URL url = getClass(). GetResource ("/img.gif ");'. –
Lo estoy usando con Java8 y no está animando mi GIF ... –