利用Java的GUI和多线程编程技术,编写一个时钟显示程序
package test1;
import java.awt.Graphics;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Jdbc extends JFrame {
public Jdbc()
{
ClockPanel jp_clock=new ClockPanel();
add(jp_clock);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
this.setLocationRelativeTo(null);
setVisible(true);
Thread ClockRun=new Thread(jp_clock);
ClockRun.start();
}
public static void main(String[] args) {
// TODO Auto-generatedmethod stub
new Jdbc();
}
}
class ClockPanel extends JPanel implements Runnable
{
private double CenterX,CenterY;
private double Radius=150;
private double HPointX,HPointY,MPointX,MPointY,SPointX,SPointY;
private int Year,Month,Day,WeekDay,Hour,Minute,Second;
private final double PI=3.1415926;
public synchronized void paintComponent(Graphics g)
{
super.paintComponent(g);
CenterX=this.getWidth()/2;
CenterY=this.getHeight()/2;
double HRadian=(PI/6)*Hour+(PI/360)*Minute+(PI/21600)*Second;
double MRadian=(PI/30)*Minute+(PI/1800)*Second;
double SRadian=(PI/30)*Second;
HPointX=CenterX+Radius*0.6*Math.sin(HRadian);
HPointY=CenterY-Radius*0.6*Math.cos(HRadian);
MPointX=CenterX+Radius*0.8*Math.sin(MRadian);
MPointY=CenterY-Radius*0.8*Math.cos(MRadian);
SPointX=CenterX+Radius*Math.sin(SRadian);
SPointY=CenterY-Radius*Math.cos(SRadian);
g.drawOval((int)(CenterX-Radius), (int)(CenterY-Radius), (int)(2*Radius),(int)(2*Radius));//椭圆
g.drawString("12",(int)(CenterX),(int)(CenterY-Radius)+10);//字符串
g.drawString("3",(int)(CenterX+Radius)-6 , (int)(CenterY));
g.drawString("6", (int)(CenterX), (int)(CenterY+Radius));
g.drawString("9",(int)(CenterX-Radius), (int)(CenterY));
g.drawLine((int)(CenterX), (int)(CenterY), (int)(SPointX), (int)(SPointY));//画线
g.drawLine((int)(CenterX), (int)(CenterY), (int)(MPointX), (int)(MPointY));
g.drawLine((int)(CenterX), (int)(CenterY), (int)(HPointX), (int)(HPointY));
g.drawString(WeekDay+" "+Month+" "+Day+" "+Hour+":"+Minute+":"+Second+" "+Year, 20, 350);
}
@Override
public void run() {
// TODO Auto-generatedmethod stub
while(true)
{
try {
setCurrentTime();
repaint();
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void setCurrentTime()
{
Calendar calendar=new GregorianCalendar();
Year=calendar.get(Calendar.YEAR);
Month=calendar.get(Calendar.MONTH);
Day=calendar.get(Calendar.DAY_OF_MONTH);
WeekDay=calendar.get(Calendar.DAY_OF_WEEK);
Hour=calendar.get(Calendar.HOUR);
Minute=calendar.get(Calendar.MINUTE);
Second=calendar.get(Calendar.SECOND);
}
}
关于数字时间向图像时针转化我个人认为是难点,在这里着重讲解一下
一个 π 是180° HRadian=(PI/6)*Hour+(PI/360)*Minute+(PI/21600)*Second; PI是π的近似值 PI/6 就是每一个小时占的弧度
PI/360 是每一个分钟占的弧度PI/360其实是PI/(6*60) PI/21600是PI/(6*60*60)每一分钟在π里占的弧度 以此类推。
坐标转换就是在中心圆点基础上进行sin 和cos 距离的相加相减,sin是相加,cos是想减,这个大家画图就能理解了