为什么这个gif没有动画?

时间:2012-05-20 21:02:30

标签: java swing animation

我正在为Java Helper Library编写一个方法来显示一个“无窗口”的摇摆组件(图像),它可以用来显示进度轮或其他东西。我asked how to do this前一段时间我收到了一个很好的答案,我正在使用它。它的效果很好,除了 the animated gif没有动画。(我没有使用图像本身,因为在你读这篇文章时看到它可能会让你生病......)这不是动画,因为它不动。它似乎暂停了什么。关于另一个问题的答案说GIF动画可以正常工作。答案是错的还是我实施了这个错误?:

public static void main(String[] args) throws IOException, InterruptedException {
  Image image = SwingHelper.resizeImageFromResourceBy(TestClass.class, progressImageLocation, 32, true); //This just gets the image in the right size I want it.
  JWindow window = SwingHelper.getProgressWheelWindow(new ImageIcon(image), .9f, 600, 400);
  window.setVisible(true);
  Thread.sleep(3000); //Just so the image only shows up for a short period of time.
  window.setVisible(false);
  SwingHelper.centerAndPack(window); //A method to center and pack the window
}

/**
* Returns a window with a partially opaque progress Icon
*
* @param icon the icon to set in the progress window
* @param opacity a float value from 0-1
* @param x the x location of the window
* @param y the y location of the window
* @return a jWindow of the progress wheel
*/
public static JWindow getProgressWheelWindow(final Icon icon, final Float opacity, final int x, final int y) {
  JWindow jWindow = new JWindow() {

    {
      setOpacity(opacity);
      setLocation(x, y);
      setSize(icon.getIconWidth(), icon.getIconHeight());
      add(new JLabel(icon));
      pack();
    }
  };

  return jWindow;
}

1 个答案:

答案 0 :(得分:2)

SwingHelper.resizeImageFromResourceBy(
    TestClass.class, progressImageLocation, 32, true); 
    //This just gets the image in the right size I want it. 

我最好的猜测是SSCCE,是辅助方法返回原始图像的静态(已调整大小)版本。

请注意,通过在标签中使用HTML,可以“动态”调整动画GIF 的大小,但效果不是最佳。最好将图像设计得合适。