Solved

How to display an animated gif in an applet ?

Posted on 1997-12-10
6
390 Views
Last Modified: 2012-06-21
Hi,

I'd like to know how to display an animated GIF image in an applet window.

Thank tou for your help

Renaud
0
Comment
Question by:hilleret
6 Comments
 
LVL 1

Expert Comment

by:pyramid
ID: 1231554
Hi;

I am assuming that you have a number of gif files that together create the appearance of movement. Let's say you have 10 separate gif files named file1.gif through file10.gif. You have to create an array that the program can access and run the gifs in  an applet. There is alot of code to understand and I tried to explain it in this small space, hope it helps you

regards....

Pyramid

here is some sample code;

//import all your awt libraries
import java.awt.*

//create a class that implements the runnable extention

public class [class name here] extends java.applet.Applet implement runnable {
//create your array
//you have 10 files, an array uses numbers 0 through 9
Image filepics[ ]=new Image[9];
//sets a variable for the image to reside in
Image currentimg;
//need a thread variable for the image to start and stop
Thread runner;
//I believe setting a position is optional
int xpos=#;
int ypos=#;
//initialize the array
public void init( ) {
//get the gif files
String filesrc[ ] = {"file1.gif",file2.gif".......file10.gif}
//interate the files located in a image directory or any location //you like
for (int=0; i<filepics.length; i++) {
filepics[1] = getImage(getCodeBase(), "images/" + filesrc[i]);
    }

}

//this is a must and is exactly the code you need here

public void start( ) {
if (runner==null) {
runner=new Thread(this);
runner.start( );
  }
}
public void stop( ) {
if (runner !=null) {
runner.stop( );
runner = null;
  }
}

//this section will run the files for you

public void run( ) {
//write this out for the number of files you have and advance //the array[1] through [10]
currentimg = filepics[1];
repaint( );
//this pause is in second 1000 is one second and can be //adjusted
pause(1000);

//this is a must have for parsing time intervals

void pause(int time) {
try {Thread.sleep(time); }
catch (InterruptedException e) { }
}
//paint to the template
public void paint(Graphics g) {
g.drawImage(currentimg, xpos, ypos, this);

   }
}
0
 
LVL 2

Author Comment

by:hilleret
ID: 1231555
Thank you for your answer, and

SORRY because I do not have separate files !

I have a GIF file that, when opended in a browser, automatically displays an animation.

For example : http://www.micronet.fr/~hilleret/alert.gif

Thank you for your help

Renaud
0
 
LVL 4

Expert Comment

by:russgold
ID: 1231556
Animated GIFs do not run automatically under Java; however, it turns out that every time you repaint an animated GIF, it will display the next frame, so you can control the animation rate by delegating a thread to repaint it continually at the desired rate.

We have gotten this to work in the AppletViewer on Windows NT.  I don't know if it is portable.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Author Comment

by:hilleret
ID: 1231557
Could you give me a sample coding of your solution ?

Renaud


0
 
LVL 5

Expert Comment

by:fontaine
ID: 1231558
Renaud, russgold's answer was OK. You were not obliged to reject his answer to continue the discussion. Even if he doesn't want to share his code, I thinck he can repost his answer...
0
 
LVL 4

Accepted Solution

by:
russgold earned 50 total points
ID: 1231559
reposting:
Animated GIFs do not run automatically under Java; however, it turns out that every time you repaint an animated GIF, it will display the next frame, so you can control the animation rate by delegating a thread to repaint it continually at the desired rate.

We have gotten this to work in the AppletViewer on Windows NT. I don't know if it is portable.

I don't know if I have any code that I can share (it's part of a proprietary system), but I will look.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 52
Java SE 8u111  Lot of stuff broke 11 69
table example 4 29
Java Restore security prompts not working 10 12
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question