時鐘錶盤代碼

package timer.com.cn;

import javax.swing.*; //時鐘
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;

class Clock extends JFrame implements ActionListener {
 int x, y, x0, y0, r, h, olds_x, olds_y, oldm_x, oldm_y, oldh_x, oldh_y, ss,
   mm, hh, old_m, old_h, ang;
 final double RAD = Math.PI / 180; // 度數轉換成弧度的比例
 // 構造函數創建了一個窗體
 public Clock() {
  super("時鐘"); // 設置標題
  setSize(250, 250); // 設置窗口尺寸
  setBackground(Color.WHITE); // 設置背景顏色
  setLocation(300, 150); // 設置窗口位置座標
  setResizable(false); // 使窗口可以最小化和關閉,但是不能任意改變大小
  setVisible(true); // 設置組建可見
  int delay = 100; // 設置延時
  // 創建一個監聽事件
  ActionListener drawClock = new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    repaint();
   }
  };

  new Timer(delay, drawClock).start(); // 創建時間計數器,每秒觸發一次
 }

 public void actionPerformed(ActionEvent e) {// 實現ActionListener接口必須實現的方法}

 } // 繪製圖形
 public void paint(Graphics g) {
  Graphics2D g2D = (Graphics2D) g;
  Insets insets = getInsets();
  int L = insets.left / 2, T = insets.top / 2;
  h = getSize().height;
  g.setColor(Color.black);

  // 畫圓
  g2D.setStroke(new BasicStroke(4.0f));
  g.drawOval(L + 40, T + 40, h - 80, h - 80);
  r = h / 2 - 40;
  x0 = 40 + r - 5 + L;
  y0 = 40 + r - 5 - T;
  ang = 60;

  // 繪製時鐘上的12個數字
  for (int i = 1; i <= 12; i++) {
   x = (int) ((r - 9) * Math.cos(RAD * ang) + x0);
   y = (int) ((r - 9) * Math.sin(RAD * ang) + y0);
   g.drawString("" + i, x, h - y);
   ang -= 30;
  }

  // 獲得當前系統時間
  Calendar now = new GregorianCalendar();
  int nowh = now.get(Calendar.HOUR_OF_DAY);
  int nowm = now.get(Calendar.MINUTE);
  int nows = now.get(Calendar.SECOND);
  String st;
  if (nowh < 10)
   st = "0" + nowh;
  else
   st = "" + nowh;
  if (nowm < 10)
   st += ":0" + nowm;
  else
   st += ":" + nowm;
  if (nows < 10)
   st += ":0" + nows;
  else
   st += ":" + nows;

  // 在窗體上顯示時間
  g.setColor(Color.white);//
  g.fillRect(L, T, 50, 28);//
  g.setColor(Color.black);//
  g.drawString(st, L + 2, T + 26);//

  // 計算時間與度數的關係
  ss = 90 - nows * 6;
  mm = 90 - nowm * 6;
  hh = 90 - nowh * 30 - nowm / 2;
  x0 = r + 40 + L;
  y0 = r + 40 + T;

  g2D.setStroke(new BasicStroke(1.0f));// 秒針粗細
  // 擦除秒針
  if (olds_x > 0) {
   g.setColor(getBackground());
   g.drawLine(x0, y0, olds_x, h - olds_y);
  } else {
   old_m = mm;
   old_h = hh;
  }
  // 繪製秒針
  x = (int) (r * 0.9 * Math.cos(RAD * ss)) + x0;// 長度
  y = (int) (r * 0.9 * Math.sin(RAD * ss)) + y0 - 2 * T;
  g.setColor(Color.black);// 指針顏色
  g.drawLine(x0, y0, x, h - y);// 軌跡
  olds_x = x;
  olds_y = y;

  g2D.setStroke(new BasicStroke(2.2f));// 分針粗細
  // 擦除分針
  if (old_m != mm) {
   g.setColor(getBackground());
   g.drawLine(x0, y0, oldm_x, h - oldm_y);
  }
  // 繪製分針
  x = (int) (r * 0.7 * Math.cos(RAD * mm)) + x0;// 長度
  y = (int) (r * 0.7 * Math.sin(RAD * mm)) + y0 - 2 * T;
  g.setColor(Color.red);// 顏色
  g.drawLine(x0, y0, x, h - y);
  oldm_x = x;
  oldm_y = y;
  old_m = mm;

  g2D.setStroke(new BasicStroke(3.4f));// 時針粗細
  // 擦除時針
  if (old_h != hh) {
   g.setColor(getBackground());
   g.drawLine(x0, y0, oldh_x, h - oldh_y);
  }
  // 繪製時針
  x = (int) (r * 0.5 * Math.cos(RAD * hh)) + x0;// 長度
  y = (int) (r * 0.5 * Math.sin(RAD * hh)) + y0 - 2 * T;
  g.setColor(Color.red);// 顏色
  g.drawLine(x0, y0, x, h - y);
  oldh_x = x;
  oldh_y = y;
  old_h = hh;
 }

 public static void main(String[] args) {
  Clock c = new Clock();
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章