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

redirecting System.out/System.err to log4j for some jars only
Is it possible make all JFrames the internal program uses into JInternalFrames and place them in a JDesktopPane?
Checking for a time-out in a JUnit test case
OutOfBoundsException for TicTacToe Game; Issue: arrays?
Final Year project Proxy Blocker Guidance
DAO vs ORM(hibernate) pattern [closed]
Convert a byte array to base64 for blackberry using Eclipse
Java GC Stop and copy
Use Ant for running program with command line arguments
C3P0 apparent deadlock when the threads are all empty?
Using Esper, how can I perform dynamic filtering of events?
Any problem with doing the main work of a class in its constructor?
how to play wav file in java 1.4
J2ME Label Problem
nutch crawler relative urls problem
java - invoking standard deserialization inside custom deserialization

Categories

HOME
sendgrid
pdf
client
fluentd
hp-exstream
lodash
alfresco
sql-server-2016
python-unittest
google-translate
rascal
messages
reactive-cocoa
grails3
tokenize
php-openssl
angular2-aot
cas
stormpath
usbserial
force-layout
mapdb
fusionpbx
ios5
libraries
reportbuilder
jna
greenrobot-eventbus
elmah
eigenvalue
segment
revapi
paxos
aurelia-validation
elgg
jxcore
worker-thread
crypt
libusb-win32
ifs
pearson
mcafee
blacklist
chord-diagram
jquery-filter
mathematica-frontend
markojs
bgp
design-by-contract
ios4
kendonumerictextbox
google-places
responsive-images
graph-api-explorer
jsapi
umbraco6
nsmutabledictionary
file-copying
googlemock
castle
elliptic-curve
xceed-datagrid
picturefill
concurrent-collections
meteor-velocity
message-driven-bean
typo3-neos
mcts
easy-install
flash-builder4.5
batterylevel
eventual-consistency
kyotocabinet
angularjs-controller
pvrtc
quickdialog
broken-links
chrono
ohm
cufon
netdna-api
gdlib
mysql-error-1005
digest-authentication
sortable-tables
mozilla-prism
zend-tool
memory-size
kdbg
photoshop-cs4

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