📄 jappletclock.java
字号:
package operation;
import java.awt.*;
import java.applet.*;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JLabel; //这是Java中的低级实用工具包,可以处理时间等内容。
/**
* <p>Title: 山海假日酒点信息管理系统</p>
*
* <p>Description: 小型酒店信息管理软件</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: FZ编程小组</p>
*
* @author not attributable
* @version 1.0
*/
public class JAppletClock extends Applet implements Runnable
{
ImageIcon i4 = new ImageIcon("image\\APclock2.jpg");
JLabel lblAp2 = new JLabel();
public JAppletClock() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
} //有线程运行接口
Date timenow; //Date是一个时间定义与创建函数.
Clock myClock; //用户自定义的类
Thread clockthread = null; //设置一个线程
public void start()
{ //线程开始的类
if (clockthread == null)
{ //如果线程为空,则
clockthread = new Thread(this); //开始新的线程
clockthread.start(); //开始
}
}
public void stop()
{ //终止线程
clockthread.stop(); //终止线程,使它
clockthread = null; //为空
}
public void run()
{ //运行线程
while (true)
{ //一个死循环,条件永远都是真的。
repaint(); //重新绘制界面
try
{
Thread.sleep(1000);
} //让线程沉睡1000毫秒,也就是一秒钟
catch (InterruptedException e)
{
e.printStackTrace();
} //捕获异常(也就是错误)
}
}
public void paint(Graphics g)
{
timenow = new Date(); //新的时间的获得
//获得小时,分钟,秒钟
myClock = new Clock(timenow.getHours(),
timenow.getMinutes(),
timenow.getSeconds());
g.drawString(timenow.toString(), 25, 240); //将它打印出来!
myClock.show(g, 100, 100, 100); //使面板显示
}
private void jbInit() throws Exception {
this.lblAp2.setIcon(i4);
this.lblAp2.setBounds(0,0,100,100);
this.lblAp2.show();
this.add(lblAp2);
}
}
class Clock
{ //用户自定义的类开始,编译后,它单独成为一个CLASS文件
Clock(int hrs, int min, int sec)
{ //类函数入口
hour = hrs % 12; //将原始数据处理,得到小时
minute = min; //将原始数据处理,得到分钟
second = sec; //将原始数据处理,得到小时
}
void show(Graphics g, int cx, int cy, int rad)
{ //重新定义SHOW函数
int hrs_len = (int) (rad * 0.5), //时针的长度
min_len = (int) (rad * 0.6), //分钟的长度
sec_len = (int) (rad * 0.9); //秒钟的长度
double theta;
//画出钟面
g.drawOval(cx - rad, cy - rad, rad * 2, rad * 2);
//画出时针
theta = (double) (hour * 60 * 60 + minute * 60 + second) / 43200.0 *
2.0 *
Math.PI;
drawNiddle(g, Color.blue, cx, cy, hrs_len, theta);
//画出分针
theta = (double) (minute * 60 + second) / 3600.0 * 2.0 * Math.PI;
drawNiddle(g, Color.red, cx, cy, sec_len, theta);
//画出秒针
theta = (double) (second) / 60.0 * 2.0 * Math.PI;
drawNiddle(g, Color.green, cx, cy, sec_len, theta);
}
private void drawNiddle(Graphics g, Color c, int x, int y, int len,
double theta)
{
int ex = (int) (x + len * Math.sin(theta));
int ey = (int) (y - len * Math.cos(theta));
g.setColor(c);
g.drawLine(x, y, ex, ey);
}
int hour, minute, second;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -