diff --git a/resources/demo/butterfly.gif b/resources/demo/butterfly.gif new file mode 100644 index 0000000..a13dfe2 Binary files /dev/null and b/resources/demo/butterfly.gif differ diff --git a/src/jexer/bits/ImageUtils.java b/src/jexer/bits/ImageUtils.java index 0741fb3..43a8f49 100644 --- a/src/jexer/bits/ImageUtils.java +++ b/src/jexer/bits/ImageUtils.java @@ -32,7 +32,9 @@ import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; +import java.io.InputStream; import java.io.IOException; +import java.net.URL; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -202,7 +204,12 @@ public class ImageUtils { * @return the animation, or null on error */ public static Animation getAnimation(final String filename) { - return getAnimation(new File(filename)); + try { + return getAnimation(new FileInputStream(filename)); + } catch (IOException e) { + // SQUASH + return null; + } } /** @@ -212,6 +219,36 @@ public class ImageUtils { * @return the animation, or null on error */ public static Animation getAnimation(final File file) { + try { + return getAnimation(new FileInputStream(file)); + } catch (IOException e) { + // SQUASH + return null; + } + } + + /** + * Open an image as an Animation. + * + * @param url the URK that contains an animation + * @return the animation, or null on error + */ + public static Animation getAnimation(final URL url) { + try { + return getAnimation(url.openStream()); + } catch (IOException e) { + // SQUASH + return null; + } + } + + /** + * Open an image as an Animation. + * + * @param inputStream the inputStream that contains an animation + * @return the animation, or null on error + */ + public static Animation getAnimation(final InputStream inputStream) { try { List frames = new LinkedList(); List disposals = new LinkedList(); @@ -230,7 +267,7 @@ public class ImageUtils { ImageReader reader = null; ImageInputStream stream; - stream = ImageIO.createImageInputStream(new FileInputStream(file)); + stream = ImageIO.createImageInputStream(inputStream); Iterator iter = ImageIO.getImageReaders(stream); while (iter.hasNext()) { reader = iter.next(); diff --git a/src/jexer/demos/DemoPixelsWindow.java b/src/jexer/demos/DemoPixelsWindow.java index 49ebd01..f00bfcd 100644 --- a/src/jexer/demos/DemoPixelsWindow.java +++ b/src/jexer/demos/DemoPixelsWindow.java @@ -47,6 +47,8 @@ import jexer.TLabel; import jexer.TTimer; import jexer.TWidget; import jexer.TWindow; +import jexer.bits.Animation; +import jexer.bits.ImageUtils; import jexer.event.TCommandEvent; import jexer.layout.StretchLayoutManager; import jexer.tackboard.Bitmap; @@ -102,7 +104,7 @@ public class DemoPixelsWindow extends TWindow { public DemoPixelsWindow(final TApplication parent) { // Construct a demo window. X and Y don't matter because it will be // centered on screen. - super(parent, i18n.getString("windowTitle"), 0, 0, 64, 11, + super(parent, i18n.getString("windowTitle"), 0, 0, 64, 17, CENTERED | RESIZABLE); setLayoutManager(new StretchLayoutManager(getWidth() - 2, @@ -197,7 +199,10 @@ public class DemoPixelsWindow extends TWindow { loader = Thread.currentThread().getContextClassLoader(); BufferedImage image; image = ImageIO.read(loader.getResource("demo/trans_icon.png")); - addUnderlay(new Bitmap(17, 33, 0, image)); + Animation animation; + animation = ImageUtils.getAnimation(loader.getResource( + "demo/butterfly.gif")); + addUnderlay(new Bitmap(17, 33, 0, animation, getApplication())); addOverlay(new Bitmap(11, 97, 0, image)); timer3 = getApplication().addTimer(100, true, diff --git a/src/jexer/tackboard/Bitmap.java b/src/jexer/tackboard/Bitmap.java index 20c6910..16529aa 100644 --- a/src/jexer/tackboard/Bitmap.java +++ b/src/jexer/tackboard/Bitmap.java @@ -30,6 +30,9 @@ package jexer.tackboard; import java.awt.image.BufferedImage; +import jexer.TApplication; +import jexer.bits.Animation; + /** * Bitmap is a raw bitmap image. */ @@ -53,6 +56,11 @@ public class Bitmap extends TackboardItem { */ private BufferedImage renderedImage; + /** + * Animation to display. + */ + private Animation animation; + // ------------------------------------------------------------------------ // Constructors ----------------------------------------------------------- // ------------------------------------------------------------------------ @@ -72,6 +80,25 @@ public class Bitmap extends TackboardItem { this.image = image; } + /** + * Public constructor. Due to the use of a timer, the Bitmap needs to + * see TApplication to start the Animation. + * + * @param x X pixel coordinate + * @param y Y pixel coordinate + * @param z Z coordinate + * @param animation the animation to display + * @param application the application to set the animation timer on + */ + public Bitmap(final int x, final int y, final int z, + final Animation animation, final TApplication application) { + + super(x, y, z); + this.animation = animation; + image = animation.getFrame(); + animation.start(application); + } + // ------------------------------------------------------------------------ // TackboardItem ---------------------------------------------------------- // ------------------------------------------------------------------------ @@ -149,6 +176,13 @@ public class Bitmap extends TackboardItem { * @param textHeight the height of a text cell */ private void render(final int textWidth, final int textHeight) { + if (animation != null) { + BufferedImage newFrame = animation.getFrame(); + if (newFrame != image) { + image = newFrame; + renderedImage = null; + } + } if (image == null) { renderedImage = null; return; @@ -184,6 +218,10 @@ public class Bitmap extends TackboardItem { * @param image the new image */ public void setImage(final BufferedImage image) { + if (animation != null) { + animation.stop(); + animation = null; + } this.image = image; setDirty(); }