查看: 1286|回复: 0

[Java学习] java实现动态时钟并设置闹钟功能

发表于 2018-3-2 08:04:41

本文实例为大家分享了java实现动态时钟设置闹钟功能,供大家参考,具体内容如下

显示如上图所示的动态时钟,并且可以设置闹钟,播放mp3。

首先用到的是时钟(Timer)和日历(Calendar)得到系统的当前时间。

代码如下:

  1. import java.awt.Graphics;
  2. import java.awt.Graphics2D;
  3. import java.awt.geom.Ellipse2D;
  4. import java.awt.geom.Line2D;
  5. import java.io.BufferedInputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.util.Calendar;
  10. import java.util.GregorianCalendar;
  11. import java.util.Timer;
  12. import java.util.TimerTask;
  13. import javax.media.CannotRealizeException;
  14. import javax.media.Manager;
  15. import javax.media.MediaLocator;
  16. import javax.media.NoPlayerException;
  17. import javax.swing.JFrame;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPanel;
  20. import javazoom.jl.player.Player;
  21. public class Clock extends JFrame {
  22. MyPanel clockPanel;
  23. Ellipse2D.Double e;
  24. int x;
  25. int y;
  26. Line2D.Double hourLine;
  27. Line2D.Double minLine;
  28. Line2D.Double secondLine;
  29. GregorianCalendar calendar;
  30. int hour;
  31. int minute;
  32. int second;
  33. String timestr = "";
  34. static int sethour;
  35. static int setminute;
  36. static int setsecond;
  37. public static final int X = 60;
  38. public static final int Y = 60;
  39. public static final int X_BEGIN = 10;
  40. public static final int Y_BEGIN = 10;
  41. public static final int RADIAN = 50;
  42. public Clock(){
  43. setSize(300, 200);
  44. setTitle("动态时钟");
  45. clockPanel = new MyPanel();
  46. add(clockPanel);
  47. Timer t = new Timer();
  48. Task task = new Task();
  49. t.schedule(task, 0, 1000);//每秒刷新一次
  50. }
  51. File file = new File("当我想你的时候.mp3");
  52. public static void playMusic(File file) { //显示mp3文件的绝对路径
  53. try {
  54. javax.media.Player player = null;
  55. if (file.exists()) {
  56. MediaLocator locator = new MediaLocator("file:"
  57. + file.getAbsolutePath());
  58. System.out.println(file.getAbsolutePath());
  59. player = Manager.createRealizedPlayer(locator);
  60. player.prefetch();// ?准备读取
  61. player.start();// 开始读取
  62. } else {
  63. System.out.println("没找到文件");
  64. }
  65. } catch (CannotRealizeException ex) {
  66. ex.printStackTrace();
  67. } catch (NoPlayerException ex) {
  68. ex.printStackTrace();
  69. } catch (IOException ex) {
  70. ex.printStackTrace();
  71. }
  72. }
  73. public void play() {//播放mp3文件
  74. try {
  75. BufferedInputStream buffer = new BufferedInputStream(new FileInputStream("当我想你的时候.mp3"));
  76. Player player = new Player(buffer);
  77. player.play();
  78. } catch (Exception e) {
  79. System.out.println(e);
  80. }
  81. }
  82. public static void main(String[] args) {
  83. Clock t = new Clock();
  84. t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85. t.setVisible(true);
  86. //t.setLocationRelativeTo(null);//窗体显示在屏幕中央
  87. //输入要设置的闹钟时间
  88. sethour = Integer.parseInt(JOptionPane.showInputDialog("请输入小时:"));
  89. setminute = Integer.parseInt(JOptionPane.showInputDialog("请输入分钟:"));
  90. setsecond = Integer.parseInt(JOptionPane.showInputDialog("请输入秒:"));
  91. }
  92. class MyPanel extends JPanel {
  93. public MyPanel() {
  94. e = new Ellipse2D.Double(X_BEGIN, Y_BEGIN, 100, 100);
  95. hourLine = new Line2D.Double(X, Y, X, Y);
  96. minLine = new Line2D.Double(X, Y, X, Y);
  97. secondLine = new Line2D.Double(X, Y, X, Y);
  98. }
  99. public void paintComponent(Graphics g) {
  100. super.paintComponent(g);
  101. Graphics2D g2 = (Graphics2D) g;
  102. g2.drawString("12", 55, 25);//整点时间
  103. g2.drawString("6", 55, 105);
  104. g2.drawString("9", 15, 65);
  105. g2.drawString("3", 100, 65);
  106. g2.drawString(timestr, 0, 130);
  107. g2.draw(e);
  108. g2.draw(hourLine);//时针
  109. g2.draw(minLine);//分针
  110. g2.draw(secondLine);//秒针
  111. }
  112. }
  113. class Task extends TimerTask {
  114. public void run() {
  115. calendar = new GregorianCalendar();
  116. hour = calendar.get(Calendar.HOUR);
  117. minute = calendar.get(Calendar.MINUTE);
  118. second = calendar.get(Calendar.SECOND);
  119. if(sethour == hour && setminute == minute && setsecond == second){
  120. playMusic(file);
  121. play();
  122. }
  123. timestr = "当前时间:" + hour + " : " + minute + " : " + second;
  124. hourLine.x2 = X + 40 * Math.cos(hour * (Math.PI / 6) - Math.PI / 2);
  125. hourLine.y2 = Y + 40 * Math.sin(hour * (Math.PI / 6) - Math.PI / 2);
  126. minLine.x2 = X + 45
  127. * Math.cos(minute * (Math.PI / 30) - Math.PI / 2);
  128. minLine.y2 = Y + 45
  129. * Math.sin(minute * (Math.PI / 30) - Math.PI / 2);
  130. secondLine.x2 = X + 50
  131. * Math.cos(second * (Math.PI / 30) - Math.PI / 2);
  132. secondLine.y2 = Y + 50
  133. * Math.sin(second * (Math.PI / 30) - Math.PI / 2);
  134. repaint();
  135. }
  136. }
  137. }
复制代码

其中播放mp3文件需要下载对应的jar包,否则不能播放。

下载地址:java实现动态时钟

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报