This is a program for creating Analogue Clock using applet. An applet is a program written in the Java programming language that can
be included in an HTML page, much in the same way an image is included
in a page. When you use a Java technology-enabled browser to view a page
that contains an applet, the applet's code is transferred to your
system and executed by the browser's Java Virtual Machine (JVM). For
information and examples on how to include an applet in an HTML page.
For this we need two files one java file and one html file. In the java file the below program should be pasted and should save as "Clock.java" and html file you have to include the program given bellow
<applet
code="Clock" width="150"
height="150"></Applet>
Applet code can be replace by the name of java file.(if you use another program.).
html file can be save in any name here i used Clock.html
Program
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
public class Clock extends Applet implements Runnable {
private volatile Thread timer;
private int lastxs, lastys, lastxm,
lastym, lastxh, lastyh;
private SimpleDateFormat formatter;
private String lastdate;
private Font clockFaceFont;
private Date currentDate;
private Color handColor,hc2;
private Color numberColor;
private int xcenter = 80, ycenter = 55;
public void init() {
int x,y;
lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
formatter = new SimpleDateFormat ("dd MMM yyyy hh:mm:ss",
Locale.getDefault());
currentDate = new Date();
lastdate = formatter.format(currentDate);
clockFaceFont = new Font("Arial", Font.BOLD, 16);
handColor = Color.blue;
numberColor = Color.darkGray;
try {
setBackground(Color.white);
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
handColor = Color.blue;
hc2=Color.green;
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
numberColor = Color.darkGray;
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
resize(170,170);
}
public void update(Graphics g) {
int xh, yh, xm, ym, xs, ys;
int s = 0, m = 10, h = 10;
String today;
currentDate = new Date();
formatter.applyPattern("s");
try {
s = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
s = 0;
}
formatter.applyPattern("m");
try {
m = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
m = 10;
}
formatter.applyPattern("h");
try {
h = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
h = 10;
}
xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
+ xcenter);
yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
+ ycenter);
formatter.applyPattern("dd MMM yyyy HH:mm:ss");
today = formatter.format(currentDate);
g.setFont(clockFaceFont);
g.setColor(getBackground());
if (xs != lastxs || ys != lastys) {
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.drawString(lastdate, 5, 150);
}
if (xm != lastxm || ym != lastym) {
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym);
}
if (xh != lastxh || yh != lastyh) {
g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
}
g.setColor(numberColor);
g.drawString(today, 5, 150);
g.drawLine(xcenter, ycenter, xs, ys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, xm, ym);
g.drawLine(xcenter-1, ycenter, xm, ym);
g.setColor(hc2);
g.drawLine(xcenter, ycenter-1, xh, yh);
g.drawLine(xcenter-1, ycenter, xh, yh);
lastxs = xs; lastys = ys;
lastxm = xm; lastym = ym;
lastxh = xh; lastyh = yh;
lastdate = today;
currentDate = null;
}
public void paint(Graphics g) {
g.setFont(clockFaceFont);
g.setColor(handColor);
g.drawArc(xcenter-51, ycenter-51, 110, 110, 0, 360);
g.drawArc(xcenter-53, ycenter-53, 115, 115, 0, 360);
g.setColor(numberColor);
g.drawString("9", xcenter-45, ycenter+5);
g.drawString("3", xcenter+40, ycenter+5);
g.drawString("12", xcenter-5, ycenter-37);
g.drawString("6", xcenter-1, ycenter+55);
g.setColor(numberColor);
g.drawString(lastdate, 5, 150);
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym);
g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
}
public void start() {
timer = new Thread(this);
timer.start();
}
public void stop() {
timer = null;
}
public void run() {
Thread me = Thread.currentThread();
while (timer == me) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
}
repaint();
}
}
}
import java.awt.*;
import java.applet.*;
import java.text.*;
public class Clock extends Applet implements Runnable {
private volatile Thread timer;
private int lastxs, lastys, lastxm,
lastym, lastxh, lastyh;
private SimpleDateFormat formatter;
private String lastdate;
private Font clockFaceFont;
private Date currentDate;
private Color handColor,hc2;
private Color numberColor;
private int xcenter = 80, ycenter = 55;
public void init() {
int x,y;
lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
formatter = new SimpleDateFormat ("dd MMM yyyy hh:mm:ss",
Locale.getDefault());
currentDate = new Date();
lastdate = formatter.format(currentDate);
clockFaceFont = new Font("Arial", Font.BOLD, 16);
handColor = Color.blue;
numberColor = Color.darkGray;
try {
setBackground(Color.white);
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
handColor = Color.blue;
hc2=Color.green;
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
numberColor = Color.darkGray;
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
resize(170,170);
}
public void update(Graphics g) {
int xh, yh, xm, ym, xs, ys;
int s = 0, m = 10, h = 10;
String today;
currentDate = new Date();
formatter.applyPattern("s");
try {
s = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
s = 0;
}
formatter.applyPattern("m");
try {
m = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
m = 10;
}
formatter.applyPattern("h");
try {
h = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
h = 10;
}
xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
+ xcenter);
yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
+ ycenter);
formatter.applyPattern("dd MMM yyyy HH:mm:ss");
today = formatter.format(currentDate);
g.setFont(clockFaceFont);
g.setColor(getBackground());
if (xs != lastxs || ys != lastys) {
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.drawString(lastdate, 5, 150);
}
if (xm != lastxm || ym != lastym) {
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym);
}
if (xh != lastxh || yh != lastyh) {
g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
}
g.setColor(numberColor);
g.drawString(today, 5, 150);
g.drawLine(xcenter, ycenter, xs, ys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, xm, ym);
g.drawLine(xcenter-1, ycenter, xm, ym);
g.setColor(hc2);
g.drawLine(xcenter, ycenter-1, xh, yh);
g.drawLine(xcenter-1, ycenter, xh, yh);
lastxs = xs; lastys = ys;
lastxm = xm; lastym = ym;
lastxh = xh; lastyh = yh;
lastdate = today;
currentDate = null;
}
public void paint(Graphics g) {
g.setFont(clockFaceFont);
g.setColor(handColor);
g.drawArc(xcenter-51, ycenter-51, 110, 110, 0, 360);
g.drawArc(xcenter-53, ycenter-53, 115, 115, 0, 360);
g.setColor(numberColor);
g.drawString("9", xcenter-45, ycenter+5);
g.drawString("3", xcenter+40, ycenter+5);
g.drawString("12", xcenter-5, ycenter-37);
g.drawString("6", xcenter-1, ycenter+55);
g.setColor(numberColor);
g.drawString(lastdate, 5, 150);
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym);
g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
}
public void start() {
timer = new Thread(this);
timer.start();
}
public void stop() {
timer = null;
}
public void run() {
Thread me = Thread.currentThread();
while (timer == me) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
}
repaint();
}
}
}
Compliation And Running
javac Clock.java
appletviewer Clock.html
Output
No comments:
Post a Comment