📄 clock.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.Timer;
import javax.swing.UIManager;
import java.util.*;
interface TimerInterface {
void timeSpare(Timer t);
}
class SimpleClock extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
//Construct the frame
public SimpleClock() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(360, 360));
this.setTitle("时钟和日期");
contentPane.add(new Panel());
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
public class Clock {
boolean packFrame = false;
//Construct the application
public Clock() {
SimpleClock frame = new SimpleClock();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Clock();
}
}
class Panel extends JPanel implements TimerInterface{
private Calendar calendar;//用于获取当前时间
private String year,month1,day1,week1,hour,minute,second;
private int seconds;
int s,m ,h,offset=-350,a,i=1; //offset为X方向的偏移量
int week2,month2,day2;
int xcenter=180,ycenter=160; //定义时钟指针的旋转枢纽
BorderLayout borderLayout1 = new BorderLayout();
public Panel() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception { //初始化
this.setLayout(borderLayout1);
Timer t=new Timer(50,this);/////////////////////////////
t.start();
}
public void timeSpare(Timer t){
/**@todo Implement this simpleclock.TimerInterface abstract method*/
//本方法主要是实现获得系统当前时间,年,月,日,时,分,秒
calendar=new GregorianCalendar();
Date rightnow=new Date();
calendar.setTime(rightnow);
year = String.valueOf(calendar.get(Calendar.YEAR));
week2=calendar.get(Calendar.DAY_OF_WEEK);
month2=calendar.get(Calendar.MONTH);
day2=calendar.get(Calendar.DAY_OF_MONTH);
hour = String.valueOf(calendar.get(Calendar.HOUR));
h=calendar.get(Calendar.HOUR);
minute = String.valueOf(calendar.get(Calendar.MINUTE));
m=calendar.get(Calendar.MINUTE);
second = String.valueOf(calendar.get(Calendar.SECOND));
seconds=calendar.get(Calendar.HOUR)*60*60+calendar.get(Calendar.MINUTE)*60+calendar.get(Calendar.SECOND);
repaint();
}
protected void paintComponent(Graphics g) {
/**@todo Override this javax.swing.JComponent method*/
super.paintComponent(g);
//生成一个图片镜像,并显示背景图片
Image image=new ImageIcon("clock.jpg").getImage();
g.drawImage(image,0,0,null);
double secondAngle=2*Math.PI*seconds/60; //计算秒针与12点钟的方向
int [] minX={((int) (Math.cos(m * 3.14f/ 30 - 3.14f/ 2) * 98 + xcenter)),((int) (Math.cos(m * 3.14f/ 30) * (10) + xcenter)),((int) (Math.cos(m * 3.14f/ 30 + 3.14f* 5 / 12) * (15) + xcenter)),((int) (Math.cos(m * 3.14f/ 30 + 3.14f* 7 / 12) * (15) + xcenter)),((int) (Math.cos(m * 3.14f/ 30 - 3.14f) * (10) + xcenter))};
int [] minY={((int) (Math.sin(m * 3.14f/ 30 - 3.14f/ 2) * 98 + ycenter)),((int) (Math.sin(m * 3.14f/ 30) * (10) + ycenter)),((int) (Math.sin(m * 3.14f/ 30 + 3.14f* 5 / 12) * (15) + ycenter)),((int) (Math.sin(m * 3.14f/ 30 + 3.14f* 7 / 12) * (15) + ycenter)),((int) (Math.sin(m * 3.14f/ 30 - 3.14f) * (10) + ycenter))};
//构造时针五个点的坐标
int [] houX={((int) (Math.cos((h * 30 + m / 2) * 3.14f/ 180 - 3.14f/ 2) * 75 + xcenter)),((int) (Math.cos((h * 30 + m / 2) * 3.14f/ 180) * 12 + xcenter)),((int) (Math.cos((h * 30 + m / 2) * 3.14f/ 180 + 3.14f* 5/ 12) * 20 + xcenter)),((int) (Math.cos((h * 30 + m / 2) * 3.14f/ 180 + 3.14f* 7/ 12) * 20 + xcenter)),((int) (Math.cos((h * 30 + m / 2) * 3.14f/ 180 - 3.14f) * 12 + xcenter))};
int [] houY={((int) (Math.sin((h * 30 + m / 2) * 3.14f/ 180 - 3.14f/ 2) * 75 + ycenter)),((int) (Math.sin((h * 30 + m / 2) * 3.14f/ 180) * 12 + ycenter)),((int) (Math.sin((h * 30 + m / 2) * 3.14f/ 180 + 3.14f* 5/ 12) * 20 + ycenter)),((int) (Math.sin((h * 30 + m / 2) * 3.14f/ 180 + 3.14f* 7/ 12) * 20 + ycenter)),((int) (Math.sin((h * 30 + m / 2) * 3.14f/ 180 - 3.14f) * 12 + ycenter))};
//用yellow颜色填充分针
g.setColor(Color.YELLOW);
g.fillPolygon(minX,minY,minX.length); //画一个小圆以构造指针的枢纽
g.setColor(Color.GREEN); //用GREEN色填充时针
g.fillPolygon(houX,houY,houX.length);
g.setColor(Color.red); //画秒针
g.drawLine(180,160,180+(int)(90*Math.cos(secondAngle-Math.PI/2)),160+(int)(90*Math.sin(secondAngle-Math.PI/2)));
//构造分针五个点的坐标。
g.setColor(Color.black);
g.fillOval(175,160,10,10); //实现跑马灯
if (offset> 360) {//如果大于面板的宽度,则往左走
a = -3;
} else if (offset<= 0) {//如果小面板的宽度,则往右走
a = 3;
}
offset = offset + a;
Font f=new Font("New Roman",Font.BOLD,14);//设置字体的大小,类型
g.setFont(f);
switch(week2){ //把星期转化为大写表示
case 1:week1="日";break;
case 2:week1="一";break;
case 3:week1="二";break;
case 4:week1="三";break;
case 5:week1="四";break;
case 6:week1="五";break;
case 7:week1="六";break;
}
switch(month2){ //把月分转化为大写表示
case 0:month1="一";break;
case 1:month1="二";break;
case 2:month1="三";break;
case 3:month1="四";break;
case 4:month1="五";break;
case 5:month1="六";break;
case 6:month1="七";break;
case 7:month1="八";break;
case 8:month1="九";break;
case 9:month1="十";break;
case 10:month1="十一";break;
case 11:month1="十二";break;
}
switch(day2){ //把日转化为大写表示
case 1:day1="一";break;
case 2:day1="二";break;
case 3:day1="三";break;
case 4:day1="四";break;
case 5:day1="五";break;
case 6:day1="六";break;
case 7:day1="七";break;
case 8:day1="八";break;
case 9:day1="九";break;
case 10:day1="十";break;
case 11:day1="十一";break;
case 12:day1="十二";break;
case 13:day1="十三";break;
case 14:day1="十四";break;
case 15:day1="十五";break;
case 16:day1="十六";break;
case 17:day1="十七";break;
case 18:day1="十八";break;
case 19:day1="十九";break;
case 20:day1="二十";break;
case 21:day1="二十一";break;
case 22:day1="二十二";break;
case 23:day1="二十三";break;
case 24:day1="二十四";break;
case 25:day1="二十五";break;
case 26:day1="二十六";break;
case 27:day1="二十七";break;
case 128:day1="二十八";break;
case 29:day1="二十九";break;
case 30:day1="三十";break;
case 31:day1="三十一";break;
}
//把日期画在面板上
g.drawString(" ",140,100);
g.drawString(" ",123,125);
g.drawString("开始调试:07.1.8",130,225);
g.drawString("结束调试:07.1.13",130,245);
g.drawString("星期"+week1+", "+month1+"月"+day1+"日, "+year+"年 "+hour+": "+minute+": "+second, offset, 320); //写上时间文字
}
}
class Timer extends Thread {//定义一个timer类,并继承Thread
private int spare;
private TimerInterface ti;
public Timer(int i,TimerInterface t){
spare=i;
ti=t;
setDaemon(true);
}
public void run(){
try{
while(!interrupted()){
sleep(spare); //每1秒走一次
ti.timeSpare(this);
}
}catch(InterruptedException e){
}
}
public static void main(String[] args) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -