Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to display an animated gif in an applet ?

Posted on 1997-12-10
6
392 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

791 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