[Java] java时钟 →→→→→进入此内容的聊天室

来自 , 2021-02-05, 写在 Java, 查看 139 次.
URL http://www.code666.cn/view/6dd4e10e
  1. package day01;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9.  
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12.  
  13. public class Clock {
  14.  
  15.         public static void main(String[] args) {
  16.                 // TODO Auto-generated method stub
  17.                 JFrame frame = new ClockFrame();
  18.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.                 frame.setVisible(true);
  20.         }
  21. }
  22.      
  23.  
  24.  
  25.   class ClockFrame extends JFrame{
  26.           public ClockFrame(){
  27.           setTitle("java clock");
  28.           setBounds(100, 200, 600, 800);
  29.                 setVisible(true);      
  30.             ClockPanel cp = new ClockPanel();
  31.                 add(cp);
  32.           }
  33.   }
  34.  
  35.  
  36.   class ClockPanel extends JPanel{
  37.           int h;
  38.           int m;
  39.           int s;
  40.           int sx;
  41.           int sy;              
  42.           int mx;
  43.           int my;
  44.           int hx;
  45.           int hy;
  46.              public ClockPanel() {
  47.                 move();
  48.                 for(int i=0;i<60;i++){         
  49.                 int r = 100;
  50.                         if(i%5==0){
  51.                        
  52.                         if(i%15==0){
  53.  
  54.                         r=120;
  55.                                
  56.                        }else{
  57.                
  58.                         r=140;
  59.                        
  60.                         }
  61.                
  62.                 }
  63.                        
  64.                 int x1 = (int)(300+Math.sin(Math.toRadians(6*i))*r);
  65.  
  66.                 int y1 = (int)(300-Math.cos(Math.toRadians(i*6))*r);
  67.                
  68.                 int x2 = (int)(300+Math.sin(Math.toRadians(6*i))*180);
  69.                        
  70.                 int y2 = (int)(300-Math.cos(Math.toRadians(i*6))*180);
  71.                        
  72.  
  73.                 }
  74.                
  75.           }
  76.  
  77.                
  78.           public void paint(Graphics g ) {
  79.                
  80.                 super.paint(g);
  81.                
  82.                 setBackground(Color.LIGHT_GRAY);
  83.  
  84.                 Graphics2D g2 = (Graphics2D) g;
  85.                
  86.                 g2.drawString(h + ":" + m + ":" + s, 20, 50);
  87.                
  88.                 g2.setStroke(new BasicStroke(5));
  89.                
  90.                 g.setColor(Color.yellow);
  91.                
  92.                 g.drawLine(300,300,sx,sy);
  93.                
  94.                 g.setColor(Color.red);
  95.                
  96.                 g.drawLine(300,300,mx,my);
  97.  
  98.                 g.setColor(Color.gray);
  99.                
  100.                 g.drawLine(300,300,hx,hy);
  101.                
  102.                 g.setColor(Color.orange);
  103.                
  104.                 g.drawOval(120,120,360,360);
  105.                
  106.                 g.drawOval(205,205,190,190);
  107.                
  108.                 g.setColor(Color.black);
  109.                
  110.                 g.fillOval(292, 292, 15, 15);
  111.                
  112.                
  113.           for(int i=0;i<60;i++){
  114.                        
  115.                 int r = 100;
  116.                    if(i%5==0){
  117.                
  118.                  if(i%15==0){
  119.                
  120.                   r=120;
  121.                        
  122.                     }else{
  123.  
  124.                    r=140;
  125.                
  126.                 }
  127.  
  128.           }
  129.                                
  130.               int x1 = (int)(300+Math.sin(Math.toRadians(6*i))*r);
  131.                
  132.                 int y1 = (int)(300-Math.cos(Math.toRadians(i*6))*r);
  133.                
  134.                 int x2 = (int)(300+Math.sin(Math.toRadians(6*i))*180);
  135.                
  136.                 int y2 = (int)(300-Math.cos(Math.toRadians(i*6))*180);
  137.                        
  138.                 g.setColor(Color.green);
  139.                
  140.                 g.drawLine(x1,y1,x2,y2);
  141.                
  142.           }
  143.                
  144.               }
  145.  
  146.  
  147.                 public void move() {
  148.                
  149.                 new Thread() {
  150.                
  151.                         public void run() {
  152.                
  153.                                 while (true) {
  154.                
  155.                                         Date d = new Date();
  156.                
  157.                                         System.out.println(d);
  158.                
  159.                                         SimpleDateFormat ss = new SimpleDateFormat("ss");
  160.                
  161.                                         SimpleDateFormat mm = new SimpleDateFormat("mm");
  162.                
  163.                                         SimpleDateFormat hh = new SimpleDateFormat("HH");
  164.  
  165.                
  166.                                         s = Integer.parseInt(ss.format(d));
  167.                
  168.                                         m = Integer.parseInt(mm.format(d));
  169.  
  170.                                         h = Integer.parseInt(hh.format(d));
  171.  
  172.                                         sx = (int) (300 + Math.sin(Math.toRadians(6 * s)) * 150);
  173.                        
  174.                                         sy = (int) (300 - Math.cos(Math.toRadians(6 * s)) * 150);
  175.                
  176.                                         mx = (int) (300 + Math.sin(Math.toRadians(6 * m)) * 120);
  177.  
  178.                                         my = (int) (300 - Math.cos(Math.toRadians(6 * m)) * 120);
  179.                                
  180.                                         hx = (int) (300 + Math.sin(Math.toRadians(h * 30 + m * 0.5)) * 90);
  181.                
  182.                                         hy = (int) (300 - Math.cos(Math.toRadians(h * 30 + m * 0.5)) * 90);
  183.  
  184.                        
  185.                                        repaint();
  186.                        
  187.                                        try {
  188.                        
  189.                                         Thread.sleep(1000);
  190.                
  191.                                         } catch (InterruptedException e) {
  192.                        
  193.                                         e.printStackTrace();
  194.                        
  195.                                 }
  196.  
  197.                     }                          
  198.                   }
  199.                                
  200.                  }.start();
  201.  
  202.                }
  203.  
  204.           }

回复 "java时钟"

这儿你可以回复上面这条便签

captcha