📄 zhong.java
字号:
/*
* zhong.java
*
* Created on 2007年3月22日, 下午8:22
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package zhong;
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
public class zhong 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; // 表针和数字的颜色
private Color numberColor;
private Color HourColor;
private Color MinuteColor;
private Color SecondColor;
private int xcenter = 150, ycenter = 100; // 中心位置(重新设置位置使得表盘变大)
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;
HourColor = Color.red; //时针为红色
MinuteColor = Color.black; //分针为黑色
SecondColor = Color.blue; //秒针为蓝色
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); // 设置钟表窗口大小
}
// 绘制钟表的主要部件
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) * 55 + xcenter);
ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 55 + ycenter);
xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 40
+ xcenter);
yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 40
+ ycenter);
// 获得显示在底部的日期数据
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, 50, 200);
}
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, 50, 200);
g.setColor(SecondColor);
g.drawLine(xcenter, ycenter, xs, ys); //绘制秒针
g.setColor(HourColor);
g.drawLine(xcenter, ycenter-1, xm, ym); //绘制时针
g.drawLine(xcenter-1, ycenter, xm, ym);
g.setColor(MinuteColor);
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-75, ycenter-75, 150, 150, 0, 360); //绘制圆
g.setColor(numberColor);
g.drawString("9", xcenter-70, ycenter+3); //绘制数字
g.drawString("3", xcenter+65, ycenter+3);
g.drawString("12", xcenter-5, ycenter-60);
g.drawString("6", xcenter-3, ycenter+70);
// 绘制日期和指针
g.setColor(numberColor);
g.drawString(lastdate, 50, 200);
g.setColor(SecondColor);
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.setColor(HourColor);
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym);
g.setColor(MinuteColor);
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -