import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Clock extends JFrame{
public
Clock() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Global.FRAMEWIDTH
, Global.FRAMEHEIGHT);
this.setTitle("My
Clock");
this.setLocationRelativeTo(null);
this.add(new
ClockPanel() , BorderLayout.CENTER);
}
public
static void main(String[] args) {
//
TODO Auto-generated method stub
new
Clock().setVisible(true);
}
}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JPanel;
public class ClockPanel extends JPanel{
private
GregorianCalendar now ;
private
int hour , minute , second ;
public
ClockPanel() {
now
= new GregorianCalendar();
hour
= now.get(Calendar.HOUR);
minute
= now.get(Calendar.MINUTE);
second
= now.get(Calendar.SECOND);
//
线程启动
getNowTimeThread();
}
public
void paintComponent(Graphics g) {
Graphics2D
g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new
BasicStroke(2f));
//
绘制表盘
clockCircle(g2);
//
绘制时针
drawHour(g2);
//
绘制分针
drawMinute(g2);
//
绘制秒针
clockSecond(g2);
//
绘制表盘的中心点
g2.setColor(Color.RED);
Ellipse2D
circle = new Ellipse2D.Double(Global.CX-4, Global.CY-4, 8, 8);
g2.fill(circle);
}
private
void clockCircle(Graphics2D g) {
super.paintComponent(g);
//
表盘一圈360度,每间隔step画一个点
for(int
i = 0 ;i < 360; i+= Global.STEP) {
double
x , y;
x
= Global.CX + (Global.CLOCKR) * Math.cos(Math.toRadians(i - 90));
y
= Global.CY + (Global.CLOCKR) * Math.sin(Math.toRadians(i - 90));
Ellipse2D
circle ;
if(i
% (360/12) == 0) {
g.setColor(Color.RED);
circle
= new Ellipse2D.Double(x-3, y-3, 6, 6);
}
else
{
g.setColor(Color.BLUE);
circle
= new Ellipse2D.Double(x-2, y-2, 4, 4);
}
g.fill(circle);
}
}
//
绘制秒针
private
void clockSecond(Graphics2D g) {
//
起点坐标
double
x0 , y0;
x0
= Global.CX + 15 * Math.cos(Math.toRadians(Global.STEP * second + 90));
y0
= Global.CY + 15 * Math.sin(Math.toRadians(Global.STEP * second + 90));
//
终点坐标
double
x , y;
x
= Global.CX + Global.SECONDR * Math.cos(Math.toRadians(Global.STEP * second - 90));
y
= Global.CY + Global.SECONDR * Math.sin(Math.toRadians(Global.STEP * second - 90));
Line2D
line = new Line2D.Double(x0, y0, x, y);
g.setStroke(new
BasicStroke(3f));
g.setColor(Color.BLUE);
g.draw(line);
}
//绘制分针
public
void drawMinute(Graphics2D g) {
int
x1 = (int) (Global.CX + Global.MINUTER*Math.cos(Math.toRadians(minute*6-90)));
int
x2 = (int) (Global.CX + 5*Math.cos(Math.toRadians(minute*6)));
int
x3 = (int) (Global.CX + 10*Math.cos(Math.toRadians(minute*6-270)));;
int
x4 = (int) (Global.CX + 5*Math.cos(Math.toRadians(minute*6-180)));
int
y1 = (int) (Global.CY + Global.MINUTER*Math.sin(Math.toRadians(minute*6-90)));
int
y2 = (int) (Global.CY + 7*Math.sin(Math.toRadians(minute*6)));
int
y3 = (int) (Global.CY + 10*Math.sin(Math.toRadians(minute*6-270)));
int
y4 = (int) (Global.CY + 7*Math.sin(Math.toRadians(minute*6-180)));
int[]
xPoints = {x1 , x2 , x3 , x4};
int[]
yPoints = {y1 , y2 , y3 , y4};
g.setColor(Color.LIGHT_GRAY);
g.fillPolygon(xPoints,
yPoints, 4);
g.setColor(Color.BLACK);
g.drawPolygon(xPoints,
yPoints, 4);
}
//
绘制时针
public
void drawHour(Graphics2D g) {
int
x1 = (int)
(Global.CX + Global.HINUTER*Math.cos(Math.toRadians(hour*30-90 + minute * 6/12)));
int
x2 = (int)
(Global.CX + 5*Math.cos(Math.toRadians(hour*30 + minute * 6/12)));
int
x3 = (int) (Global.CX + 10*Math.cos(Math.toRadians(hour*30-270 + minute * 6/12)));;
int
x4 = (int)
(Global.CX + 5*Math.cos(Math.toRadians(hour*30-180 + minute * 6/12)));
int
y1 = (int) (Global.CY + Global.HINUTER*Math.sin(Math.toRadians(hour*30-90 + minute * 6/12)));
int
y2 = (int) (Global.CY + 7*Math.sin(Math.toRadians(hour*30 + minute * 6/12)));
int
y3 = (int) (Global.CY + 10*Math.sin(Math.toRadians(hour*30-270 + minute * 6/12)));
int
y4 = (int) (Global.CY + 7*Math.sin(Math.toRadians(hour*30-180 + minute * 6/12)));
int[]
xPoints = {x1 , x2 , x3 , x4};
int[]
yPoints = {y1 , y2 , y3 , y4};
g.setColor(Color.LIGHT_GRAY);
g.fillPolygon(xPoints,
yPoints, 4);
g.setColor(Color.BLACK);
g.drawPolygon(xPoints,
yPoints, 4);
}
//
时钟时分秒变化的线程
public
void getNowTimeThread() {
new
Thread() {
public
void run() {
while(true)
{
second++;
if(second
> 59) {
minute
+= 1;
second
= 0;
}
if(minute
> 59) {
hour
+= 1;
minute
= 0;
}
if(hour
> 24) {
hour
= 0;
}
repaint();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}
public
class Global {
//
设置一下影响窗体大小和时钟大小相关参数
public
static final int MARGINTOP = 30;
public
static final int MARGINLEFT = 30;
public
static final int CLOCKR = 150;
public
static final int SECONDR = CLOCKR - 10;
public
static final int MINUTER = CLOCKR - 10 - 15;
public
static final int HINUTER = CLOCKR - 10 - 15 * 2;
public
static final int STEP = 360 / 60;
public
static final int CX = MARGINLEFT + CLOCKR;
public
static final int CY = MARGINTOP +
CLOCKR;
public
static final int FRAMEWIDTH = CX * 2 + 8;
public
static final int FRAMEHEIGHT = CY * 2 + 30;
}