【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-Simple_Clock.java,希望本篇文章对您学习有所帮助。
package chp07;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Simple_Clock extends JFrame implements ActionListener {
int x, y, x_1, y_1, r, h, ss_x, ss_y, mm_x, mm_y, hh_x, hh_y, ss, mm, hh,
old_m, old_h, ang;
final double RAD = Math.PI / 180; // 度数转换成弧度的比例
// 构造函数创建了一个窗体
public Simple_Clock() {
super("Java Applet小时钟");
setDefaultCloseOperation(3);
Image image = getToolkit().getImage("1.jpg");
setIconImage(image);
setSize(250, 250);
setBackground(Color.black);
setLocation(300, 150);
setResizable(true);
show();
int delay = 1000;
// 创建一个监听事件
ActionListener draw = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
};
// 创建一个时间计数器,每一秒触发一次
new Timer(delay, draw).start();
}
// 实现ActionListener接口必须实现的方法
public void actionPerformed(ActionEvent e) {
}
// 绘制图形
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;// Graphics强制类型转换成Graphics2D
Insets insets = getInsets();// 创建JFrame的边界对象
int L = insets.left / 2, T = insets.top / 2;// L=左边界的2/1,T=上边界的2/1
h = getSize().height;// 返回JFrame的高度
g.setColor(new Color(115, 74, 18));// 设置画笔的颜色为棕色
// 画圆
g2D.setStroke(new BasicStroke(5.0f));// 勾画出宽度为4的圆
g.drawOval(L + 40, T + 40, h - 80, h - 80);//
r = h / 2 - 40;
x_1 = 40 + r - 5 + L;
y_1 = 40 + r - 5 - T;
ang = 60;
// 绘制时钟上的12个字
g.setFont(new Font("", Font.BOLD, 14));
g.setColor(new Color(240, 255, 255));
for (int i = 1; i <= 12; i++) {
x = (int) ((r + 12) * Math.cos(RAD * ang) + x_1);
y = (int) ((r + 12) * Math.sin(RAD * ang) + y_1);
g.drawString("" + i, x, h - y);
ang -= 30;
}
// 获得现在的时间
Calendar now = new GregorianCalendar();// 创建Calendar对象now
int hour = now.get(Calendar.HOUR_OF_DAY);// 取得当前的小时部分
int minute = now.get(Calendar.MINUTE);// 取得当前时间的分钟部分
int second = now.get(Calendar.SECOND);// 取得当前时间的秒
String st;
if (hour < 10)
st = "0" + hour;
else
st = "" + hour;
if (minute < 10)
st += ":0" + minute;
else
st += ":" + minute;
if (second < 10)
st += ":0" + second;
else
st += ":" + second;
// 在窗体上显示时间格式如:00:00:00
g.setColor(Color.pink);
g.fillRect(L, T, 60, 30);// 画出宽为60像素,高为30像素的矩形,颜色为粉色
g.setColor(Color.yellow);
g.drawString(st, L + 2, T + 26);// 将上面组成好的字符串st画出,颜色为黄色
// 计算时间与度数的关系
ss = 90 - second * 6;
mm = 90 - minute * 6;
hh = 90 - hour * 30 - minute / 2;
x_1 = r + 40 + L;
y_1 = r + 40 + T;
g2D.setStroke(new BasicStroke(1.0f));// 设置秒针的宽度
if (ss_x > 0) {// 擦除秒针
g.setColor(getBackground());
g.drawLine(x_1, y_1, ss_x, h - ss_y);
} else {
old_m = mm;
old_h = hh;
}
// 绘制秒针
x = (int) (r * 0.9 * Math.cos(RAD * ss)) + x_1;
y = (int) (r * 0.9 * Math.sin(RAD * ss)) + y_1 - 2 * T;
g.setColor(new Color(255, 99, 71));
g.drawLine(x_1, y_1, x, h - y);
ss_x = x;
ss_y = y;
g2D.setStroke(new BasicStroke(2.0f));// 设置分针的宽度
// 擦除分针
if (old_m != mm) {
g.setColor(getBackground());
g.drawLine(x_1, y_1, mm_x, h - mm_y);
}
// 绘制分针
x = (int) (r * 0.7 * Math.cos(RAD * mm)) + x_1;
y = (int) (r * 0.7 * Math.sin(RAD * mm)) + y_1 - 2 * T;
g.setColor(new Color(138, 43, 226));
g.drawLine(x_1, y_1, x, h - y);
mm_x = x;
mm_y = y;
old_m = mm;
g2D.setStroke(new BasicStroke(3.0f));// 设置时针的宽度
// 擦除时针
if (old_h != hh) {
g.setColor(getBackground());
g.drawLine(x_1, y_1, hh_x, h - hh_y);
}
// 绘制时针
x = (int) (r * 0.5 * Math.cos(RAD * hh)) + x_1;
y = (int) (r * 0.5 * Math.sin(RAD * hh)) + y_1 - 2 * T;
g.setColor(new Color(107, 142, 35));
g.drawLine(x_1, y_1, x, h - y);
hh_x = x;
hh_y = y;
old_h = hh;
}
public static void main(String[] args) {
Simple_Clock cd = new Simple_Clock();
}
}