Share on Google+Share on Google+

Animation in Java Applet

Advertisement
In this segment of tutorial we will learn about the Animation in Java applet. Then We will create an applet and use the animation it.

  • Applet is a java class that runs inside the internet browser.
  • It uses Thread along with its methods for animation.
  • Applet class implements the Runnable interface for animation program.


Example of Animation in Java program
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

//<applet code="rect1" width="300" height="160"></applet>
public class rect1 extends Applet implements Runnable {
Thread t1;

public void init() {
t1 = new Thread(this);
}

public void start() {
t1.start();
}

public void run() {
Thread t2 = Thread.currentThread();
while (t1 == t2) {
repaint();
}
}

public void paint(Graphics g) {
Color c[] = { Color.red, Color.yellow, Color.BLUE, Color.pink,
Color.MAGENTA };

for (int x = 0; x <= 4; x++) {
try {
Thread.sleep(1000);
g.setColor(c[x]);
g.fillRect(10, 10, 200, 200);
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void stop() {
t1 = null;
}
}

Advertisements

Advertisement

Posted on: November 9, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.