java實現(xiàn)動態(tài)時鐘并設置鬧鐘功能
更新時間:2018年01月25日 09:19:35 作者:HQUZkP
這篇文章主要為大家詳細介紹了java實現(xiàn)動態(tài)時鐘并設置鬧鐘功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)動態(tài)時鐘設置鬧鐘功能,供大家參考,具體內(nèi)容如下

顯示如上圖所示的動態(tài)時鐘,并且可以設置鬧鐘,播放mp3。
首先用到的是時鐘(Timer)和日歷(Calendar)得到系統(tǒng)的當前時間。
代碼如下:
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("動態(tài)時鐘");
clockPanel = new MyPanel();
add(clockPanel);
Timer t = new Timer();
Task task = new Task();
t.schedule(task, 0, 1000);//每秒刷新一次
}
File file = new File("當我想你的時候.mp3");
public static void playMusic(File file) { //顯示mp3文件的絕對路徑
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();// Ԥ準備讀取
player.start();// 開始讀取
} else {
System.out.println("沒找到文件");
}
} catch (CannotRealizeException ex) {
ex.printStackTrace();
} catch (NoPlayerException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void play() {//播放mp3文件
try {
BufferedInputStream buffer = new BufferedInputStream(new FileInputStream("當我想你的時候.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);//窗體顯示在屏幕中央
//輸入要設置的鬧鐘時間
sethour = Integer.parseInt(JOptionPane.showInputDialog("請輸入小時:"));
setminute = Integer.parseInt(JOptionPane.showInputDialog("請輸入分鐘:"));
setsecond = Integer.parseInt(JOptionPane.showInputDialog("請輸入秒:"));
}
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);//整點時間
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);//時針
g2.draw(minLine);//分針
g2.draw(secondLine);//秒針
}
}
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 = "當前時間:" + 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();
}
}
}
其中播放mp3文件需要下載對應的jar包,否則不能播放。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
springboot集成druid,多數(shù)據(jù)源可視化,p6spy問題
這篇文章主要介紹了springboot集成druid,多數(shù)據(jù)源可視化,p6spy問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringBoot使用Apache Tika實現(xiàn)多種文檔的內(nèi)容解析
在日常開發(fā)中,我們經(jīng)常需要解析不同類型的文檔,如PDF、Word、Excel、HTML、TXT等,Apache Tika是一個強大的內(nèi)容解析工具,可以輕松地提取文檔中的內(nèi)容和元數(shù)據(jù)信息,本文將通過SpringBoot和Apache Tika的結(jié)合,介紹如何實現(xiàn)對多種文檔格式的內(nèi)容解析2024-12-12
基于Spring Security的Oauth2授權(quán)實現(xiàn)方法
這篇文章主要介紹了基于Spring Security的Oauth2授權(quán)實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
關于重寫equals()方法和hashCode()方法及其簡單的應用
這篇文章主要介紹了關于重寫equals()方法和hashCode()方法及其簡單的應用,網(wǎng)上的知識有些可能是錯誤的,關于?equals()?方法的理解,大家討論不一樣,需要的朋友可以參考下2023-04-04

