EarthWeb
Developer.com
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-

Wake-up a Java applet


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

internet.commerce



Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers