java


synchronize Swing Timer with gif Image


I have a gif Image being displayed on a JPanel in an endless Loop. Now I need to stop the animation after a random amount of Frames. In fact, I generate a random number that can be 0 or 1. Say the gif consists of 6 Frames. If the number is 0 I want to stop at the 3rd Frame, if it is 1 the animation should freeze at the 6th Frame.
To realize this I tried to use a Swing Timer which fires Events exactly when the next Frame comes. So if the Frames have a delay of 50 ms, I construct the Timer like
new Timer(50, this);
Sadly, this doesn't seem to work, in fact the Animation seems to be slower than the Timer. (I assume this has something to do with loading Times.) Anyway, i added some Code illustrating the Problem and (faily) Solution approach.
import java.awt.event.*;
import javax.swing.*;
public class GifTest extends JPanel implements ActionListener{
ImageIcon gif = new ImageIcon(GifTest.class.getResource("testgif.gif"));
JLabel label = new JLabel(gif);
Timer timer = new Timer(50, this);
int ctr;
public GifTest() {
add(label);
timer.setInitialDelay(0);
timer.start();
}
#Override
public void actionPerformed(ActionEvent e) {
ctr++;
if (ctr == 13){
timer.stop();
try {
Thread.sleep(1000);
} catch (InterruptedException i) {
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Gif Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GifTest());
frame.setSize(150,150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
For the giftest.gif, it is a simple 6 Layers with the Numbers 1 to 6 on them, saved with a delay of 50ms.
I would be grateful for any help.
Ps: If it turns out that there is no elegant way to do this, it would also suffice to retrieve the Frame currently displayed. That way I could ask for it and stop when it's the 3rd (resp. 6th) Frame. Due to the task's context i would prefer a modified version of my solution though.
You can unpack as mentioned above and store in an array of images (simple and straightforward).
You could also use a more advanced option using ImageObserver Interface:
ImageObserver provides monitoring for the loading process via a special API called:
imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
You can track progress with this API as follows:
ImageIcon gif = new ImageIcon();
JLabel label = new JLabel(gif);
ImageObserver myObserver = new ImageObserver() {
public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
if ((flags & HEIGHT) != 0)
System.out.println("Image height = " + height);
if ((flags & WIDTH) != 0)
System.out.println("Image width = " + width);
if ((flags & FRAMEBITS) != 0)
System.out.println("Another frame finished.");
if ((flags & SOMEBITS) != 0)
System.out.println("Image section :" + new Rectangle(x, y, width, height));
if ((flags & ALLBITS) != 0)
System.out.println("Image finished!");
if ((flags & ABORT) != 0)
System.out.println("Image load aborted...");
label.repaint();
return true;
}
};
gif.setImageObserver( myObserver );
gif.setImage(GifTest.class.getResource("testgif.gif"));
You can stop the loading process using return false;
UPDATE: (using ImageReader)
ImageObserver is not so intuitive to work with.
It will update each time a repaint is required and a full animation sequence will be triggered.
Although you can stop it as some point, it will execute from the first image each time.
Another solution is to use ImageReader:
ImageReader can unpack a GIF to a sequence of BufferedImages.
You can then control the entire sequence with a Timer as you wish.
String gifFilename = "testgif.gif";
URL url = getClass().getResource(gifFilename);
ImageInputStream iis = new FileImageInputStream(new File(url.toURI()));
ImageReader reader = ImageIO.getImageReadersByFormatName("GIF").next();
// (reader is actually a GIFImageReader plugin)
reader.setInput(iis);
int total = reader.getNumImages(true);
System.out.println("Total images: "+total);
BufferedImage[] imgs = new BufferedImage[total];
for (int i = 0; i < total; i++) {
imgs[i] = reader.read(i);
Icon icon = new ImageIcon(imgs[i]);
// JLabel l = new JLabel(icon));
}

Related Links

Report all long common subseqence(LCS) and its responsive location in the two strings
in eclipse plug-in, programmatically save new .java files to an existing jar file in the workspace
What is a programmers reference book? [closed]
How to dynamically generate Labels in jsf2.0 based on the data in the backing bean?
The difference between && and || [duplicate]
Java Textbook: “the size of an array must be known at compile time”
Why only String[] args in java instead of Object[] args?
I'm not sure how to add spaces to the small triangles to make it into a larger triangle
Credit Card validator for java
Unable to cancel PendingIntent from AlarmManager
How to download csv file without writing it using OpenCsv
java.lang.ClassCastException: Creating a synchronized Linked List
Your method must return a value. If your method has multiple paths of execution
Java Add Object to ArrayList error
Using Java 8, what is the most concise way of creating a sorted AND grouped list of Strings
UPDATED: Reading text file into a byte array

Categories

HOME
magnific-popup
reflection
path-finding
bpmn
razor
amazon-ecs
baqend
blueprintjs
android-youtube-api
vault
circular-dependency
phaser
visual-studio-cordova
iron-router
clearcase-ucm
telephony
pythonanywhere
code-review
swiftlint
autoconf
csrf-protection
mustache.php
autosys
cas
facebook-access-token
phpfox
strncpy
maquette
publish
vision
procdump
sql-server-2012-express
bcd
pdb
jspresso
webtest
event-driven
temporary-files
google-closure
spring-security-kerberos
dds
rails-routing
typescript1.8
veracode
fancybox-2
jxcore
lift-json
pg-dump
libusb-win32
capacity
color-profile
redundancy
sonarlint-vs
cubes
visual-c++-2008
android-textview
dstu2-fhir
moveit
master-slave
rvest
pagerank
phpcas
thredds
wyam
internet-connection
onactivityresult
openexr
p4java
yorick
valueconverter
yui-compressor
browser-detection
hosts-file
regsvr32
eclipse-templates
heartbeat
psi
h.323
multi-tier
data-driven
defensive-programming
multiple-languages

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App