JAVA时钟

5年以前  |  阅读数:444 次  |  编程语言:JAVA 
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.lang.Thread;
import java.text.DecimalFormat;

public class DongClock extends JPanel {
  /**

   * @param args

   */

  private int hour;

  private int minute;

  private int second;



  //构造函数

  public DongClock() {

    setCurrentTime();

  }



  //返回小时

  public int getHour() {

    return hour;

  }





  public int getMinute() {

    return minute;

  }


  public int getSecond() {

    return second;

  }


  //绘制时钟

  protected void paintComponent(Graphics g) {



    super.paintComponent(g);

    //初始化

    int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);

    int xCenter = getWidth() / 2;

    int yCenter = getHeight() / 2;

    //画圆

    g.setColor(Color.black);

    g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius);

    g.drawString("12", xCenter - 5, yCenter - clockRadius + 15);

    g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);

    g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);

    g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);

    //画秒针

    int sLength = (int)(clockRadius * 0.8);

    int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));

    int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));



    g.setColor(Color.red);

    g.drawLine(xCenter, yCenter, xSecond, ySecond);



    //画分针

    int mLenth = (int)(clockRadius * 0.65);

    int xMinute = (int)(xCenter + mLenth * Math.sin(minute * (2 * Math.PI / 60)));

    int yMinute = (int)(xCenter - mLenth * Math.cos(minute * (2 * Math.PI / 60)));

    g.setColor(Color.blue);

    g.drawLine(xCenter, yCenter, xMinute, yMinute);



    //画时针

    int hLength = (int)(clockRadius * 0.5);

    int xHour = (int)(xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));

    int yHour = (int)(yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));

    g.setColor(Color.green);

    g.drawLine(xCenter, yCenter, xHour, yHour);

    //画数字时钟

    g.setColor(Color.black);

    DecimalFormat s=new DecimalFormat("00");

    g.drawString(s.format(getHour()) + ":" + s.format(getMinute()) + ":" + s.format(getSecond()), xCenter - 22, yCenter - clockRadius - 15);
  }

  public void setCurrentTime() {

    Calendar calendar = new GregorianCalendar();

    this.hour = calendar.get(Calendar.HOUR_OF_DAY);

    this.minute = calendar.get(Calendar.MINUTE);

    this.second = calendar.get(Calendar.SECOND);

  }

  public static void main(String[] args) {

    // TODO Auto-generated method stub

    JFrame frame = new JFrame("DiaplayClock");

        frame.setResizable(false);

    frame.setTitle("DiaplayClock");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(300,350);

    frame.setVisible(true);

    while(true) {
    DongClock clock = new DongClock();
    frame.getContentPane().add(clock);

      clock.setVisible(true);
      frame.validate();

      try {
        Thread.sleep(1000);
      }

      catch (InterruptedException e) {
        e.printStackTrace(); 
      }

      clock.setVisible(false);
      frame.remove(clock);
      clock = null;
      frame.validate();

    }
  }

}
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例