Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

pb with animation of images

Thread view: 
Christina Lumineau - 24 Apr 2006 18:49 GMT
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){}
       }
   }
andrewthommo@gmail.com - 26 Apr 2006 01:24 GMT
> Hello, I have wrote a code for animate images with an applet,

Applets are a lot easier to debug if you can load all the files to
the internet so people can see it (work or break) at the click of a
link.

>..but  it
> doesn't run : it seems to load images indefinitely.

What makes you say that?
Have you checked the the Java console?
What format are the images (GIF, JPEG, PNG)?
What version of Java is your browser running?

> ..Here is the code :

It was a good idea to include the code, perhaps someone else
will spot a problem with it, but unfortunately I do not have the time
to compile run it just now.

OTOH (on the other hand), the problem might not be in the Java
code at all.  Applets are tricky, and can be much harder to
debug than applications.

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.