Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com

Animation in Applet

Animation-Applet

 

Animation in Applet

Applet is mostly used in games and animation. For this purpose image is required to be moved.

Example of animation in applet:

  1. import java.awt.*;  
  2. import java.applet.*;  
  3. public class AnimationExample extends Applet {  
  4.   
  5.   Image picture;  
  6.   
  7.   public void init() {  
  8.     picture =getImage(getDocumentBase(),"bike_1.gif");  
  9.   }  
  10.     
  11.   public void paint(Graphics g) {  
  12.     for(int i=0;i<500;i++){  
  13.       g.drawImage(picture, i,30this);  
  14.   
  15.       try{Thread.sleep(100);}catch(Exception e){}  
  16.     }  
  17.   }  
  18. }  
In the above example, drawImage() method of Graphics class is used to display the image. The 4th argument of drawImage() method of is ImageObserver object. The Component class implements ImageObserver interface. So current class object would also be treated as ImageObserver because Applet class indirectly extends the Component class.

myapplet.html

  1. <html>  
  2. <body>  
  3. <applet code="DisplayImage.class" width="300" height="300">  
  4. </applet>  
  5. </body>  
  6. </html>  
download this example.

Post a Comment

© JAVA. The Best Codder All rights reserved. Distributed by