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.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<BufferedImage> frames = new LinkedList<BufferedImage>();
List<String> disposals = new LinkedList<String>();
@ -230,7 +267,7 @@ public class ImageUtils {
ImageReader reader = null;
ImageInputStream stream;
stream = ImageIO.createImageInputStream(new FileInputStream(file));
stream = ImageIO.createImageInputStream(inputStream);
Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
while (iter.hasNext()) {
reader = iter.next();

View file

@ -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,

View file

@ -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();
}