Solved

How to display an animated gif in an applet ?

Posted on 1997-12-10
6
387 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now