转载的学习下
package com.huaxia.wang;
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
/**
* @目的:时钟的小程序
* @author Rachel Gollub
* @modified Daniel Peek replaced circle drawing calculation, few more changes
*/
public class Clock extends Applet implements Runnable {
private volatile Thread timer; // The thread that displays clock
private int lastxs, lastys, lastxm,
lastym, lastxh, lastyh; // Dimensions used to draw hands
private SimpleDateFormat formatter; // Formats the date displayed
private String lastdate; // String to hold date displayed
private Font clockFaceFont; // Font for number display on clock
private Date currentDate; // Used to get date to display
private Color handColor; // Color of main hands and dial
private Color numberColor; // Color of second hand and numbers
private int xcenter = 80, ycenter = 55; // Center position
public void init() {
int x,y;
lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy",
Locale.getDefault());
currentDate = new Date();
lastdate = formatter.format(currentDate);
clockFaceFont = new Font("Serif", Font.PLAIN, 14);
handColor = Color.blue;
numberColor = Color.darkGray;
try {
setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
16)));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
16));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
16));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
resize(300,300); // Set clock window size
}
// Paint is the main part of the program
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;
}
// Set position of the ends of the hands
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);
// Get the date to print at the bottom
formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
today = formatter.format(currentDate);
g.setFont(clockFaceFont);
// Erase if necessary
g.setColor(getBackground());
if (xs != lastxs || ys != lastys) {
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.drawString(lastdate, 5, 125);
}
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);
}
// Draw date and hands
g.setColor(numberColor);
g.drawString(today, 5, 125);
g.drawLine(xcenter, ycenter, xs, ys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, xm, ym);
g.drawLine(xcenter-1, ycenter, xm, ym);
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);
// Draw the circle and numbers
g.setColor(handColor);
g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);
g.setColor(numberColor);
g.drawString("9", xcenter-45, ycenter+3);
g.drawString("3", xcenter+40, ycenter+3);
g.drawString("12", xcenter-5, ycenter-37);
g.drawString("6", xcenter-3, ycenter+45);
// Draw date and hands
g.setColor(numberColor);
g.drawString(lastdate, 5, 125);
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();
}
}
public String getAppletInfo() {
return "Title: A Clock \n"
+ "Author: Rachel Gollub, 1995 \n"
+ "An analog clock.";
}
public String[][] getParameterInfo() {
String[][] info = {
{"bgcolor", "hexadecimal RGB number",
"The background color. Default is the color of your browser."},
{"fgcolor1", "hexadecimal RGB number",
"The color of the hands and dial. Default is blue."},
{"fgcolor2", "hexadecimal RGB number",
"The color of the second hand and numbers. Default is dark gray."}
};
return info;
}
}
分享到:
相关推荐
一个时钟用JAVA APPLET写的。与真的表有一样的功能
经典JAVA(Applet)教程课件,PPT。
这是一个JavaGUI程序设计的完整作品 1.带完整的报告 2.带详细的使用方法 ...这是一个显示时钟的java Applet 用JavaGUI编程实现 能实现机械表面和电子表面的单机切换 能实现与网页文件的交互选择改变市区
java写的简单的时钟程序,界面简洁漂亮,对初学者很有参考价值, 类Clock继承了java.applet.Applet,并实现了Runnable接口
java做的时钟,实时显示时间,钟面带数字
用applet编写的一个简单的时钟小程序,有动态的时钟模拟画面。
将这个Java文件编译后,再嵌入到HTML中,打开,会有一个不停走动的时钟!而且不用调时间!和系统时间同步! 非常好玩!非常有意思! 想改成Java应用程序!但不知怎么做,望高人指点!
Java 网页版电子时钟 Applet,使用Java多线程方法,有利于学习和使用!
Java写桌面时钟Applet小程序源代码,类名为Clock就可以直接运行。
内有编写计算器、日历以及时钟的Java程序源代码,应用多种方法(有Application(CUI与GUI)和Applet)编写。
java applet
显示时钟 JSP ClockApplet.jsp ClockApplet.java
运用java语言编写 实现时钟功能 为java_Applet方式实现
一个JAVA的APPLET的时钟程序
java applet趣味程序
Java圆形电子时钟源代码 1个目标文件 内容索引:JAVA源码,系统相关,电子钟 用JAVA编写的指针式圆形电子钟,效果图如下所示,其实代码很简单,希望对你有帮助。 Message-Driven Bean EJB实例源代码 2个目标文件 ...
这是个日历小程序,含有日历、时钟和背景音乐,很不错哦
Applet钢琴模拟程序java源码 Calendar万年历 copass源代码.rar DataBuffer在Java中使用ADO.NET.rar EJB 模拟银行ATM流程及操作源代码 ejbCreate函数用于初始化一个EJB实例 EJB中JNDI的使用源码例子 EJB的真实世界...
Java写的巨型LCD液晶时钟显示屏.rar JAVA图书馆管理系统源码.rar Java图像文件的缩小与放大.rar Java学生信息管理系统源码包.rar Java推箱子游戏(50关+音效).rar Java文件切割器源代码.rar Java文件选择器源代码....
Java圆形电子时钟源代码 1个目标文件 内容索引:JAVA源码,系统相关,电子钟 用JAVA编写的指针式圆形电子钟,效果图如下所示,其实代码很简单,希望对你有帮助。 Message-Driven Bean EJB实例源代码 2个目标文件 ...