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

来自 , 2019-10-14, 写在 Java, 查看 160 次.
URL http://www.code666.cn/view/b865367f
  1.  
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.awt.*;
  6.  
  7. import java.util.*;
  8.  
  9. import java.lang.Thread;
  10.  
  11. import java.text.DecimalFormat;
  12.  
  13.  
  14.  
  15. public class DongClock extends JPanel {
  16.  
  17.  
  18.  
  19.   /**
  20.  
  21.    * @param args
  22.  
  23.    */
  24.  
  25.   private int hour;
  26.  
  27.   private int minute;
  28.  
  29.   private int second;
  30.  
  31.    
  32.  
  33.   //构造函数
  34.  
  35.   public DongClock() {
  36.  
  37.     setCurrentTime();
  38.  
  39.   }
  40.  
  41.  
  42.  
  43.   //返回小时
  44.  
  45.   public int getHour() {
  46.  
  47.     return hour;
  48.  
  49.   }
  50.  
  51.    
  52.  
  53.    
  54.  
  55.   public int getMinute() {
  56.  
  57.     return minute;
  58.  
  59.   }
  60.  
  61.    
  62.  
  63.    
  64.  
  65.    
  66.  
  67.   public int getSecond() {
  68.  
  69.     return second;
  70.  
  71.   }
  72.  
  73.    
  74.  
  75.    
  76.  
  77.   //绘制时钟
  78.  
  79.   protected void paintComponent(Graphics g) {
  80.  
  81.      
  82.  
  83.     super.paintComponent(g);
  84.  
  85.     //初始化
  86.  
  87.     int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
  88.  
  89.     int xCenter = getWidth() / 2;
  90.  
  91.     int yCenter = getHeight() / 2;
  92.  
  93.     //画圆
  94.  
  95.     g.setColor(Color.black);
  96.  
  97.     g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius);
  98.  
  99.     g.drawString("12", xCenter - 5, yCenter - clockRadius + 15);
  100.  
  101.     g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
  102.  
  103.     g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
  104.  
  105.     g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);
  106.  
  107.     //画秒针
  108.  
  109.     int sLength = (int)(clockRadius * 0.8);
  110.  
  111.     int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
  112.  
  113.     int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
  114.  
  115.      
  116.  
  117.     g.setColor(Color.red);
  118.  
  119.     g.drawLine(xCenter, yCenter, xSecond, ySecond);
  120.  
  121.      
  122.  
  123.     //画分针
  124.  
  125.     int mLenth = (int)(clockRadius * 0.65);
  126.  
  127.     int xMinute = (int)(xCenter + mLenth * Math.sin(minute * (2 * Math.PI / 60)));
  128.  
  129.     int yMinute = (int)(xCenter - mLenth * Math.cos(minute * (2 * Math.PI / 60)));
  130.  
  131.     g.setColor(Color.blue);
  132.  
  133.     g.drawLine(xCenter, yCenter, xMinute, yMinute);
  134.  
  135.      
  136.  
  137.     //画时针
  138.  
  139.     int hLength = (int)(clockRadius * 0.5);
  140.  
  141.     int xHour = (int)(xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
  142.  
  143.     int yHour = (int)(yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
  144.  
  145.     g.setColor(Color.green);
  146.  
  147.     g.drawLine(xCenter, yCenter, xHour, yHour);
  148.  
  149.      
  150.  
  151.     //画数字时钟
  152.  
  153.     g.setColor(Color.black);
  154.  
  155.     DecimalFormat s=new DecimalFormat("00");
  156.  
  157.     g.drawString(s.format(getHour()) + ":" + s.format(getMinute()) + ":" + s.format(getSecond()), xCenter - 22, yCenter - clockRadius - 15);
  158.  
  159.      
  160.  
  161.   }
  162.  
  163.    
  164.  
  165.   public void setCurrentTime() {
  166.  
  167.     Calendar calendar = new GregorianCalendar();
  168.  
  169.      
  170.  
  171.     this.hour = calendar.get(Calendar.HOUR_OF_DAY);
  172.  
  173.     this.minute = calendar.get(Calendar.MINUTE);
  174.  
  175.     this.second = calendar.get(Calendar.SECOND);
  176.  
  177.   }
  178.  
  179.    
  180.  
  181.    
  182.  
  183.   public static void main(String[] args) {
  184.  
  185.     // TODO Auto-generated method stub
  186.  
  187.     JFrame frame = new JFrame("DiaplayClock");
  188.  
  189.         frame.setResizable(false);
  190.  
  191.     frame.setTitle("DiaplayClock");
  192.  
  193.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  194.  
  195.     frame.setSize(300,350);
  196.  
  197.     frame.setVisible(true);
  198.  
  199.      
  200.  
  201.     while(true) {
  202.  
  203.         DongClock clock = new DongClock();
  204.  
  205.       frame.getContentPane().add(clock);
  206.  
  207.       clock.setVisible(true);
  208.  
  209.       frame.validate();
  210.  
  211.       try {
  212.  
  213.         Thread.sleep(1000);
  214.  
  215.          
  216.  
  217.       }
  218.  
  219.       catch (InterruptedException e) {
  220.  
  221.         e.printStackTrace();
  222.  
  223.       }
  224.  
  225.       clock.setVisible(false);
  226.  
  227.       frame.remove(clock);
  228.  
  229.       clock = null;
  230.  
  231.       frame.validate();
  232.  
  233.        
  234.  
  235.     }
  236.  
  237.  
  238.  
  239.   }
  240.  
  241. }
  242.  

回复 "时钟"

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

captcha