Link to home
Start Free TrialLog in
Avatar of hilleret
hilleret

asked on

How to display an animated gif in an applet ?

Hi,

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

Thank tou for your help

Renaud
Avatar of pyramid
pyramid

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);

   }
}
Avatar of hilleret

ASKER

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
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.
Could you give me a sample coding of your solution ?

Renaud


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...
ASKER CERTIFIED SOLUTION
Avatar of russgold
russgold

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial