Java implements the dynamic clock and sets the alarm function, java Dynamic clock alarm

Source: Internet
Author: User

Java implements the dynamic clock and sets the alarm function, java Dynamic clock alarm

The example in this article shares with you the java function for implementing the dynamic clock setting function for your reference. The details are as follows:

Shows the dynamic clock shown in. You can set an 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 every second} File file = new File ("wait for your time"); public static void playMusic (File file) {// display 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 (); // prepare to read the player. start (); // start to read} else {System. out. println ("file not 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 ("wait when I think about you ")); 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. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); t. setVisible (true); // t. setLocationRelativeTo (null); // The form is displayed in the center of the screen // enter the set alarm time sethour = Integer. parseInt (JOptionPane. showInputDialog ("Enter the hour:"); setminute = Integer. parseInt (JOptionPane. showInputDialog ("Enter the minute:"); setsecond = Integer. parseInt (JOptionPane. showInputDialog ("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); // The hour 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; hourLine. 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 ();}}}

The corresponding jar package must be downloaded to play the mp3 file; otherwise, the file cannot be played.

: Implement Dynamic clock in java

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.