fix the showing gif in Java swing [duplicate] - java

This question already has answers here:
Show an animated BG in Swing
(3 answers)
Closed 5 years ago.
I want to show a gif in java swing.
For show gif I use from these code but in both of them the gif is not moving and it like as an image is static.
first code:
void showGif() {
try {
JPanel panel = new JPanel();
BufferedImage bufferedImage = ImageIO.read(new File("address of gif");
Icon icon = new ImageIcon(bufferedImage);
JLabel label = new JLabel(icon);
label.setVisible(true);
panel.add(label);
} catch (IOException e) {
e.printStackTrace();
}
}
second code:
void showGif() {
try {
ImageIcon imageIcon;
BufferedImage bufferedImage = ImageIO.read(new File("address of gif"));
imageIcon = new ImageIcon(bufferedImage);
JLabel label = new JLabel(imageIcon);
JPanel panel = new JPanel();
label.setVisible(true);
panel.add(label, TOP_ALIGNMENT);
} catch (IOException e) {
e.printStackTrace();
}
}
please help me to show gif correctly.

You ImageIcon in this case will be showing just the first frame of the GIF.
You can add the GIF to a JLabel, which you can add to the JPanel. Check this out for further help.

Related

Gif no long animated after replacing with JLabel's setIcon method

I am still very new to Java and programming in general. I am trying to display a GIF using Swing and the following code:
JPanel contentPane;
JLabel imageLabel = new JLabel();
public FrostyCanvas(String imageName) {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane = (JPanel) getContentPane();
contentPane.setLayout(new BorderLayout());
setSize(new Dimension(1600, 900));
setTitle("FrostySpirit v1.1.1 (Beta) - FrostyCanvas");
// add the image label
ImageIcon ii = new ImageIcon(this.getClass().getResource(imageName));
imageLabel.setIcon(ii);
contentPane.add(imageLabel, java.awt.BorderLayout.CENTER);
// display target GIF
this.setLocationRelativeTo(null);
this.setVisible(true);
} catch (Exception exception) {
exception.printStackTrace();
}
}
The method call is the following: (in a different class)
FrostyCanvas FC = new FrostyCanvas("old.gif");
The GIF is animated at this point. I then try to replace the GIF with another one using the following method: (in the same class as ForstyCanvas(String imageName))
public void changeGif(String imageName) throws IOException
{
Icon icon; File file;
file = new File(imageName);
icon = new ImageIcon(ImageIO.read(file));
imageLabel.setIcon(icon);
}
I call the method above in a different class using the following code:
FC.changeGif("D:\\new.gif")
The image is successfully replaced. However, now it only shows the very first frame of new.gif and is no longer animated. How can I make the new GIF move?
The way ImageIO reads the image is creating a static image. The thing to do is to use ImageIcon's loader.
ImageIcon replacement = new ImageIcon(file.toURI().toURL());
That way you are using the same method to construct the image icon as you do with the resource / url.

Not work in auto generated but work when I manual coding?

This is so frustrated. The code only work on manual generated but not work in auto generate ???
There is an image in project "Bird.png".
public ComboBox() {
initComponents();
try {
image = ImageIO.read(new File("Bird.png"));
lblShow = new JLabel(new ImageIcon(image.getScaledInstance(300, 300, Image.SCALE_SMOOTH)));
} catch (Exception e) {
}
}
This is my manual generate code :
public MainFrame(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600,400);
//create new panel and add panel to frame
JPanel pnlImg=new JPanel();
add(pnlImg);
//create new label for showing image
JLabel lblShowImg;
BufferedImage image = null;
try {
image = ImageIO.read(new File("1.jpg"));
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
ImageIcon ii=new ImageIcon(image.getScaledInstance(300,300,Image.SCALE_SMOOTH));
lblShowImg=new JLabel(ii);
/*add label to panel */
pnlImg.add(lblShowImg);
/* show frame*/
setVisible(true);
}
Why is this not working?
public ComboBox() {
initComponents();
try {
image = ImageIO.read(new File("Bird.png"));
lblShow = new JLabel(new ImageIcon(image.getScaledInstance(300, 300, Image.SCALE_SMOOTH)));
} catch (Exception e) {
}
}
Likely due to your not taking layout managers into account, but the truth is, as written it is impossible to say
You never show where you add the JLabel to any container
You don't indicate what layout manager the container is using
You haven't posted a valid mcve.
To possibly solve this --
allow NetBeans to generate a JPanel
Add your JLabel to your own JPanel, one whose layout you control.
Add both to a JFrame (respecting its contentPane's layout) that you display.

Java Mouse Event Listerner in Image [duplicate]

This question already has answers here:
Adding a mouse listener to image or image Icon in java
(3 answers)
Closed 6 years ago.
is it possible to add mouse listener to an image for game development.
Or to any class which puts an image to a JPanel.
you could use an JButton with an BufferdImage for this, there you have the Standard listener for a JButton to work with.
Sample:
JButton button;
BufferedImage buttonIcon;
JFrame frame = new JFrame();
try {
buttonIcon = ImageIO.read(new File("path")); //path of image
button = new JButton(new ImageIcon(buttonIcon));
} catch (IOException e) {
button = new JButton(); //couldn't load Image
}
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);

String and BufferedImage on JFrame

I am trying to display a string and a BufferedImage onto a JFrame that both come as output from a method. I am not able to separate the String from the image, therefore I need to add both to the JFrame.
Here is the code I have do far and nothing is displaying. Thank you very much for your help in advance.
String path = getUserInfo("abc123"); <-- method that returns a string and a buffered image
BufferedImage image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException ex) {
Logger.getLogger(InstagramClient.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame f = new JFrame();
f.setSize(400,400);
f.setVisible(true);
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "icon", -1);
You can render the string over the image using drawString(), as shown here.
Alternatively, you can use label.setText() and rely on the label's horizontal & vertical alignment for positioning, as illustrated here.
Basically, you can use the label's setText method to supply a text value for the label.
You also need to "add" the label to the frame, or it won't display anything.
String path = getUserInfo("abc123"); <-- method that returns a string and a buffered image
BufferedImage image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException ex) {
Logger.getLogger(InstagramClient.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame f = new JFrame();
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel("This is some text", icon, JLabel.CENTER);
f.add(label);
f.setVisible(true);
Hovercraft Full Of Eels actually answered my question. I wanted to return both a String and BufferedImage but now I know that is not possible. Thank you all for your help :)

Loading animated GIF in JLabel weirdness

I'm trying to load an animated GIF in a JLabel.
While this works:
URL urlsd;
try {
urlsd = new URL("http://pscode.org/media/starzoom-thumb.gif");
ImageIcon imageIcon = new ImageIcon(urlsd);
JLabel progress = new JLabel(imageIcon);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
This, on the other hand, does not, and I don't want to get the GIF from an URL, since I already have the GIF. Result of loading this shows only the first frame of the GIF:
try {
ImageIcon imageIcon = new ImageIcon(ImageIO.read(ClassLoader.getSystemResourceAsStream("res/images/progress_indicator.gif")));
JLabel progress = new JLabel(imageIcon);
imageIcon.setImageObserver(progress);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
I guess there must be a reason for this, but I cannot find it.
Thanks!
Alex
You can try loading your GIF file like that:
public class Test extends JPanel {
public Test() {
ImageIcon imageIcon =
new ImageIcon(Test.this.getClass().getResource("starzoom-thumb.gif"));
}
}
Or using Test.class.getResouce() if your context is static.
Below code worked for me, it will show the animation instead of first frame of the image.
public class Test extends JPanel {
public Test() {
ImageIcon yyyyIcon = new ImageIcon(xxx.class.getClassLoader().getResource("yyyy.gif"));
connectionLabel.setIcon(yyyy);
}
}
So there is also a simpler way of doing it. The following line is how I did it.
this.setContentPane(new JLabel(new ImageIcon("Path To Gif File")));

Categories

Resources