📄 clock.java
字号:
import java.awt.Graphics;
import java.awt.Color;
import java.util.Calendar;
/*类Clock继承了java.applet.Applet,由于要用到线程,因而实现了Runnable接口*/
public class Clock extends java.applet.Applet implements Runnable {
/*声明一个Thread对象threadObj*/
Thread threadObj;
/*下面的6个变量分别代表前一个位置秒针、分针、时针末端的横纵坐标*/
int lastxs, lastys, lastxm,lastym, lastxh, lastyh;
/*lastdate为前一个指针位置对应的日期*/
String lastdate;
/*handColor用于设置时针、分针和钟框的颜色*/
Color handColor;
/*numberColor设置秒针和钟面上数字的颜色*/
Color numberColor;
/*声明一个Calendar对象,用于取得当前时间*/
Calendar rightnow = Calendar.getInstance();
/*获得当前日期的年份并赋给yearInt*/
int yearInt = rightnow.get(rightnow.YEAR);
/*获得当前日期的月份并赋给monthInt*/
int monthInt = rightnow.get(rightnow.MONTH);
/*值得注意的是:月份以0为基准开始计数,一月对应的month值为0,依次类推*/
/*获得当前的日期,赋给dayInt*/
int dayInt = rightnow.get(rightnow.DATE);
/*获得当前时间时钟对应的整数并赋给hoursInt*/
int hoursInt = rightnow.get(rightnow.HOUR_OF_DAY);
/*获得当前时间分针对应的整数并赋给minutesInt*/
int minutesInt = rightnow.get(rightnow.MINUTE);
/*获得当前时间秒针对应的整数并赋给secondsInt*/
int secondsInt = rightnow.get(rightnow.SECOND);
/*获得当前时间对应的星期并赋给dayofweekInt*/
int dayofweekInt = rightnow.get(rightnow.DAY_OF_WEEK);
/*值得注意的是通过DAY_OF_WEEK获得的星期值中,星期天对应的值为1,星期一对应的为2,依次类推,为此,将利用getweekday方法转换(参考getweekday方法)。*/
/*init()方法用来初始化指针的位置和颜色,以及日期*/
public void init() {
/*将秒针、分针和时钟初始位置的横纵坐标设置为0*/
lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
/*将上面获得的信息用字符串表示出来*/
lastdate = yearInt + "年" + (monthInt + 1) + "月" + dayInt + "日" + hoursInt + ":" + minutesInt + ":" + secondsInt + " 星期" + getweekday(dayofweekInt);
/*设置时钟上时、分、秒针的颜色*/
handColor = Color.red;
/*设置时钟上数据的颜色*/
numberColor = Color.darkGray;
}
/*下面的方法变换星期的表示,将1,2,3,4,5,7匹配为7,1,2,3,4,5,6*/
public int getweekday(int dayInt) {
int weekdayInt;
switch(dayInt) {
case 1: weekdayInt = 7;
break;
default: weekdayInt = dayInt - 1;
}
return weekdayInt;
}
/*start()方法用来启动Clock线程*/
public void start() {
/*初始化线程对象,名称为“Clock”*/
threadObj = new Thread(this, "Clock");
threadObj.start();
}
/*update()方法用来刷新图象*/
public void update(Graphics g) {
paint(g);
}
/*run()方法用来实现刷新时钟图象*/
public void run() {
while (threadObj != null) {
try {
threadObj.sleep(10);
} catch(InterruptedException e) {}
/*repaint方法用于时间的刷新显示*/
repaint();
}
}
/*paint()绘制指针位置并进行可能的刷新操作,同时显示当前日期和时间*/
public void paint(Graphics g) {
/*下面的前6个int变量分别代表时针、分钟和秒钟外边缘端点的横纵坐标,xcenter和ycenter代表时钟的中心位置*/
int xh, yh, xm, ym, xs, ys, xcenter = 100, ycenter = 70;
/*下面的语句用于获得当前时间*/
Calendar rightnow = Calendar.getInstance();
yearInt = rightnow.get(rightnow.YEAR);
monthInt = rightnow.get(rightnow.MONTH);
dayInt = rightnow.get(rightnow.DATE);
hoursInt = rightnow.get(rightnow.HOUR_OF_DAY);
minutesInt = rightnow.get(rightnow.MINUTE);
secondsInt = rightnow.get(rightnow.SECOND);
dayofweekInt = rightnow.get(rightnow.DAY_OF_WEEK);
/*字符串today包含当前日期和时间信息*/
String today = yearInt + "年" + (monthInt + 1) + "月" + dayInt + "日" + hoursInt + ":" + minutesInt + ":" + secondsInt + " 星期" + getweekday(dayofweekInt);
/*计算秒针、分针、时针外边缘端点的横纵坐标(设定三针长度分别为45,40,30)*/
xs = (int)(Math.cos(secondsInt * 3.14f / 30 - 3.14f / 2) * 45 + xcenter);
ys = (int)(Math.sin(secondsInt * 3.14f / 30 - 3.14f / 2) * 45 + ycenter);
/*值得注意的是,计算分针外边缘端点的横纵坐标时,需要考虑秒针对它的影响*/
xm = (int)(Math.cos((minutesInt + secondsInt/60f) * 3.14f / 30 - 3.14f / 2) * 40 + xcenter);
ym = (int)(Math.sin((minutesInt + secondsInt/60f) * 3.14f / 30 - 3.14f / 2) * 40 + ycenter);
/*计算时针外边缘端点的横纵坐标时,需要考虑分针对它的影响,将秒针的影响忽略了*/
xh = (int)(Math.cos((hoursInt * 30 + minutesInt / 2) * 3.14f / 180 - 3.14f/2) * 30 + xcenter);
yh = (int)(Math.sin((hoursInt * 30 + minutesInt / 2) * 3.14f / 180 - 3.14f/2) * 30 + ycenter);
/*绘制矩形和数字(9, 12, 3, 6)*/
handColor = Color.red;
g.setColor(handColor);
/*下面的语句绘制红色的矩形外框*/
g.drawRect(xcenter - 50, ycenter - 50, 100, 100);
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);
/*下面的语句根据当前时间和上一次显示的时间时、分、秒针是否相同来擦除相应的指针,绘制新指针*/
g.setColor(getBackground());
if (xs != lastxs || ys != lastys) {
/*擦除秒针的显示*/
g.drawLine(xcenter, ycenter, lastxs, lastys);
/*擦除界面中日期和时间的字符串*/
g.drawString(lastdate, 30, 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("", 30, 150);
g.drawString(today, 30, 150);
g.drawLine(xcenter, ycenter, xs, ys);
/*将分针和时针的颜色设置为蓝色*/
handColor = Color.blue;
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);
g.drawLine(xcenter, ycenter - 2, xh, yh);
g.drawLine(xcenter - 2, ycenter, xh, yh);
/*将当前时间的时、分、秒针的横纵坐标赋给前一次时间对应的变量*/
lastxs=xs;
lastys=ys;
lastxm=xm;
lastym=ym;
lastxh=xh;
lastyh=yh;
/*将当前日期赋给上一次日期*/
lastdate = today;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -