JAVA认证:小时钟编程代码介绍
来源:优易学  2011-9-12 10:40:28   【优易学:中国教育考试门户网】   资料下载   IT书店
  /**
  *
  *
  */
  //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;
  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训