Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

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.   Image picture;
  5.   public void init() {
  6.     picture =getImage(getDocumentBase(),“bike_1.gif”);
  7.   }
  8.   public void paint(Graphics g) {
  9.     for(int i=0;i<500;i++){
  10.       g.drawImage(picture, i,30this);
  11.       try{Thread.sleep(100);}catch(Exception e){}
  12.     }
  13.   }
  14. }
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>

Comments

comments