Author: Real Gagnon
Author's WebSite: http://tactika.com/realhome/realhome.html
When moving the mouse over an image, we want to start an applet (here a simple chronometer), and when the mouse leave the image, the Applet should stop.
[JAVA APPLET]
import java.applet.*;
import java.awt.*;
public class JavaChrono extends Applet {
String previousTime = "0" ;
String currentTime = "0";
long j,k, l;
boolean okToChrono = false;
Color b, f;
public void init() {
b = getBackground();
f = getForeground();
MyThread tm = new MyThread(this);
tm.start();
}
public void startChrono() {
j = System.currentTimeMillis();
okToChrono = true;
}
public void stopChrono() {
okToChrono = false;
}
public void paint(Graphics g) {
if (okToChrono) {
g.setColor(b);
g.drawString(previousTime,10,30);
g.setColor(f);
k = System.currentTimeMillis();
l = k-j;
currentTime = Long.toString(l/1000);
g.drawString(currentTime,10,30);
previousTime = currentTime;
}
}
}
class MyThread extends Thread {
JavaChrono theApplet;
public MyThread(JavaChrono a) {
theApplet = a;
}
public void run() {
while (true) {
try {
theApplet.repaint();
this.sleep(1000);
}
catch(InterruptedException e) { }
}
}
}
[HTML]
<HTML><HEAD></HEAD><BODY>
<APPLET CODE=JavaChrono.class
WIDTH=150
HEIGHT=150>
</APPLET>
<A HREF=""
onMouseOver="document.applets[0].startChrono()"
onMouseOut="document.applets[0].stopChrono()" >
<IMG SRC="whatever.gif" WIDTH=100 HEIGHT=100>
</A>
</BODY></HTML>
Posted On: 7-Jul-1999