欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现动态时钟并设置闹钟功能

程序员文章站 2023-11-29 08:37:34
本文实例为大家分享了java实现动态时钟设置闹钟功能,供大家参考,具体内容如下 显示如上图所示的动态时钟,并且可以设置闹钟,播放mp3。 首先用到的是时钟(ti...

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

java实现动态时钟并设置闹钟功能

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

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

代码如下:

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("动态时钟"); 
    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包,否则不能播放。

下载地址:

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