Given a list of true-color full frames in BufferedImage
and a list of frame durations, how can I create an Image losslessly, that when put on a JLabel, will animate?
From what I can find, I could create an ImageWriter
wrapping a ByteArrayOutputStream
, write IIOImage
frames to it, then Toolkit.getDefaultToolkit().createImage
the stream into a ToolkitImage
.
There are two problems with this attempt.
- ImageWriter can only be instantiated with one of the known image encoders, and there is none for a lossless true-color animated image format (e.g. MNG),
- It encodes (compresses) the image, then decompresses it again, becoming an unnecessary performance hazard.
[Edit]
Some more concise constraints and requirements. Please don't come up with anything that bends these rules.
What I don't want:
- Making an animation thread and painting/updating each frame of the animation myself,
- Using any kind of 3rd party library,
- Borrowing any external process, for example a web browser,
- Display it in some kind of video player object or 3D-accelerated scene (OpenGL/etc),
- Work directly with classes from the
sun.*
packages
What I do want:
- Frame size can be as large as monitor size. Please don't worry about performance. I'll worry about that. You'll just worry about correctness.
- Frames all have the same size,
- an
Image
subclass. I should be able to draw the image likeg.drawImage(ani, 0, 0, this)
and it would animate, or wrap it in anImageIcon
and display it on a JLabel/JButton/etc and it would animate, - Each frame can each have a different delay, from 10ms up to a second,
- Animation can loop or can end, and this is defined once per animation (just like GIF),
- I can use anything packaged with Oracle Java 8 (e.g. JavaFX),
- Whatever happens, it should integrate with SWING
Optional:
- Frames can have transparency. If needed, I can opaquify my images beforehand as the animation will be shown on a known background (single color) anyway.
- I don't care if I have to subclass
Image
myself and add an animation thread in there that will cooperate with theImageObserver
, or write my ownInputStreamImageSource
, but I don't know how. - If I can somehow display a JavaFX scene with some HTML and CSS code that animates my images, then that's fine too. BUT as long as it's all encapsulated in a single SWING-compatible object that I can pass around.