📄 stillclock.java
字号:
import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
public class StillClock extends JPanel
{
protected TimeZone timeZone;
protected int xCenter,yCenter;
protected int clockRadius;
protected DateFormat formatter;
public StillClock()
{
this(Locale.getDefault(),TimeZone.getDefault());
}
public StillClock(Locale locale,TimeZone timeZone)
{
setLocale(locale);
this.timeZone=timeZone;
}
//当地区变化时用来设置local属性
public void setLocaleIn(Locale locale)
{
setLocale(locale);
}
public void setTimeZoneID(String newTimeZoneID)
{
timeZone=TimeZone.getTimeZone(newTimeZoneID);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
clockRadius=(int)(Math.min(getSize().width,getSize().height)*0.7*0.5);
xCenter=(getSize().width)/2;
yCenter=(getSize().height)/2;
//draw Circle
g.setColor(Color.black);
g.drawOval(xCenter-clockRadius,yCenter-clockRadius,2*clockRadius,2*clockRadius);
g.drawString("12",xCenter-5,yCenter-clockRadius);
g.drawString("6",xCenter-3,yCenter+clockRadius+10);
g.drawString("9",xCenter-clockRadius-10,yCenter+3);
g.drawString("3",xCenter+clockRadius,yCenter+3);
//get Current time using GregorianCalendar
GregorianCalendar cal=new GregorianCalendar(timeZone);
//draw second minute hour hand
int second=(int)cal.get(GregorianCalendar.SECOND);
//System.out.println(cal.get(GregorianCalendar.SECOND));
int sLength=(int)(clockRadius*0.9);
int xSecond=(int)(xCenter+sLength*Math.sin(second*(2*Math.PI/60)));
int ySecond=(int)(yCenter-sLength*Math.cos(second*(2*Math.PI/60)));
g.setColor(Color.red);
g.drawLine(xCenter,yCenter,xSecond,ySecond);
//箭头
int xx1Second=(int)(xCenter+0.8*sLength*Math.sin(second*(2*Math.PI/60)));
int yy1Second=(int)(yCenter-0.9*sLength*Math.cos(second*(2*Math.PI/60)));
g.drawLine(xSecond,ySecond,xx1Second,yy1Second);
int xx2Second=(int)(xCenter+0.9*sLength*Math.sin(second*(2*Math.PI/60)));
int yy2Second=(int)(yCenter-0.8*sLength*Math.cos(second*(2*Math.PI/60)));
g.drawLine(xSecond,ySecond,xx2Second,yy2Second);
//minute
int minute=(int)cal.get(GregorianCalendar.MINUTE);
//System.out.println(cal.get(GregorianCalendar.MINUTE));
int mLength=(int)(clockRadius*0.6);
int xMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)));
int yMinute=(int)(yCenter-mLength*Math.cos(minute*(2*Math.PI/60)));
g.setColor(Color.blue);
g.drawLine(xCenter,yCenter,xMinute,yMinute);
//箭头
int xx1Minute=(int)(xCenter+0.8*mLength*Math.sin(minute*(2*Math.PI/60)));
int yy1Minute=(int)(yCenter-0.9*mLength*Math.cos(minute*(2*Math.PI/60)));
g.drawLine(xMinute,yMinute,xx1Minute,yy1Minute);
int xx2Minute=(int)(xCenter+0.9*mLength*Math.sin(minute*(2*Math.PI/60)));
int yy2Minute=(int)(yCenter-0.8*mLength*Math.cos(minute*(2*Math.PI/60)));
g.drawLine(xMinute,yMinute,xx2Minute,yy2Minute);
//hour
int hour=(int)cal.get(GregorianCalendar.HOUR);
//System.out.println(cal.get(GregorianCalendar.HOUR));
int hLength=(int)(clockRadius*0.4);
int xHour=(int)(xCenter+hLength*Math.sin(hour*(2*Math.PI/12)));
int yHour=(int)(yCenter-hLength*Math.cos(hour*(2*Math.PI/12)));
g.setColor(Color.green);
g.drawLine(xCenter,yCenter,xHour,yHour);
//箭头
int xx1Hour=(int)(xCenter+0.8*hLength*Math.sin(hour*(2*Math.PI/12)));
int yy1Hour=(int)(yCenter-0.9*hLength*Math.cos(hour*(2*Math.PI/12)));
g.drawLine(xHour,yHour,xx1Hour,yy1Hour);
int xx2Hour=(int)(xCenter+0.9*hLength*Math.sin(hour*(2*Math.PI/12)));
int yy2Hour=(int)(yCenter-0.8*hLength*Math.cos(hour*(2*Math.PI/12)));
g.drawLine(xHour,yHour,xx2Hour,yy2Hour);
//format
formatter=DateFormat.getDateTimeInstance (DateFormat.MEDIUM,DateFormat.LONG,getLocale());
formatter.setTimeZone(timeZone);
//display the current time
g.setColor(Color.red);
String today=formatter.format(cal.getTime());
FontMetrics fm=g.getFontMetrics();
g.drawString(today,(getSize().width-fm.stringWidth(today))/2,yCenter+clockRadius+30);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -