This article shares the Java implementation of dynamic clock setting alarm clock for your reference. The specific content is as follows
Displays the dynamic clock as shown in the figure above, and can set the alarm to play mp3.
The first thing to use is the clock (Timer) and calendar (Calendar) to get the current time of the system.
The code is as follows:
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Timer; import java.util.TimerTask; import javax.media.CannotRealizeException; import javax.media.Manager; import javax.media.MediaLocator; import javax.media.NoPlayerException; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javazoom.jl.player.Player; public class Clock extends JFrame { MyPanel clockPanel; Ellipse2D.Double e; int x; int y; Line2D.Double hourLine; Line2D.Double minLine; Line2D.Double secondLine; GregorianCalendar calendar; int hour; int minute; int second; String timestr = ""; static int sethour; static int setminute; static int setsecond; public static final int X = 60; public static final int Y = 60; public static final int X_BEGIN = 10; public static final int Y_BEGIN = 10; public static final int RADIAN = 50; public Clock(){ setSize(300, 200); setTitle("Dynamic Clock"); clockPanel = new MyPanel(); add(clockPanel); Timer t = new Timer(); Task task = new Task(); t.schedule(task, 0, 1000);//Refresh once every second} File file = new File("When I miss you.mp3"); public static void playMusic(File file) { //Show the absolute path of the mp3 file try { javax.media.Player player = null; if (file.exists()) { MediaLocator locator = new MediaLocator("file:" + file.getAbsolutePath()); System.out.println(file.getAbsolutePath()); player = Manager.createRealizedPlayer(locator); player.prefetch();// 新 is ready to read player.start();// Start reading } else { System.out.println("No file found"); } } catch (CannotRealizeException ex) { ex.printStackTrace(); } catch (NoPlayerException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } public void play() {//Play mp3 file try { BufferedInputStream buffer = new BufferedInputStream(new FileInputStream("When I miss you.mp3")); Player player = new Player(buffer); player.play(); } catch (Exception e) { System.out.println(e); } } public static void main(String[] args) { Clock t = new Clock(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setVisible(true); //t.setLocationRelativeTo(null); // form is displayed in the center of the screen//Enter the alarm time to be sethour = Integer.parseInt(JOptionPane.showInputDialog("Please enter the hour:")); setminute = Integer.parseInt(JOptionPane.showInputDialog("Please enter minutes: ")); setsecond = Integer.parseInt(JOptionPane.showInputDialog("Please enter seconds: ")); } class MyPanel extends JPanel { public MyPanel() { e = new Ellipse2D.Double(X_BEGIN, Y_BEGIN, 100, 100); hourLine = new Line2D.Double(X, Y, X, Y); minLine = new Line2D.Double(X, Y, X, Y); secondLine = new Line2D.Double(X, Y, X, Y); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.drawString("12", 55, 25);//Occasion time g2.drawString("6", 55, 105); g2.drawString("9", 15, 65); g2.drawString("3", 100, 65); g2.drawString(timestr, 0, 130); g2.draw(e); g2.draw(hourLine);//hour hand g2.draw(minLine);//minute hand g2.draw(secondLine);//second hand} } class Task extends TimerTask { public void run() { calendar = new GregorianCalendar(); hour = calendar.get(Calendar.HOUR); minute = calendar.get(Calendar.MINUTE); second = calendar.get(Calendar.SECOND); if(sethour == hour && setminute == minute && setsecond == second){ playMusic(file); play(); } timestr = "Current time:" + hour + " : " + minute + " : " + second; hour.x2 = X + 40 * Math.cos(hour * (Math.PI / 6) - Math.PI / 2); hourLine.y2 = Y + 40 * Math.sin(hour * (Math.PI / 6) - Math.PI / 2); minLine.x2 = X + 45 * Math.cos(minute * (Math.PI / 30) - Math.PI / 2); minLine.y2 = Y + 45 * Math.sin(minute * (Math.PI / 30) - Math.PI / 2); secondLine.x2 = X + 50 * Math.cos(second * (Math.PI / 30) - Math.PI / 2); secondLine.y2 = Y + 50 * Math.sin(second * (Math.PI / 30) - Math.PI / 2); repaint(); } } } Playing the mp3 file requires downloading the corresponding jar package, otherwise it cannot be played.
Download address: Java implements dynamic clock
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.