C
Christina Lumineau
Hello, I have wrote a code for animate images with an applet, but it
doesn't run : it seems to load images indefinitely. Here is the code :
If someone could help me, i would be very very happy!!!
Thanks!
Christina Lumineau
import java.awt.*;
import javax.swing.*;
/**
* Class Animation - write a description of the class here
*
* @author (your name)
* @version (a version number)
*/
public class Animation extends JApplet implements Runnable {
Thread mAnimation = null;
//Animation support
private Graphics mGraphics;//utilisé pour le contexte graphique
private Image mImages[];//tableau d'images
private int nCurrImage;//index de l'image courante
private int nImgWidth = 0;//width and...
private int nImgHeight = 0;//height of images
private boolean fAllLoaded = false;//true : images chargées
private int mFps = 10;
private String imageFile = "bonhomme";
private int numImages = 5;
private int nSleepTime;//temps de mise en sommeil(fps)
private final String PARAMfps = "fps";
private final String PARAMimageFile = "imageFile";
private final String PARAMnumImages = "numImages";
/**
* Called by the browser or applet viewer to inform this JApplet that it
* has been loaded into the system. It is always called before the first
* time that the start method is called.
*/
public void init(){
String param;
param = getParameter(PARAMfps);
if(param != null)
mFps = Integer.parseInt(param);
param = getParameter(PARAMimageFile);
if(param != null)
imageFile = param;
param = getParameter(PARAMnumImages);
if(param != null)
numImages = Integer.parseInt(param);
resize(320,240);
if(mFps != 0)
nSleepTime = 1000/mFps;
}
//dessine l'image suivante si ttes les images sont chargées
private void displayImage(Graphics g){
if(!fAllLoaded)
return;
g.drawImage(mImages[nCurrImage],(getSize().width -
nImgWidth)/2,(getSize().height - nImgHeight)/2,null);
//afficher le numéro de cadre(optionnel)
g.setColor(Color.white);
g.drawString("Image #" + nCurrImage, 10, 40);
}
public void paint(Graphics g){
if(fAllLoaded){
Rectangle r = g.getClipBounds();
g.clearRect(r.x,r.y,r.width,r.height);
//displayImage(g); : à quoi ça sert
}
else
g.drawString("Loading Images...",10,20);
}
/**
* Called by the browser or applet viewer to inform this JApplet that it
* should start its execution. It is called after the init method and
* each time the JApplet is revisited in a Web page.
*/
public void start(){
if(mAnimation == null){
mAnimation = new Thread(this);
mAnimation.start();
}
}
/**
* Called by the browser or applet viewer to inform this JApplet that
* it should stop its execution. It is called when the Web page that
* contains this JApplet has been replaced by another page, and also
* just before the JApplet is to be destroyed.
*/
public void stop(){
if(mAnimation != null)
mAnimation = null;
}
public void run(){
nCurrImage = 0;
if(!fAllLoaded){
repaint();
mGraphics = getGraphics();
mImages = new Image[numImages];
//charger ttes les images
MediaTracker tracker = new MediaTracker(this);
String strImage;
for(int i = 1; i <= numImages; i++){
strImage = imageFile + i + ".gif";
mImages[i-1] = getImage(getCodeBase(),strImage);
tracker.addImage(mImages[i-1],0);
}
//attendre que toutes les images soient chargées
try{
tracker.waitForAll();
fAllLoaded = !tracker.isErrorAny();
}catch(InterruptedException e){}
if(!fAllLoaded){
stop();
mGraphics.drawString("Error Loading Images !",10,40);
return;
}
//s'assurer que ttes les images ont la meme taille
nImgWidth = mImages[0].getWidth(this);
nImgHeight = mImages[0].getHeight(this);
}
repaint();
while(true){
try{
repaint();
displayImage(mGraphics);
nCurrImage++;
if(nCurrImage == numImages)
nCurrImage = 0;
Thread.sleep(nSleepTime);
}catch(InterruptedException e){}
}
}
doesn't run : it seems to load images indefinitely. Here is the code :
If someone could help me, i would be very very happy!!!
Thanks!
Christina Lumineau
import java.awt.*;
import javax.swing.*;
/**
* Class Animation - write a description of the class here
*
* @author (your name)
* @version (a version number)
*/
public class Animation extends JApplet implements Runnable {
Thread mAnimation = null;
//Animation support
private Graphics mGraphics;//utilisé pour le contexte graphique
private Image mImages[];//tableau d'images
private int nCurrImage;//index de l'image courante
private int nImgWidth = 0;//width and...
private int nImgHeight = 0;//height of images
private boolean fAllLoaded = false;//true : images chargées
private int mFps = 10;
private String imageFile = "bonhomme";
private int numImages = 5;
private int nSleepTime;//temps de mise en sommeil(fps)
private final String PARAMfps = "fps";
private final String PARAMimageFile = "imageFile";
private final String PARAMnumImages = "numImages";
/**
* Called by the browser or applet viewer to inform this JApplet that it
* has been loaded into the system. It is always called before the first
* time that the start method is called.
*/
public void init(){
String param;
param = getParameter(PARAMfps);
if(param != null)
mFps = Integer.parseInt(param);
param = getParameter(PARAMimageFile);
if(param != null)
imageFile = param;
param = getParameter(PARAMnumImages);
if(param != null)
numImages = Integer.parseInt(param);
resize(320,240);
if(mFps != 0)
nSleepTime = 1000/mFps;
}
//dessine l'image suivante si ttes les images sont chargées
private void displayImage(Graphics g){
if(!fAllLoaded)
return;
g.drawImage(mImages[nCurrImage],(getSize().width -
nImgWidth)/2,(getSize().height - nImgHeight)/2,null);
//afficher le numéro de cadre(optionnel)
g.setColor(Color.white);
g.drawString("Image #" + nCurrImage, 10, 40);
}
public void paint(Graphics g){
if(fAllLoaded){
Rectangle r = g.getClipBounds();
g.clearRect(r.x,r.y,r.width,r.height);
//displayImage(g); : à quoi ça sert
}
else
g.drawString("Loading Images...",10,20);
}
/**
* Called by the browser or applet viewer to inform this JApplet that it
* should start its execution. It is called after the init method and
* each time the JApplet is revisited in a Web page.
*/
public void start(){
if(mAnimation == null){
mAnimation = new Thread(this);
mAnimation.start();
}
}
/**
* Called by the browser or applet viewer to inform this JApplet that
* it should stop its execution. It is called when the Web page that
* contains this JApplet has been replaced by another page, and also
* just before the JApplet is to be destroyed.
*/
public void stop(){
if(mAnimation != null)
mAnimation = null;
}
public void run(){
nCurrImage = 0;
if(!fAllLoaded){
repaint();
mGraphics = getGraphics();
mImages = new Image[numImages];
//charger ttes les images
MediaTracker tracker = new MediaTracker(this);
String strImage;
for(int i = 1; i <= numImages; i++){
strImage = imageFile + i + ".gif";
mImages[i-1] = getImage(getCodeBase(),strImage);
tracker.addImage(mImages[i-1],0);
}
//attendre que toutes les images soient chargées
try{
tracker.waitForAll();
fAllLoaded = !tracker.isErrorAny();
}catch(InterruptedException e){}
if(!fAllLoaded){
stop();
mGraphics.drawString("Error Loading Images !",10,40);
return;
}
//s'assurer que ttes les images ont la meme taille
nImgWidth = mImages[0].getWidth(this);
nImgHeight = mImages[0].getHeight(this);
}
repaint();
while(true){
try{
repaint();
displayImage(mGraphics);
nCurrImage++;
if(nCurrImage == numImages)
nCurrImage = 0;
Thread.sleep(nSleepTime);
}catch(InterruptedException e){}
}
}