日历时钟 rlsz.java

佛山谢山水 2002.6.18.

// 时钟针转一周360°,时钟针每秒转过的角度:秒针 6(°/秒),分针 0.1(°/秒),时针 1/120(°/秒)。

import java.awt.Graphics;//导入java语言awt抽象窗口工具包的Graphics类
import java.awt.Color; //导入java语言awt包的Color类
import java.util.Date; //导入java语言util实用工具包的Date类

//时钟sz类继承Java小程序Applet类
public class sz extends java.applet.Applet{
//paint方法输出屏幕图形(图形对象g) 
public void paint(Graphics g){
//每间隔一段时间分别设置背景蓝和绿色1次,刷新背景,移动时分秒针位置.
for(int i=0;i<60;i++){ 
setBackground(Color.blue);
setBackground(Color.green);

//方法Date() 检测到年月日-时分秒赋值给日期类timeNow 
Date timeNow = new Date();
Clock myClock = new Clock(timeNow.getHours(),
timeNow.getMinutes(),
timeNow.getSeconds());
//显示当前日期时间数字格式为:星期-月日-时分秒-年
g.setColor(new Color(0,0,255)); //设置画笔为蓝色
g.drawString(timeNow.toString(),18,275);
g.drawString("日历时钟 佛山 谢山水 2002.7.20.设计",16,295);
//图形对象g,中心xy,半径r.
myClock.show(g,120,120,100);
}
}

//显示钟面-刻度-指针
class Clock{
Clock(int hrs,int min,int sec){
//用模除(%)把系统时间24小时制转为时钟12小时制显示方式
hour = hrs % 12;
minute = min;
second = sec;
}

void show(Graphics g,int center_x,int center_y,int radius){
int hrs_nid_len = (int)(radius * 0.4), //定出时针的长度
hrs_nid_len1 = (int)(radius * 0.5),
min_nid_len = (int)(radius * 0.6), //定出分针的长度
min_nid_len1 = (int)(radius * 0.7),
sec_nid_len = (int)(radius * 0.85),//定出秒针的长度
kd = (int)(radius * 0.8), //定出刻度的长度
kd1 = (int)(radius * 0.95);
//刻度转角,指针转角,刻度间隔角度,宽度
double theta,theta1,theta2; 

//画出矩形框
g.setColor(new Color(255,255,0)); //设置画笔为黄色
g.drawRect
(5,5,230,310);
g.drawRect
(6,6,228,308);
g.drawRect
(7,7,226,306);

//画出日历框
g.drawRect
(13,255,213,50);
g.drawRect
(12,254,215,52);

//画出钟面红圈
g.setColor(new Color(255,0,0));
//画椭圆(左上角坐标,外切矩形宽、高)
g.drawOval
(center_x-radius,center_y-radius,radius * 2,radius * 2);

//画出钟面蓝圈
g.setColor(new Color(0,0,255));
//画椭圆(左上角坐标,外切矩形宽、高)
g.drawOval
(center_x-radius+2,center_y-radius+2,radius*2-4,radius*2-4);
g.drawOval
(center_x-radius+3,center_y-radius+3,radius*2-6,radius*2-6);
g.drawOval
(center_x-radius+4,center_y-radius+4,radius*2-8,radius*2-8);

//画出钟面红圈
g.setColor(new Color(255,0,0));
//画椭圆(左上角坐标,外切矩形宽、高)
g.drawOval
(center_x-radius+6,center_y-radius+6,radius*2-12,radius*2-12);
//画出钟面中心轴
g.fillOval
(center_x-5,center_y-5,11,11);

//钟面小时刻度的间隔角度,刻度的宽度
theta1 = Math.PI/6.0;
theta2 = theta1/20.0;
//画出钟面小时刻度
for(theta=0;theta<2.0*Math.PI;theta+=theta1){
drawKd
(g,Color.red,center_x,center_y,kd,kd1,theta-theta2);
drawKd
(g,Color.red,center_x,center_y,kd,kd1,theta);
drawKd
(g,Color.red,center_x,center_y,kd,kd1,theta+theta2);
}

//时针旋转角度
// =当前时分秒,时针转一周12小时分别转换为秒数的比 * 2PI
theta = (double)(hour * 60 * 60 + minute * 60 + second)/
(12.0 * 3600.0) * 2.0 * Math.PI;
//画出时针
drawNiddle(g,Color.blue,center_x,center_y,hrs_nid_len,theta-theta2);
drawNiddle(g,Color.blue,center_x,center_y,hrs_nid_len1,theta);
drawNiddle(g,Color.blue,center_x,center_y,hrs_nid_len,theta+theta2);

//分针旋转角度=当前分秒,分针转一周60分钟分别转换为秒数的比 * 2PI
theta = (double)(minute * 60 + second)/3600.0 * 2.0 * Math.PI;
//画出分针
drawNiddle(g,Color.blue,center_x,center_y,min_nid_len,theta-theta2);
drawNiddle(g,Color.blue,center_x,center_y,min_nid_len1,theta);
drawNiddle(g,Color.blue,center_x,center_y,min_nid_len,theta+theta2);

//秒针旋转角度=当前秒/秒针转一周60秒的比 * 2PI
theta = (double)second/60.0 * 2.0 * Math.PI;
//画出秒针
drawNiddle(g,Color.red,center_x,center_y,sec_nid_len,theta);
}

//画刻度的方法
private void drawKd
(Graphics g,Color c,int x,int y,int len,int len1,double theta)
{
g.setColor(c); //设置画笔颜色
g.drawLine((int)(x+len * Math.sin(theta)),
(int)(y-len * Math.cos(theta)),
(int)(x+len1 * Math.sin(theta)),
(int)(y-len1 * Math.cos(theta)));
}

//画指针的方法
private void drawNiddle
(Graphics g,Color c,int x,int y,int len,double theta){
g.setColor(c); //设置画笔颜色
g.drawLine(x,y,(int)(x+len * Math.sin(theta)),
(int)(y-len * Math.cos(theta)));
}
int hour,minute,second;
}
}