当前位置:   article > 正文

java 时钟设计_java timer 时钟的设计

java timer 时钟的设计

;;;;;;用Applet小程序写的;;;;;;;;

设计思路:: 画图,先是画指针起点就是圆心,终点是一个可以变的量,用系统获取的时间作为自变量

讲一下重要的,就是计算


  1. package practice1;
  2. import java.applet.Applet;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.sql.Time;
  7. import java.time.chrono.ChronoLocalDate;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. import java.util.Timer;
  11. import java.util.TimerTask;
  12. import javax.xml.crypto.Data;
  13. import org.w3c.dom.CDATASection;
  14. import practice3.Carculator;
  15. public class ClockApplet extends Applet {
  16. // 圆的左上角坐标(x,y) 半径为r
  17. private int x,y,r;
  18. //刻度的长度
  19. private int length;
  20. @Override
  21. public void init() {
  22. // TODO Auto-generated method stub
  23. super.init();
  24. x = y = 150;
  25. r = 100;
  26. length = 10;
  27. this.resize(new Dimension(500, 500));
  28. }
  29. @Override
  30. public void paint(Graphics g) {
  31. //画出四个刻度
  32. g.drawOval(x, y, 2*r, 2*r);
  33. g.setColor(Color.BLUE);
  34. //上刻度 0时
  35. g.drawLine(x+r, y, x+r, y+length);
  36. //12点的字样,坐标是调整出来的
  37. g.drawString("12", x+r-length/2, y+2*length+5);
  38. //下刻度 6时
  39. g.drawLine(x+r, y+r*2, x+r, y+r*2-length);
  40. //6点的字样,坐标是调整出来的
  41. g.drawString("6", x+r-2, y+r*2-length-5);
  42. //左刻度 9时
  43. g.drawLine(x, y+r, x+length, y+r);
  44. //9点的字样,坐标是调整出来的
  45. g.drawString("9", x+length+5, y+r+length/2);
  46. //右刻度 3时
  47. g.drawLine(x+2*r, y+r, x+2*r-length, y+r);
  48. //3点的字样,坐标是调整出来的
  49. g.drawString("3", x+2*r-length*2, y+r+length/2);
  50. //获取当前时间
  51. Calendar c = Calendar.getInstance();
  52. int hour = c.get(Calendar.HOUR);
  53. int minute = c.get(Calendar.MINUTE);
  54. int second = c.get(Calendar.SECOND);
  55. String s = new String(hour+":"+minute+":"+second);
  56. double angle1 = second*Math.PI/30.0;
  57. double angle2 = (second/60 + minute)*Math.PI/30.0;
  58. double angle3 = (minute/60 + hour)*Math.PI/6;
  59. //画时针、分针、秒针
  60. //圆心坐标(x+r,y+r)
  61. g.drawString(s, 100, 100);
  62. g.setColor(Color.RED);
  63. // TODO Auto-generated method stub
  64. //秒针
  65. g.drawLine(x+r, y+r, (int)(x+r+r*Math.sin(angle1)), (int)(x+r-r*Math.cos(angle1)));
  66. //分针
  67. g.drawLine(x+r, y+r, (int)(x+r+(r-length)*Math.sin(angle2)), (int)(x+r-(r-length)*Math.cos(angle2)));
  68. //时针
  69. g.drawLine(x+r, y+r, (int)(x+r+(r-length*3)*Math.sin(angle3)), (int)(x+r-(r-length*3)*Math.cos(angle3)));
  70. EXE();
  71. }
  72. public void EXE() {
  73. Timer t = new Timer();
  74. t.schedule(new TimerTask() {
  75. @Override
  76. public void run() {
  77. // TODO Auto-generated method stub
  78. repaint();
  79. }
  80. }, 1000);
  81. }
  82. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号