趣味Java编程:Java小时钟编程代码介绍Java认证考试

文章作者 100test 发表时间 2010:01:01 15:44:59
来源 100Test.Com百考试题网


  /**
  *
  *
  */
  //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.
  }

  编辑特别推荐:

  指点一下:到底该不该去考JAVA认证?

  Java认证权威问答精华集



相关文章


Java加密和数字签名3公钥加密Java认证考试
Java加密和数字签名2私钥加密Java认证考试
Java加密和数字签名1消息摘要Java认证考试
Java新技术在协同OA中的应用Java认证考试
趣味Java编程:Java小时钟编程代码介绍Java认证考试
JSP无刷新文件上传的实现Java认证考试
使用Decorate模式实现留言版词汇处理Java认证考试
一个Servlet生命周期Java认证考试
Java学习笔记整理(4)Java认证考试
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛