import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.Timer;
import javax.swing.JApplet;
import javax.swing.JPanel;
public class ClockPanel extends JPanel implements ActionListener{
// JPanel(LayoutManager flowLayout);
// FlowLayout mylayout=new FlowLayout();
// FlowLayout exLayout=new FlowLayout(FlowLayou.LEFT);
// setLayout(exLayout);
private int hour;
private int minute;
private int second;
private int year;
ClockPanel()
{
// Timer timer=new Timer(1000,this);
// timer.start();
//方法1,每秒响应
int delay=1000;
ActionListener taskPerformer=new ActionListener(){
public void actionPerformed(ActionEvent e){
repaint();
}
};
new Timer(delay,taskPerformer).start();
//方法2,每秒响应
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
setCurrentTime();
int clockRadius=55; //圆的半径
int xCenter=70;
int yCenter=55;
g.drawOval(15, 0, 2*clockRadius, 2*clockRadius);
//画一个可以包含矩形的圆,前2个参数是坐标值,后面2个参数是矩形的长和宽
g.drawString("12", xCenter-7, yCenter-clockRadius+12);
g.drawString("9",xCenter-clockRadius+3,yCenter+5);
g.drawString("3", xCenter+clockRadius-10, yCenter+5);
g.drawString("6", xCenter-3, yCenter+clockRadius-3);
g.drawString("1", xCenter+20, yCenter-clockRadius+20);
g.drawString("2", xCenter+38, yCenter-clockRadius+38); //表盘上的12个数字,后面2个参数是坐标
g.drawString("4", xCenter+40, yCenter+clockRadius-25);
g.drawString("5", xCenter+20, yCenter+clockRadius-10);
g.drawString("7", xCenter-28, yCenter+clockRadius-10);
g.drawString("8", xCenter-45, yCenter+clockRadius-25);
g.drawString("10", xCenter-45, yCenter-clockRadius+38);
g.drawString("11", xCenter-30, yCenter-clockRadius+20);
int sLength=(int)(clockRadius*0.8);//秒针的长度
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);//画一条线,前面2个参数是坐标值,后面2个参数表示线的2个端点 ps:绘制一条连接由坐标对指定的两个点的线条。
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 hLength=(int)(clockRadius*0.4);
int xHour=(int)(xCenter+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12)));
int yHour=(int)(yCenter+hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12)));
g.setColor(Color.BLACK);
g.drawLine(xCenter,yCenter,xHour,yHour);
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss E");
g.drawString(sdf.format(d),20,150);
}
private void setCurrentTime() {
// TODO Auto-generated method stub
//用GregorianCalendar获得当前时间
Calendar cal=new GregorianCalendar();
this.hour=cal.get(Calendar.HOUR_OF_DAY);
this.minute=cal.get(Calendar.MINUTE);
this.second=cal.get(Calendar.SECOND);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
repaint();
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JFrame;
//import javax.swing.JApplet;
//import javax.swing.JFrame;