Animate gifs for tackboard.Bitmap

This commit is contained in:
Autumn Lamonte 2022-01-05 21:04:21 -06:00
parent dd557079e8
commit 07d4b4be80
4 changed files with 84 additions and 4 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 763 KiB

View file

@ -32,7 +32,9 @@ import java.awt.Graphics2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -202,7 +204,12 @@ public class ImageUtils {
* @return the animation, or null on error * @return the animation, or null on error
*/ */
public static Animation getAnimation(final String filename) { 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 * @return the animation, or null on error
*/ */
public static Animation getAnimation(final File file) { 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 { try {
List<BufferedImage> frames = new LinkedList<BufferedImage>(); List<BufferedImage> frames = new LinkedList<BufferedImage>();
List<String> disposals = new LinkedList<String>(); List<String> disposals = new LinkedList<String>();
@ -230,7 +267,7 @@ public class ImageUtils {
ImageReader reader = null; ImageReader reader = null;
ImageInputStream stream; ImageInputStream stream;
stream = ImageIO.createImageInputStream(new FileInputStream(file)); stream = ImageIO.createImageInputStream(inputStream);
Iterator<ImageReader> iter = ImageIO.getImageReaders(stream); Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
while (iter.hasNext()) { while (iter.hasNext()) {
reader = iter.next(); reader = iter.next();

View file

@ -47,6 +47,8 @@ import jexer.TLabel;
import jexer.TTimer; import jexer.TTimer;
import jexer.TWidget; import jexer.TWidget;
import jexer.TWindow; import jexer.TWindow;
import jexer.bits.Animation;
import jexer.bits.ImageUtils;
import jexer.event.TCommandEvent; import jexer.event.TCommandEvent;
import jexer.layout.StretchLayoutManager; import jexer.layout.StretchLayoutManager;
import jexer.tackboard.Bitmap; import jexer.tackboard.Bitmap;
@ -102,7 +104,7 @@ public class DemoPixelsWindow extends TWindow {
public DemoPixelsWindow(final TApplication parent) { public DemoPixelsWindow(final TApplication parent) {
// Construct a demo window. X and Y don't matter because it will be // Construct a demo window. X and Y don't matter because it will be
// centered on screen. // centered on screen.
super(parent, i18n.getString("windowTitle"), 0, 0, 64, 11, super(parent, i18n.getString("windowTitle"), 0, 0, 64, 17,
CENTERED | RESIZABLE); CENTERED | RESIZABLE);
setLayoutManager(new StretchLayoutManager(getWidth() - 2, setLayoutManager(new StretchLayoutManager(getWidth() - 2,
@ -197,7 +199,10 @@ public class DemoPixelsWindow extends TWindow {
loader = Thread.currentThread().getContextClassLoader(); loader = Thread.currentThread().getContextClassLoader();
BufferedImage image; BufferedImage image;
image = ImageIO.read(loader.getResource("demo/trans_icon.png")); 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)); addOverlay(new Bitmap(11, 97, 0, image));
timer3 = getApplication().addTimer(100, true, timer3 = getApplication().addTimer(100, true,

View file

@ -30,6 +30,9 @@ package jexer.tackboard;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import jexer.TApplication;
import jexer.bits.Animation;
/** /**
* Bitmap is a raw bitmap image. * Bitmap is a raw bitmap image.
*/ */
@ -53,6 +56,11 @@ public class Bitmap extends TackboardItem {
*/ */
private BufferedImage renderedImage; private BufferedImage renderedImage;
/**
* Animation to display.
*/
private Animation animation;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Constructors ----------------------------------------------------------- // Constructors -----------------------------------------------------------
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -72,6 +80,25 @@ public class Bitmap extends TackboardItem {
this.image = image; 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 ---------------------------------------------------------- // TackboardItem ----------------------------------------------------------
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -149,6 +176,13 @@ public class Bitmap extends TackboardItem {
* @param textHeight the height of a text cell * @param textHeight the height of a text cell
*/ */
private void render(final int textWidth, final int textHeight) { 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) { if (image == null) {
renderedImage = null; renderedImage = null;
return; return;
@ -184,6 +218,10 @@ public class Bitmap extends TackboardItem {
* @param image the new image * @param image the new image
*/ */
public void setImage(final BufferedImage image) { public void setImage(final BufferedImage image) {
if (animation != null) {
animation.stop();
animation = null;
}
this.image = image; this.image = image;
setDirty(); setDirty();
} }