彩票走势图

Java开发示例:如何编写小时钟

原创|其它|编辑:郝浩|2009-08-25 10:09:58.000|阅读 449 次

概述:本文是一篇Java开发代码示例的文章,主要介绍了如何编写小时钟。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

/**
  *
  * author @;wwwwwwking
  *
  */
  //package MainPackage;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.geom.*;
  import java.awt.event.*;
  import java.util.Calendar;
  import java.util.GregorianCalendar;
  /**
  * This is the main Function of the program.
  */
  public class Clock{
  public static void main(String []args){
  ClockFrame frame = new ClockFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  }
  }
  /**
  * This class is used to define the main frame of the clock
  */
  class ClockFrame extends JFrame{
  //constructor function
  public ClockFrame(){
  setTitle("小时钟");
  setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  setLocation(DEFAULT_LOC_WIDTH, DEFAULT_LOC_HEIGHT);
  ClockPanel panel = new ClockPanel();
  add(panel);
  }
  //variables of the frame
  private int DEFAULT_LOC_WIDTH = 300;
  private int DEFAULT_LOC_HEIGHT = 300;
  private int DEFAULT_WIDTH = 330;
  private int DEFAULT_HEIGHT = 330;
  }
  /**
  * This class is used to defind the main panel of the clock
  */
  class ClockPanel extends JPanel{
  public void paintComponent(Graphics g){
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;
  //get the time of the system
  GregorianCalendar calendar = new GregorianCalendar();
  int hour = calendar.get(Calendar.HOUR);
  int minute = calendar.get(Calendar.MINUTE);
  int second = calendar.get(Calendar.SECOND);
  //draw the clock face
  Ellipse2D clockFace = new Ellipse2D.Double();
  clockFace.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+RADIUS, CENTER_Y+RADIUS);
  g2.setColor(Color.BLUE);
  g2.draw(clockFace);
  //draw the clock center
  Ellipse2D clockCenter = new Ellipse2D.Double();
  clockCenter.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+INNER_RADIUS, CENTER_Y+INNER_RADIUS);
  g2.setColor(Color.RED);
  g2.fill(clockCenter);
  //help to get the exact position of the lines
  double lenX, lenY, posX, posY;
  //draw the clock second line
  Line2D clockSecond = new Line2D.Double();
  double secondTime = (double) calendar.get(Calendar.SECOND);
  lenX = SECOND_LEN*Math.sin(2*Math.PI*secondTime/60.0);
  lenY = SECOND_LEN*Math.cos(2*Math.PI*secondTime/60.0);
  posX = CENTER_X + lenX;
  posY = CENTER_Y - lenY;
  clockSecond.setLine(CENTER_X, CENTER_Y, posX, posY);
  g2.setColor(Color.PINK);
  g2.draw(clockSecond);
  //draw the clock minute line
  Line2D clockMinute = new Line2D.Double();
  double minuteTime = (double) calendar.get(Calendar.MINUTE);
  lenX = MINUTE_LEN*Math.sin(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
  lenY = MINUTE_LEN*Math.cos(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
  posX = CENTER_X + lenX;
  posY = CENTER_Y - lenY;
  clockMinute.setLine(CENTER_X, CENTER_Y, posX, posY);
  g2.setColor(Color.GREEN);
  g2.draw(clockMinute);
  //draw the clock hour line
  Line2D clockHour = new Line2D.Double();
  double hourTime = (double) calendar.get(Calendar.HOUR);
  lenX = HOUR_LEN*Math.sin(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
  lenY = HOUR_LEN*Math.cos(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
  posX = CENTER_X + lenX;
  posY = CENTER_Y - lenY;
  clockHour.setLine(CENTER_X, CENTER_Y, posX, posY);
  g2.setColor(Color.BLUE);
  g2.draw(clockHour);
  int delay = 1000;
  // actionListener
  ActionListener drawClock;
  drawClock=new ActionListener(){
  public void actionPerformed(ActionEvent evt){
  repaint();
  }
  };
  //create timer
  new Timer(delay, drawClock).start();
  }
  //variables of the panel
  private int HOUR_LEN = 50;
  private int MINUTE_LEN = 70;
  private int SECOND_LEN = 90;
  private int RADIUS = 100;
  private int INNER_RADIUS = 2;
  private int CENTER_X = 150;
  private int CENTER_Y = 150;
  }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP