用Java畫簡易時鐘

來源:互聯網
上載者:User

標籤:try   port   ack   hint   img   opera   功能   double   ++   

最近我一直在寫老師布置的時鐘日期工具,但是因為部分功能還未實現,所以還不能貼完整代碼,不過後面會陸續更新電子錶和日曆的小程式。雖然這個時鐘的不是特別好看,但是基本的功能已經實現。讀者可以試著把它做的跟美觀漂亮一點。

簡易時鐘的代碼如下:

 1 import java.awt.*; 2 import java.awt.geom.AffineTransform; 3 import java.util.Calendar; 4 import javax.swing.JFrame; 5 import javax.swing.JPanel; 6 //@Author:M-Y 7 //@Time:2016/12/1 8 public class time extends JFrame{ 9     public time(){10         JPanel panel=new JPanel();11         panel.setLayout(new GridLayout());12         Clock1 c=new Clock1();13         panel.add(c);14         this.add(panel);15     }16     public static void main(String[] args) {17         time t=new time();18         t.setSize(300,300); //視窗大小19         t.setTitle("Clock");//視窗名20         t.setVisible(true); //設定為可見21         t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);22     }23 }24 class Clock1 extends JPanel{25     Thread th;26     public void run(){27         while(true){28             try{29                 repaint();30             }catch(Exception e){}31         }32     }33     public void paint(Graphics g){34         Graphics2D g2d=(Graphics2D)g;35         //去掉鋸齒  36         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);  37         //清空原來的圖形  38         g2d.setColor(Color.white);  39         g2d.fillRect(0,0,this.getWidth(),this.getHeight()); 40         g2d.setColor(Color.black);41         //圓心(x,y)  半徑radius=10042         int x0=150;43         int y0=150;44         int radius=100;45         g2d.drawOval(50,50,200,200);46         g2d.drawOval(48,48,204,204);47         //畫刻度48          for(int i=0;i<60;i++){    49                g2d.fillRect(x0-2,46,3,3);   50                g2d.rotate(Math.toRadians(6),x0,y0);    51            }52         //畫12個數字53          for(int i=0;i<12;i++){    54                 double d=Math.PI/180*i*(360/12); //每次轉360/12度    55                 int x=(x0-4)+(int)((radius-12)*Math.cos(d));    56                 int y=(y0+4)+(int)((radius-12)*Math.sin(d));    57                 //因為是從順時鐘3點鐘開始畫,所以索引i需要加上3    58                 int j=i+3;    59                 if(j>12)    60                 j=j-12;    61                 g2d.drawString(Integer.toString(j),x,y);                  62          }63         //設定旋轉重設 64          AffineTransform old=g2d.getTransform(); 65          g2d.setTransform(old);  66          Calendar c=Calendar.getInstance();   67          int h=c.get(Calendar.HOUR_OF_DAY); //得到hour ----時68          int m=c.get(Calendar.MINUTE);      //得到minute---分69          int s=c.get(Calendar.SECOND);      //得到second---秒70          //畫時針 71          double hAngle=(h-12+m/60d)*360d/12d;72          g2d.rotate(Math.toRadians(hAngle),x0,y0);   73          g.drawLine(x0,y0,x0,100);74          //畫分針75          double mAngle=(m+s/60d)*360d/60d;76          g2d.rotate(Math.toRadians(mAngle),x0,y0);   77          g.drawLine(x0,y0,x0,70);78          //畫秒針79          double sAngle=s*360d/60d;  80          g2d.rotate(Math.toRadians(sAngle),x0,y0);   81          g.drawLine(x0,y0,x0,55);  82          repaint();83     }84 }

運行介面如下:

             

                               

 

用Java畫簡易時鐘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.