java实现的小时钟示例分享
//package com.clock;
import java.awt.basicstroke;
import java.awt.color;
import java.awt.font;
import java.awt.graphics;
import java.awt.graphics2d;
import java.awt.insets;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.windowadapter;
import java.awt.event.windowevent;
import java.util.calendar;
import java.util.gregoriancalendar;
import javax.swing.*;
import javax.swing.jframe;
public class clock extends jframe implements actionlistener{
private final double rad=math.pi/180;
public clock(){
super("clock");
setsize(400, 400);
setlocation(400, 200);
setvisible(true);
setresizable(true);
setbackground(color.white);
addwindowlistener(new windowadapter() {
@override
public void windowclosing(windowevent e) {
system.exit(0);
}
});
actionlistener drawclock=new actionlistener() {
@override
public void actionperformed(actionevent e) {
repaint();
}
};
new timer(1000,drawclock).start();
}
@override
public void actionperformed(actionevent e) {}
public void paint(graphics g){
graphics2d g2=(graphics2d)g;
insets insert=getinsets();
int h=getsize().height;
g2.setstroke(new basicstroke(3.0f));
g.drawoval(40,40, h-80,h-80);
int r=200;
int r1=140;
int initrad=60;
for(int i=1;i<=5;i++){
int x0= (int) (r+r1*math.cos(rad*initrad));
int y0= (int) (r-r1*math.sin(rad*initrad));
g.setfont(new font("楷体", font.bold, 20));
g.drawstring(""+i, x0, y0);
initrad-=30;
}
int r2=150;
int initrad2=-120;
for(int i=7;i<=11;i++){
int x0= (int) (r+r2*math.cos(rad*initrad2));
int y0= (int) (r-r2*math.sin(rad*initrad2));
g.setfont(new font("楷体", font.bold, 20));
g.drawstring(""+i, x0, y0);
initrad2-=30;
}
g.drawstring("6", 190, 350);
g.drawstring("12", 190, 60);
calendar now=new gregoriancalendar();
int hour=now.get(calendar.hour_of_day);
int minute=now.get(calendar.minute);
int second=now.get(calendar.second);
string time;
if(hour<10){time="0"+hour;}else{time=""+hour;}
if(minute<10){time+=":0"+minute;}else{time+=":"+minute;}
if(second<10){time+=":0"+second;}else{time+=":"+second;}
g.setcolor(color.white);
g.fillrect(10, 30, 90, 28);
g.setcolor(color.red);
g.drawstring(time, 10, 50);
int s=90-second*6;
int m=90-minute*6;
int h=90-hour*30-minute/2;
g2.setstroke(new basicstroke(1.0f));
int x=(int) (200+130*math.cos(rad*s));
int y=(int) (200-130*math.sin(rad*s));
int ss=90-(second-1)*6;
int xx=(int) (200+130*math.cos(rad*ss));
int yy=(int) (200-130*math.sin(rad*ss));
g.setcolor(getbackground());
g.drawline(200, 200, xx, yy);
g.setcolor(color.black);
g.drawline(200, 200, x, y);
g2.setstroke(new basicstroke(2.2f));
int x1=(int) (200+110*math.cos(rad*m));
int y1=(int) (200-110*math.sin(rad*m));
int mm=90-(minute-1)*6;
int x11=(int) (200+110*math.cos(rad*mm));
int y11=(int) (200-110*math.sin(rad*mm));
g.setcolor(getbackground());
g.drawline(200, 200, x11, y11);
g.setcolor(color.blue);
g.drawline(200, 200, x1, y1);
g2.setstroke(new basicstroke(4.3f));
int xx1=(int) (200+90*math.cos(rad*h));
int yy1=(int) (200-90*math.sin(rad*h));
int hh=90-(hour-1)*30-minute/2;;
int xxx1=(int) (200+90*math.cos(rad*hh));
int yyy1=(int) (200-90*math.sin(rad*hh));
g.setcolor(color.white);
g.drawline(200, 200, xxx1, yyy1);
g.setcolor(color.green);
g.drawline(200, 200, xx1, yy1);
}
public static void main(string[] args) {
new clock();
}
}