2

I'm writing a method for the Java Helper Library to show a "windowless" swing component (image) which can be used as a way to show a progress wheel or something. I asked how to do this a while ago and received a good answer which I'm using. It works great except the animated gif is not animated. (I'm not using the image itself because seeing it the whole time you're reading this might make you sick...) It's not animated as in it's not moving. It's seemingly paused or something. The answer on the other question said animated gifs would work fine. Is the answer-er wrong or am I implementing this wrong?:

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;
}
Community
  • 1
  • 1
kentcdodds
  • 27,113
  • 32
  • 108
  • 187
  • 2
    That code does not copy / paste / **compile.** It is **not** an SSCCE. For the image, try hot-linking to http://pscode.org/media/starzoom-thumb.gif or (smaller) http://1point1c.org/gif/thum/plnttm.gif – Andrew Thompson May 20 '12 at 21:12
  • I would start by only using Swing components in the event dispatch thread, and see if it changes something. http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html – JB Nizet May 20 '12 at 21:13
  • @AndrewThompson, I'm normally pretty good about posting a real SSCCE. I'll remove that part about the SSCCE. I would include it, but I've discovered you're right. That method is the problem. After getting the image from the url it works great. If you'd like to post your answer, I'll accept it. Thanks for looking into it. – kentcdodds May 20 '12 at 21:20

1 Answers1

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

My best guess short of an SSCCE, is that the helper method returns a static (resized) version of the original image.

Note that by using HTML in the label, an animated GIF can be resized 'on-the-fly', however the effect is less than optimal. It is better to design the image the right size.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433