Home Tutorial Java Thread Java Thread In Applet

 
 

Share on Google+Share on Google+
Java Thread In Applet
Posted on: November 9, 2009 at 12:00 AM
Advertisement
In this segment of tutorial we will learn how to use the Thread in the Applet.Then We will create an example and use the Thread with Applet.

  • Java Thread Applet is a java class that runs inside the internet browser.
  • It is used to make the gui application, network application in java
  • Thread is used in applet to make the multithread application

Example of Java Thread in Applet

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Color;
import java.util.Date;

//<applet code="Clock" width="100" height="100"></applet>
public class Clock extends java.applet.Applet implements Runnable {
private Thread clockThread = null;

public void init() {
setBackground(Color.white);
}

public void start() {
if (clockThread == null) {
clockThread = new Thread(this, "Clock");
clockThread.start();
}
}

public void run() {
Thread myThread = Thread.currentThread();
while (clockThread == myThread) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void paint(Graphics g) {
setBackground(Color.CYAN);
Font f = new Font("serif", Font.BOLD, 25);
g.setFont(f);
Date now = new Date();
g.drawString(now.getHours() + ":" + now.getMinutes() + ":"
+ now.getSeconds(), 5, 30);
}

public void stop() {
clockThread = null;
}
}
Advertisement

Related Tags for Java Thread In Applet :


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.

Posted on: November 9, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com