时钟代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1package com.jp.lianxi3;
2import java.awt.Color;
3import java.awt.Graphics;
4import java.util.Date;
5import java.util.Timer;
6import java.util.TimerTask;
7import javax.swing.JFrame;
8import javax.swing.JPanel;
9public class DrawClock extends JPanel{
10private int localX=150;
11private int localY=150;
12private int r=150;
13public DrawClock(int r){
14this.localX=this.localY=this.r=r;
15 }
16public DrawClock(){}
17public void paintComponent(Graphics g){
18super.paintComponent(g);
19 g.translate(250, 250);
20 drawO(g);
21 drawHand(g);
22 }
23public void drawO(Graphics g){//画表盘
24 g.drawOval(-localX, -localY, r*2, r*2);
25int a=0;
26for(int i=0;i<60;i++){
27double x,y;
28 x=Math.cos(Math.toRadians(a));
29 y=Math.sin(Math.toRadians(a));
30if(a%30==0)
31 g.drawLine((int)(0.8*x*r), (int)(0.8*y*r), (int)(x*r), (int)(y*r));
32else
33 g.drawLine((int)(0.9*x*r), (int)(0.9*y*r), (int)(x*r), (int)(y*r));
34 a+=6;
35 }
36 }
37public void drawHand(Graphics g){//画表针
38 Date date=new Date();
39int hour,minute,second;
40 hour=minute=second=90;
41//确定各个针的角度
42 hour-=date.getHours()*30;
43 minute-=date.getMinutes()*6;
44 second-=date.getSeconds()*6;
45
46 g.setColor(Color.RED);
47double x,y;
48 x=Math.cos(Math.toRadians(hour));
49 y=Math.sin(Math.toRadians(hour));
50 g.drawLine(0,0,(int)(0.5*r*x),-(int)(0.5*r*y));//画时针
51 x=Math.cos(Math.toRadians(minute));
52 y=Math.sin(Math.toRadians(minute));
53 g.drawLine(0,0,(int)(0.6*r*x),-(int)(0.6*r*y));//分针
54 x=Math.cos(Math.toRadians(second));
55 y=Math.sin(Math.toRadians(second));
56 g.drawLine(0,0,(int)(0.8*r*x),-(int)(0.8*r*y));//画秒针
57
g.drawString(date.getHours()+":"+date.getMinutes()+":"+date.getSeconds(), 20,
10);
58 }
59public void go(){
60 Timer t=new Timer();
61 t.schedule(new MyTimerTask(), 0, 1000);//定义定时器每一秒刷一次
62 }
63class MyTimerTask extends TimerTask{
64@Override
65public void run() {
66 repaint();
67 }
68
69 }
70public static void main(String[] args) {
71 DrawClock dc=new DrawClock(150);
72 JFrame f=new JFrame("时钟");
73 f.add(dc);
74 f.setSize(500,500);
75 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
76 f.setLocationRelativeTo(null);
77 f.setVisible(true);
78 dc.go();
79 }
80}
import java.awt.*;
import java.applet.Applet;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ClockApplet extends Applet implements Runnable //Applet支持线程{
private Thread athread; //线程
private SimpleDateFormat sdateformat; //日期格式
public void init()
{
this.setBackground(Color.white);
this.athread = null;
}
public void paint(Graphics g)
{
this.sdateformat = new SimpleDateFormat("hh时mm分ss秒");
g.drawString(this.sdateformat.format(new Date()),25,131);
Calendar rightnow = Calendar.getInstance();
int second = rightnow.get(Calendar.SECOND);
int minute = rightnow.get(Calendar.MINUTE);
int hour = rightnow.get(Calendar.HOUR);
//半径
int R_H = 20,R_M = 4,R_S = 4;
//时针的坐标
//x ====(9-3)[0-6] (3-9)[6-0]
//y ====(12-6)[0-6] (6-12)[6-0]
int H_x ;
int H_y;
//x
if(hour == 0)
{
hour = 12;
}
if( hour >= 3 && hour <= 9 )
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
if(hour > 9)
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
H_x = R_H*Math.abs(hour+3);
}
}
//y
if( hour >= 6 && hour <= 12 )
{
H_y = R_H*Math.abs(hour - 12); }
else
{
H_y = R_H*hour;
}
//分针的坐标
int M_x;
int M_y;
if(minute == 0)
{
minute = 60;
}
if( minute >= 15 && minute <= 45 )
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
if(minute > 45)
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
M_x = R_M*Math.abs(minute+15);
}
}
//y
if( minute >= 30 && minute < 60 )
{
M_y = R_M*Math.abs(minute - 60);
}
else
{
M_y = R_M*minute;
}
//秒针的坐标
int S_x;
int S_y;
if(second == 0)
{
second = 60;
}
if( second >= 15 && second <= 45 )
{
S_x = R_S*Math.abs(second - 45);
}
else
{
if(second > 45)
{
S_x = R_S*Math.abs(second - 45);
}
else
{
S_x = R_S*Math.abs(second+15);
}
}
//y
if( second >= 30 && second <= 60 )
{
S_y = R_S*Math.abs(second - 60);
}
else
{
S_y = R_S*second;
}
// g.drawString(String.valueOf(second),25,50); // g.drawString(String.valueOf(minute),25,60); // g.drawString(String.valueOf(hour),25,70);
// g.drawString(String.valueOf(H_x),25,80);
// g.drawString(String.valueOf(H_y),25,90); g.drawOval(0,0,120,120);//距离相差10像素
g.setColor(Color.darkGray);
g.drawString("9",5,65);
g.drawString("3",110,65);
g.drawString("12",55,15);
g.drawString("6",55,115);
g.drawString("1",80,20);
g.drawString("2",100,40);
g.drawString("4",100,90);
g.drawString("5",80,110);
g.drawString("7",30,110);
g.drawString("8",10,90);
g.drawString("10",10,40);
g.drawString("11",30,20);
g.setColor(Color.red);
g.drawLine(60,60,H_x,H_y);//前一个点表示起点,另一个表示终点
g.setColor(Color.blue);
g.drawLine(60,60,M_x,M_y);
g.setColor(Color.yellow);
g.drawLine(60,60,S_x,S_y);
}
public void start()
{
if(athread == null)
{
athread = new Thread(this);
athread.start();
}
}
public void stop()
{
if(athread != null)
{
athread.interrupt();
athread = null;
}
}
public void run()
{
while(athread != null)
{
repaint();
try
{
athread.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}
import java.awt.*;
import java.applet.*;
import java.util.Date;
import java.text.*;
public class Clockpanel extends Applet implements Runnable {
Date timenow;
Clock myClock;
Thread clockthread = null;
//初始化Applet
public void init()
{
setBackground(Color.white);
}
//启动线程
public void start()
{
if(clockthread == null)
{
clockthread = new Thread(this,"clockthread"); clockthread.start();
}
}
//终止线程
public void stop()
{
clockthread.stop();
clockthread = null;
}
//线程体
public void run()
{
Thread current = Thread.currentThread();
while(clockthread == current)
{
try
{
Thread.sleep(1000);//刷新间隔为1000毫秒
}
catch(InterruptedException e)
{}
repaint();
}
}
public void paint(Graphics g)
{
timenow = new Date();
myClock = new Clock(timenow.getHours(),timenow.getMinutes(),timenow.getSeconds());
//条形时钟面板
g.drawString(timenow.toString(),25,400);
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss a"); String msg = df.format(timenow);
g.clearRect(11,9,199,13);
g.drawRect(10,8,200,14);
g.drawString(msg,20,20);
//圆形时钟面板
myClock.show(g,100,200,100);
}
}
class Clock
{
int hour,minute,second;
Clock(int hrs, int min, int sec)//取时间
{
hour = hrs%12;
minute = min;
second = sec;
}
void show(Graphics g,int cx,int cy,int rad)//显示圆形时钟
{
int hrs_len = (int)(rad*0.5),min_len = (int)(rad*0.6),sec_len = (int)(rad*0.9); double theta;
int i;
//画时钟面板
g.drawOval(cx-rad,cy-rad,rad*2,rad*2);
for(i=1;i<=12;i++)
{
theta = (double)(i)/12*2.0*Math.PI;
int tx = (int)(cx + sec_len*Math.sin(theta));
int ty = (int)(cy - sec_len*Math.cos(theta));
g.drawString(""+i,tx,ty);
}
//画时、分、秒针
theta = (double)(hour*60*60 + minute*60 + second)/(12*3600.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,min_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);
}
}
/******************************************
*
*
import java.util.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
class Paintclock extends JPanel implements Runnable { //private JButton Setalarmclock,offalarmclock;
//private JButton b2;
//private JButton b3;
private Image backgroundImage;
public int alarmhour;
public int alarmminute;
public int alarmsecond;
MyClock mc = new MyClock();
Set st = new Set();
public Thread thread = null;
public int hour;
public int minute;
public int second;
public int year;
public int month;
public int day;
public boolean Isclock = false;
//Image clockimage;
int clockX;
int clockY;
int x = 128;
int y = 128;
public String[] Weekdays = new String[] { "星期日", "星期一", "星期二", "星期三",
"星期四", "星期五", "星期六" };
Image off1;
Graphics offG;
public Paintclock() {
backgroundImage= new ImageIcon("../hgfhg/chahua1.jpg").getImage();
/*
Setalarmclock = new JButton("设置闹钟");// 设置
this.setLayout(null);
this.add(Setalarmclock);
Setalarmclock.setBounds(75, 250, 100, 25);
Setalarmclock.setBorder(null);
Setalarmclock.setVisible(true);
Setalarmclock.setOpaque(true);
*/
//this.setBounds(300, 200, 10,; 300);
//this.setVisible(true);
} //关闭闹钟按钮事件
public void offalarmclockactionPerformed(ActionEvent e) {
Isclock =false;
MyClock mc = new MyClock();
mc.Onalarm();
}
//设置时间按钮事件
/*
Setalarmclock.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SetalarmclockActionPerformed(evt);
}
});
*/
//按钮事件
public void SetalarmclockActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
Isclock = true;
/*
* ****************************************************
* 设置闹铃时间界面
*
*
*
* ************************************************
* */
JLabel T;
final JTextField sethour;
final JTextField setminute;
final JTextField setsecond;
JButton setyes,setno;
final JFrame Setclock = new JFrame();
// JButton ce = new JButton("hjgjhgj");
//ce.setBounds(30, 60, 60, 60);
// ce.setVisible(true);
//ce.setBorder(null);
//Setclock.add(ce);
Setclock.setTitle("设置闹钟");
Setclock.setVisible(true);
Setclock.setSize(200, 200);
Setclock.setLocation(600, 400);
Setclock.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Setclock.setResizable(true);
//Paintsetclock Setpaint = new Paintsetclock();
//Setclock.add(Setpaint);
T= new JLabel("请设置时间:");
T.setBounds(10, 20, 100, 30);
T.setBorder(null);
T.setEnabled(true);
//T.setVisible(true);
Setclock.setLayout(null);
Setclock.add(T);
//时间设置
sethour = new JTextField("00");
setminute = new JTextField("00");
setsecond = new JTextField(" : ");
sethour.setBounds(50+10,50,20,20);
setminute.setBounds(80+10, 50, 20, 20);
setsecond.setBounds(68+10, 50, 20, 20);
sethour.setVisible(true);
setminute.setVisible(true);
setsecond.setVisible(true);
setsecond.setEditable(false);
Setclock.add(sethour);
Setclock.add(setminute);
Setclock.add(setsecond);
//按钮
setyes = new JButton("确定");
setyes.setBounds(30, 100,50, 30);
setyes.setBorder(null);
setyes.setVisible(true);
setno = new JButton("取消");
setno.setBounds(100, 100,50, 30);
setno.setBorder(null);
setno.setVisible(true);
Setclock.add(setno);
Setclock.add(setyes);
//按钮事件
setyes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Isclock =true;
alarmhour= Integer.valueOf(sethour.getText());
alarmminute= Integer.valueOf(setminute.getText());
// alarmsecond = Integer.valueOf(setsecond.getText());
Paintclock pc = new Paintclock();
pc.show();
Setclock.dispose();
MyClock mc = new MyClock();
mc.Offalarm();
// pc.Setalarmclock.setVisible(false);//隐藏设置时间按钮
// pc.offalarmclock.setVisible(true);//显示关闭按钮
System.out.println("闹钟时间"+alarmhour+":"+alarmminute);
}
});/* */
setno.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Isclock =false;
Setclock.dispose();
}
});
}
//双数保证
// 线程启动
public void start() {
if (thread == null) {
//show();
thread = new Thread(this);
thread.start();
}
}
public void run() {
while (thread != null) {
//if(mc.alarmhour == hour|| mc.alarmminute == minutes||mc.alarmsecond==second)break;
repaint();
if(Isclock ){
if(alarmhour >9&&alarmminute <= 9)
mc.alarmtimeon.setText("(开) 闹钟:"+ alarmhour+" : "+'0'+alarmminute);
else if(alarmhour <=9&&alarmminute > 9)
mc.alarmtimeon.setText("(开) 闹钟:"+'0'+ alarmhour+" : "+alarmminute);
else if(alarmhour <=9&&alarmminute <= 9)
mc.alarmtimeon.setText("(开) 闹钟:"+'0'+ alarmhour+" : "+'0'+alarmminute);
else
mc.alarmtimeon.setText("(开) 闹钟:"+ alarmhour+" : "+alarmminute);
/*JTextField alarmtimeoff = new JTextField("(开) 闹钟:"+ "-"+" : "+"-");
alarmtimeoff.setVisible(true);
alarmtimeoff.setBounds(80, 220,200, 30);
alarmtimeoff.setBorder(null);
this.add(alarmtimeoff);*/
if(alarmhour== this.hour&&alarmminute==this.minute&& this.second == 0){
/*
* ************************************************************
*
* 时间到弹出界面
*
*
*
*
*
* **********************************************************
*/
final JFrame alarmjf = new JFrame("闹钟");
JLabel message = new JLabel("时间到!是时候展现真正的技术了!");
JButton Myes = new JButton("知道了");
JButton Mno = new JButton("关闭");
//message.setVisible(true);
//message.setEditable(false);
Myes.setVisible(true);
Mno.setVisible(true);
alarmjf.setVisible(true);
Myes.setLayout(null);
Mno.setLayout(null);
alarmjf.setLayout(null);
message.setBounds(50, 10, 250, 20);
Myes.setBounds(40, 40, 80, 30);
Mno.setBounds(200,40,80,30 );
alarmjf.setLocation(600, 300);
alarmjf.setSize(320, 100);
alarmjf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
alarmjf.setResizable(false);
alarmjf.add(message);
alarmjf.add(Myes);
alarmjf.add(Mno);
Myes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Isclock =false;
alarmjf.dispose();
}
});
Mno.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Isclock =false;
alarmjf.dispose();
}
});
//JOptionPane.showMessageDialog(null, "闹钟时间已到", "闹钟",RMATION_MESSAGE);
Isclock =false;
}
}
else {
}
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
//绘图
public void paint(Graphics g1) {
super.paint(g1);
Graphics2D g = (Graphics2D) g1;
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
g.setColor(Color.DARK_GRAY);
// 绘制表盘
int radius = 70;
g.setFont(new Font("Curier", 10, 15));
g.drawString("12", x - 8, y - 74);
g.drawString("3", x + radius + 4, y + 6);
g.drawString("6", x - 3, y + radius + 16);
g.drawString("9", x - radius - 12, y + 6);
int Sintemp = (int) (radius * Math.sin(Math.PI / 6)) - 2;
int Costemp = (int) (radius * Math.cos(Math.PI / 6)) - 2;
g.drawString("1", x + Sintemp - 1, y - Costemp - 6);
g.drawString("2", x + Costemp + 4, y - Sintemp - 2);
g.drawString("4", x + Costemp + 4, y + Sintemp + 12);
g.drawString("5", x + Sintemp, y + Costemp + 20);
g.drawString("7", x - Sintemp - 10, y + Costemp + 20);
g.drawString("8", x - Costemp - 14, y + Sintemp + 12);
g.drawString("10", x - Costemp - 20, y - Sintemp - 2);
g.drawString("11", x - Sintemp - 14, y - Costemp - 6);
g.setColor(Color.RED);
g.fillOval(x - 4, y - 4, 8, 8);
for (int i = 0; i < 60; i++) {
clockX = (int) (70 * Math.sin(Math.PI / 30 * i)) + x;
clockY = (int) (y - 70 * Math.cos(Math.PI / 30 * i));
if (i % 5 == 0) {
g.setColor(Color.black);
g.fillOval(clockX - 3, clockY - 3, 6, 6);
} else {
g.setColor(Color.cyan);
g.fillOval(clockX - 2, clockY - 2, 4, 4);
}
}
Calendar calCurrent = Calendar.getInstance();
// 获取当前时间
int weekday = calCurrent.get(Calendar.DAY_OF_WEEK);
day = calCurrent.get(Calendar.DATE);
month = calCurrent.get(Calendar.MONTH) + 1;
year = calCurrent.get(Calendar.YEAR);
hour = calCurrent.get(Calendar.HOUR_OF_DAY);
minute = calCurrent.get(Calendar.MINUTE);
second = calCurrent.get(Calendar.SECOND);
int hourX = (int) (Math.sin(Math.PI / 6.0
* (hour % 12 + (float) minute / 60)) * 35)
+ x;
int hourY = y
- (int) (Math.cos(Math.PI / 6.0
* (hour % 12 + (float) minute / 60)) * 35);
int secondX = (int) (Math.sin(Math.PI / 30.0 * second) * 70)
+ x;
int secondY = y - (int) (Math.cos(Math.PI / 30.0 * second) * 70);
int minutesX = (int) (Math.sin(Math.PI / 30.0 * minute) * 55)
+ x;
int minutesY = y - (int) (Math.cos(Math.PI / 30.0 * minute) *
55);
g.setColor(Color.gray);
g.setFont(getFont());
if( month<=9 & day >9)
g.drawString("日期:" + year + "-" +'0'+ month + "-" + day + " "
+ Weekdays[weekday - 1], 60, 15);
else if( month>9 && day <=9)
g.drawString("日期:" + year + "-" + month + "-"+'0' + day
+ " "
+ Weekdays[weekday - 1], 60, 15);
if(hour>9&& minute>9 & second >9)
g.drawString("时间: "+ hour+ ":"+ minute+ ":"+ second, 60,
30);
else if(hour<=9&& minute>9 & second >9)
g.drawString("时间: " +'0'+ hour + ":" + minute + ":" + second, 60, 30);
else if(hour>9&& minute<=9 & second >9)
g.drawString("时间: " + hour + ":" +'0'+ minute + ":" + second, 60, 30);
else if(hour>9&& minute>9 & second <=9)
g.drawString("时间: " + hour + ":" + minute + ":" +'0'+ second, 60, 30);
// g.setStroke(new
//
BasicStroke(3.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
// 秒针
g.setColor(Color.green);
g.drawLine(x, y, secondX, secondY);
// 时针
g.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND));
g.setColor(Color.red);
g.drawLine(x, y, hourX, hourY);
// 分针
g.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND));
g.setColor(Color.blue);
g.drawLine(x, y, minutesX, minutesY);
}
}
//
public class MyClock {
public static JButton Setalarmclock,offalarmclock;
public static JTextField alarmtimeon;
public static Paintclock panel = new Paintclock();
protected void Onalarm(){
Setalarmclock.setVisible(true);
offalarmclock.setVisible(false);
}
protected void Offalarm(){
Setalarmclock.setVisible(false);
offalarmclock.setVisible(true);
}
public static void main(String args[]) {
/*
*
***************************************************************** * 初始化时钟界面
*
*
*
*
* *************************************************************** * */
///Setalarmclock.setOpaque(true);
JFrame clock = new JFrame();
clock.add(alarmtimeon);
clock.add(Setalarmclock);
clock.add(offalarmclock);
clock.setTitle("钟表");
clock.add(panel);
panel.start();
clock.setVisible(true);
clock.setSize(270, 350);
clock.setLocation(500, 250);
clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clock.setResizable(true);
}
public MyClock(){
Setalarmclock = new JButton("设置闹钟");// 设置
Setalarmclock.setBounds(75, 250, 100, 25);
//Setalarmclock.setBorder(null);
//Setalarmclock.setVisible(true);
offalarmclock = new JButton("关闭闹钟");
offalarmclock.setBounds(75, 250, 100, 25);
offalarmclock.setBorder(null);
//offalarmclock.setVisible(false);
alarmtimeon = new JTextField("(关) 闹钟:"+ "-"+" : "+"-");
alarmtimeon.setVisible(true);
alarmtimeon.setBounds(80, 220,200, 30);
alarmtimeon.setOpaque(false);
alarmtimeon.setLayout(null);
alarmtimeon.setEditable(false);
alarmtimeon.setBorder(null);
//按钮事件
Setalarmclock.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
panel.SetalarmclockActionPerformed(e);
}
});
offalarmclock.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
panel.offalarmclockactionPerformed(e);
}
});
}
}。