用JAVA做小時鐘

          Posted on 2006-09-03 07:08 久城 閱讀(1012) 評論(0)  編輯  收藏 所屬分類: JavaTest

          學JAVA 做的第一個GUI程序......
          import java.awt.*;
          import javax.swing.*;
          import java.util.*;

          class Clock extends JFrame
          {
          ?public static double PI = 3.14159265;
          ?Calendar now;
          ?int hh;
          ?int mm;
          ?int ss;
          ?Clock()
          ?{
          ??super("我的時鐘");
          ??setSize(400,400);
          ??setVisible(true);
          ?}
          ?public void paint(Graphics g)
          ?{
          ??g.setColor(Color.WHITE);
          ??g.fillRoundRect(100,100,200,200,200,200);
          ??g.setColor(Color.BLACK);
          ??g.drawString("1",(int)(200-2+100*(Math.sin(30*2*PI/360))),(int)(200+10-100*(Math.cos(30*2*PI/360))));
          ??g.drawString("2",(int)(200-6+100*(Math.sin(30*2*2*PI/360))),(int)(200+10-100*(Math.cos(30*2*2*PI/360))));
          ??g.drawString("3",(int)(200-7+100*(Math.sin(30*3*2*PI/360))),(int)(200-100*(Math.cos(30*3*2*PI/360))));
          ??g.drawString("4",(int)(200-6+100*(Math.sin(30*4*2*PI/360))),(int)(200-100*(Math.cos(30*4*2*PI/360))));
          ??g.drawString("5",(int)(200-2+100*(Math.sin(30*5*2*PI/360))),(int)(200-2-100*(Math.cos(30*5*2*PI/360))));
          ??g.drawString("6",(int)(200+100*(Math.sin(30*6*2*PI/360))),(int)(200-100*(Math.cos(30*6*2*PI/360))));
          ??g.drawString("7",(int)(200+100*(Math.sin(30*7*2*PI/360))),(int)(200-100*(Math.cos(30*7*2*PI/360))));
          ??g.drawString("8",(int)(200+100*(Math.sin(30*8*2*PI/360))),(int)(200-100*(Math.cos(30*8*2*PI/360))));
          ??g.drawString("9",(int)(200+100*(Math.sin(30*9*2*PI/360))),(int)(200-100*(Math.cos(30*9*2*PI/360))));
          ??g.drawString("10",(int)(200+2+100*(Math.sin(30*10*2*PI/360))),(int)(200+2-100*(Math.cos(30*10*2*PI/360))));
          ??g.drawString("11",(int)(200+100*(Math.sin(30*11*2*PI/360))),(int)(200+10-100*(Math.cos(30*11*2*PI/360))));
          ??g.drawString("12",(int)(200+100*(Math.sin(30*12*2*PI/360))),(int)(200+10-100*(Math.cos(30*12*2*PI/360))));
          ??g.setColor(Color.RED);

          ??Calendar now=Calendar.getInstance();
          ??int hh=now.get(Calendar.HOUR_OF_DAY);//小時
          ??int mm=now.get(Calendar.MINUTE);//分鐘
          ??int ss=now.get(Calendar.SECOND);// 秒
          ??g.drawLine(200,200,(int)(200+90*(Math.sin(6*ss*2*PI/360))),(int)(200-90*(Math.cos(6*ss*2*PI/360)))); //畫秒針
          ??g.setColor(Color.black);
          ??g.drawLine(200,200,(int)(200+70*(Math.sin(6*mm*2*PI/360))),(int)(200-70*(Math.cos(6*mm*2*PI/360))));//畫分針
          ??g.setColor(Color.blue);
          ??g.drawLine(200,200,(int)(200+50*(Math.sin((30*hh+0.5*mm)*2*PI/360))),(int)(200-50*(Math.cos((30*hh+0.5*mm)*2*PI/360)))); //畫時針
          ??try{Thread.sleep(500);}catch(Exception e){}
          ??repaint();??
          ?}

          ?public static void main(String[] args)
          ?{
          ??
          ??Clock c = new Clock();
          ??c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          ??
          ?}
          }



          歡迎來訪!^.^!
          本BLOG僅用于個人學習交流!
          目的在于記錄個人成長.
          所有文字均屬于個人理解.
          如有錯誤,望多多指教!不勝感激!

          Copyright © 久城

          主站蜘蛛池模板: 乐都县| 韩城市| 哈密市| 宜丰县| 呼伦贝尔市| 静安区| 小金县| 云霄县| 郁南县| 和龙市| 崇信县| 永宁县| 靖宇县| 兴隆县| 闵行区| 杨浦区| 石狮市| 甘南县| 朔州市| 邮箱| 车致| 呈贡县| 赤壁市| 长丰县| 河北区| 宜宾县| 遂川县| 盐池县| 乐陵市| 桃园市| 沧州市| 凤庆县| 昌平区| 彭州市| 武邑县| 永平县| 高尔夫| 大洼县| 鱼台县| 饶平县| 高平市|