In the overridden function for my 开发者_Go百科JFrame
:
@Override
protected void paintComponent(Graphics g) {
BufferedImage imagePerson;
try {
imagePerson = ImageIO.read(new File("errol.gif"));
} catch (IOException e) {
imagePerson = null;
}
g.drawImage(imagePerson, i * increment, j * increment - 1, null);
}
How can I change this so the animation on the gif is shown (without using threading). I have spent many hours trying to get this to work but to no avail.
You could use an ImageIcon
for this purpose. Have a look here and here. If you need the animation on a JPanel
, simply add a JLabel
(with the ImageIcon
) to the panel.
Check this example for displaying an animated gif in Swing. you dont have to use any threads:
Icon imgIcon = new ImageIcon(this.getClass().getResource("ajax-loader.gif"));
JLabel label = new JLabel(imgIcon);
label.setBounds(668, 43, 46, 14); // for example, you can use your own values
frame.getContentPane().add(label);
Source
精彩评论