SpinTop


import java.applet.*;
import java.awt.*;

/*
@Author: Ajit Kumar (ajit@omnikron.com)
@Date: Jan 96
@Version: 0.1
@Description : A simple animation applet to demonstrate use of threads.
*/


/* SpinTop class */
public class SpinTop extends Applet implements Runnable {
	Thread spinThread = null;
	Image img[] = new Image[18];
	int counter =  0;
	int clockwise = 1;

	/* class initialization */
	public void init() {
		int i;
		for (i=0;i<18;i++) {
			img[i]=getImage(getCodeBase(),"tops"+i+".gif");
		}
	}

	/* Next Spin image */
	public void nextSpin(){
			if (clockwise==1) {
				counter = counter + 1;
				if (counter == 18) {
					counter = 0;
				}
			}
			else {
				counter = counter -1;
				if (counter < 0) {
					counter = 17;
				}
			}
	}

	/* draw the Spin image */
	public void update(Graphics g) {
		g.drawImage(img[counter],0,0,this);
	}

	/* start the thread */
	public void start() {
		if (spinThread == null) {
			spinThread = new Thread(this);
			spinThread.start();
		}
	}

	/* stop the thread */
	public void stop() {
		if (spinThread!=null) {
			spinThread.stop();
			spinThread = null;
		}
	}

	/* applet's run() method */
	public void run() {
		while (spinThread != null) {
			repaint();
			try {
			   spinThread.sleep(75);
			}
			catch(InterruptedException e) {
			}
			nextSpin();
		}
	}

	/* handle MOUSE_DOWN event */
	public boolean handleEvent(Event e){
		if (e.id == Event.MOUSE_DOWN) {
			clockwise = 1 - clockwise;
		}
	return true;
	}
}

Back to the SpinTop applet

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.