0% found this document useful (0 votes)
76 views12 pages

A Simple Applet - Digital Clock

This document provides information about creating a digital clock applet in Java. It includes an example digital clock applet code that uses threads to continuously repaint the clock display. The applet overrides init(), start(), stop(), run(), and paint() methods to define the applet lifecycle and clock updating logic. The paint() method uses Calendar and String drawing methods to display the current time on the applet window. The document also provides details about common Java classes like Color, Font, and Thread used in the example applet code.

Uploaded by

shinai_tomata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views12 pages

A Simple Applet - Digital Clock

This document provides information about creating a digital clock applet in Java. It includes an example digital clock applet code that uses threads to continuously repaint the clock display. The applet overrides init(), start(), stop(), run(), and paint() methods to define the applet lifecycle and clock updating logic. The paint() method uses Calendar and String drawing methods to display the current time on the applet window. The document also provides details about common Java classes like Color, Font, and Thread used in the example applet code.

Uploaded by

shinai_tomata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

A Simple Applet --- Digital Clock

import java.awt.*;
import java.util.Calendar;

public class DigitalColok
extends java.applet.Applet
implements Runnable {
<Fields>
<Methods>
}

The import clause is not necessary to use the
library. It is only a convenience.
An applet must be a subclass of
java.applet.Applet.
The Applet Methods
Public void init(){...}
invoked when the applet is loaded initially
public void start(){...}
invoked when entering the web page that contains the applet
public void stop(){...}
invoked when leaving the web page that contains the applet
public void run(){...}
run the applet, i.e., the main driver of the applet
public void paint(Graphics g){...}
paint the picture
The Life-Cycle of An Applet
Fields and Initialization
protected Thread clockThread = null;
protected Font font =
new Font("Monospaced", Font.BOLD, 48);
protected Color color = Color.green;


By default all class fields are automatically
initialized to their default values, usually 0 or null.







The start() and stop() Methods
public void start() {
if (clockThread == null) {
clockThread = new Thread(this);
clockThread.start();
}
}

public void stop() {
clockThread = null;
}


Start and stop the thread.
Stopped threads will not consume CPU time.
The run() Method
public void run() {
while (Thread.currentThread()
== clockThread) {
repaint();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e){}
}
}

In each iteration, repaint() is invoked, then
sleep 1 second.
Sleep() must be invoked inside the try block.
The paint() Method
public void paint(Graphics g) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
g.setFont(font);
g.setColor(color);
g.drawString(hour +
":" + minute / 10 + minute % 10 +
":" + second / 10 + second % 10,
10, 60);
}
Who Calls run() And paint()?
clockThread.start() calls
DigitalClock.run()
DigitalClock.repaint() calls
DigitalClock.paint()
The paint() method is usually not called
directly.

Drawing Strings
g.drawString("A Sample String", x, y)
HTML Source
<!--DigitalClockDemo.html-->
<html>
<head>
<title>Digital Clock Applet</title>
</head>
<body bgcolor=white>
<h1>The Digital Clock Applet</h1><p>
<applet code=DigitalClock.class
width=250 height=80>
</applet>
<p><hr>
<a href=DigitalClock.java>The source</a>
</body>
</html>
The java.awt.Color Class
Instances of the Color class represent colors.
new Color(r, g, b)
where r, g, b are the values of the red, green, and
blue components, respectively. They are in the in
the range of 0 to 255.
Some common colors are predefined as constants.
black gray orange
yellow
blue green pink
cyan lightGray red
darkGray magenta white
The java.awt.Font Class
Fonts are specified with three attributes:
font name:
Serif Sans-serif Monospaced Dialog DialogInput
TimesRoman Helvetica Courier Dialog
font style:
PLAIN BOLD ITALIC
Styles can be combined: Font.BOLD|Font.ITALIC
font size: a positive integer
A font can be created as follows:
new Font(name, style, size)

You might also like