Good!
Look, I have a class called GIFPanel which inherits from the JPanel class
package Trivia;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class GIFPanel extends JPanel{
private Image imagen;
public GIFPanel(Image imagen){
this.imagen = imagen;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imagen,0,0,getWidth(),getHeight(),this);
}
}
In my other class I create it and insert an image whose source was from a web address
Image img = Toolkit.getDefaultToolkit().getImage(new URL("https://media.giphy.com/media/l3q2XKiWAOl0qa1Tq/source.gif"));
menu = new GIFPanel(img);
Ok, it works fine, the gif plays even with the generated JAR... but... it's not really what I'm looking for. How so? A few days ago I asked about how to load images contained within packages of the same project in JAVA that were not through URLs, since when generating the jar file, the program responds with errors.
Try to do the same with the GIF file, but not even netbeans recognizes it. . .
The assets package already contains the GIF.
I want to avoid using links from the web since when there is a connection failure where it is going to be used, the program will crash (close).
How do I make it so that it can play the gif using the file contained in the package of my project?
What should work for you is to load the file into a
byte[]
and then build the image usingcreateImage(byte[] ba)
:As you can see, you load the content of your .jar resource in the same way as in your other question . Only this time you load it into a byte array that you use to build your array image using the
Toolkit
awt one.I use this in a JLabel and then mount it to a JPanel.