Resized animated gif ImageIcon not working properly with JButton etc.

The problem is that when I resize an ImageIcon representing an animated gif and place it on a Jbutton, JToggelButton or JLabel, in some cases the image does not show or does not animate. More precicely, depending on the specific image file, the image shows always, most of the time or sometimes. Images which are susceptible to not showing often do not animate when showing. Moving over or clicking with the mouse on the AbstractButton instance while the frame is supposed to updated causes the image to disappear (even when viewing the non-animating image that sometimes appears). No errors are thrown.

Here some example code: (compiled with Java 6.0 compliance)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass Test

{

publicstaticvoid main(String[] args)

{

new Test();

}

staticfinalint IMAGES= 3;

JButton[]buttons=new JButton[IMAGES];

JButton toggleButton =new JButton("Toggle scaling");

booleandoScale=true;

public Test()

{

JFrame f =new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel p =new JPanel(new GridLayout(1, IMAGES));

for (int i = 0; i < IMAGES; i++)

p.add(this.buttons[i] =new JButton());

f.add(p, BorderLayout.CENTER);

f.add(this.toggleButton, BorderLayout.SOUTH);

this.toggleButton.addActionListener(new ActionListener(){

@Override

publicvoid actionPerformed(ActionEvent e)

{

Test.this.refresh();

}

});

f.setSize(600, 300);

f.setVisible(true);

this.refresh();

}

publicvoid refresh()

{

this.doScale = !this.doScale;

for (int i = 0; i < IMAGES; i++)

{

ImageIcon image =new ImageIcon(i +".gif");

if (this.doScale)

image =new ImageIcon(image.getImage().getScaledInstance(180, 180, Image.SCALE_AREA_AVERAGING));

image.setImageObserver(this.buttons[i]);

this.buttons[i].setIcon(image);

this.buttons[i].setSelectedIcon(image);

this.buttons[i].setDisabledIcon(image);

this.buttons[i].setDisabledSelectedIcon(image);

this.buttons[i].setRolloverIcon(image);

this.buttons[i].setRolloverSelectedIcon(image);

this.buttons[i].setPressedIcon(image);

}

}

}

Download the gif images here:

http://djmadz.com/zombie/0.gif

http://djmadz.com/zombie/1.gif

http://djmadz.com/zombie/2.gif

When you press the "Toggle scaling"button it switches between unresized (properly working) and resized instances of three of my gif images. Notice that the left image almost never appears, the middle image always, and the right image most of the time. The right image seems to (almost) never animate. When you click on the left image (when visble) it disappears only when the backend is updating the animation (between those two frames with a long delay)

Why are the original ImageIcon and the resized ImageIcon behaving differently? Are they differently organized internally?

Is there any chance my way of resizing might be wrong?

[4685 byte] By [Zom-Ba] at [2007-11-27 5:09:16]
# 1
Try SCALE_REPLICATE ... it fixed it for me.
abillconsla at 2007-7-12 10:28:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hellooooooooooooo out there ... you get this? Did it help?
abillconsla at 2007-7-12 10:28:56 > top of Java-index,Desktop,Core GUI APIs...
# 3

It does work, however I refuse to use SCALE_REPLICATE for my application because resizing images is butt ugly, whether scaling up or down. Could there be a clue in the rescaling implementations, which I can override?

Maybe is there a way that I can check if an image is a multi-frame animation and set the scaling algorithm accordingly?

Message was edited by:

Zom-B

Zom-Ba at 2007-7-12 10:28:56 > top of Java-index,Desktop,Core GUI APIs...
# 4
You totally lost me. You said that the pblm was that images were not showing when resizing. What I told you to do fixes this. Now you state that you refuse to resize it. You can't resize and not resize at the same time.
abillconsla at 2007-7-12 10:28:56 > top of Java-index,Desktop,Core GUI APIs...
# 5
I think I wasn't clear enough. I don't want to resize with SCALE_REPLICATE but resizing with SCALE_AREA_AVERAGING produces nice results so I want to use that.
Zom-Ba at 2007-7-12 10:28:56 > top of Java-index,Desktop,Core GUI APIs...
# 6
> I think I wasn't clear enough. I don't want to resize> with SCALE_REPLICATE but resizing with> SCALE_AREA_AVERAGING produces nice results so I want> to use that.Not so nice in all the cases, apparently...
kirillga at 2007-7-12 10:28:56 > top of Java-index,Desktop,Core GUI APIs...