利用Java的GUI和多線程編程技術,編寫一個時鐘顯示程序 – JAVA編程語言程序開發技術文章

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是想減,這個大傢畫圖就能理解瞭

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。