java闹钟程序声音_跪求高手帮忙写一个JAVA手机闹钟程序 实现添加铃声和设置多闹钟...

展开全部

import java.util.*;

import java.awt.*;

import java.applet.*;

import java.text.*;

public class AlarmClock extends Applet implements Runnable

{

Thread timer=null; //创建线程timer

Image clockp,gif1,gif2,clock6,clock7; //clockp:闹钟的外壳,闹铃和e68a84e8a2ad3231313335323631343130323136353331333262353365报时鸟

int s,m,h,hh;

AudioClip ipAu,danger,chirp;

boolean canPaint=true;

boolean flag=false;

boolean strike=true;

int counter=0;

int lasts;

Image offscreen_buf=null;

int i,j,t=0;

int timeout=166;

int lastxs=0,lastys=0,lastxm=0,lastym=0,lastxh=0,lastyh=0;

Date dummy=new Date(); //生成Data对象

GregorianCalendar cal=new GregorianCalendar();

SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式

String lastdate=df.format(dummy);

Font F=new Font("TimesRoman",Font.PLAIN,14);//设置字体格式

Date dat=null;

Date timeNow=null;

Color fgcol=Color.blue;

Color fgcol2=Color.darkGray;

Panel setpanel;

Color backcolor=Color.pink;

TextField showhour,showmin,showsec,sethour,setmin,setsec;//显示当前时间文本框和定时文本框

Button onbutton;

Button offbutton;

Label hlabel1,mlabel1,slabel1,hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)

Label info1=new Label("欢迎使用定时提醒闹钟"),info2=new Label("");

Label note1=new Label("当前时间:"),note2=new Label("闹钟设置:");

boolean setalerm=false,clickflag=false;//判断是否响铃和振动

int fixh=0,fixm=0,fixs=0;//记录闹钟的定时

public void init()//初始化方法

{

Integer gif_number;

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数

setLayout(null); //将布局管理器初始化为null

setpanel=new Panel();

setpanel.setLayout(null);

setpanel.add(note1);

setpanel.add(note2);

note1.setBounds(30,100,60,20);

note1.setBackground(backcolor);

note1.setForeground(Color.black);

note2.setBounds(30,180,60,20);

note2.setBackground(backcolor);

note2.setForeground(Color.black);

hlabel1=new Label();

mlabel1=new Label();

slabel1=new Label();

hlabel2=new Label();

mlabel2=new Label();

slabel2=new Label();

//显示当前时间用的文本框

showhour=new TextField("00",5);

showmin=new TextField("00",5);

showsec=new TextField("00",5);

//定时用的文本框(时、分、秒)

sethour=new TextField("00",5);

setmin=new TextField("00",5);

setsec=new TextField("00",5);

//当前时间用的文本框的位置、大小

setpanel.add(showhour);

showhour.setBounds(fieldx,fieldy1,fieldw,fieldh);

showhour.setBackground(Color.white);

//在文本框后加入单位“时”

setpanel.add(hlabel1);

hlabel1.setText("时");

hlabel1.setBackground(backcolor);

hlabel1.setForeground(Color.black);

hlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//当前时间的分钟文本框的位置、大小

setpanel.add(showmin);

showmin.setBounds(fieldx,fieldy1,fieldw,fieldh);

showmin.setBackground(Color.white);

//在文本框后加入单位“分”

setpanel.add(mlabel1);

mlabel1.setText("分");

mlabel1.setBackground(backcolor);

mlabel1.setForeground(Color.black);

mlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//当前时间的秒文本框的位置、大小

setpanel.add(showsec);

showsec.setBounds(fieldx,fieldy1,fieldw,fieldh);

showsec.setBackground(Color.white);

//在文本框后加入单位“秒”

setpanel.add(slabel1);

slabel1.setText("秒");

slabel1.setBackground(backcolor);

slabel1.setForeground(Color.black);

slabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=50;

//定时的小时文本框的位置、大小

setpanel.add(sethour);

sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);

sethour.setBackground(Color.white);

//在文本框后加入单位“时”

setpanel.add(hlabel2);

hlabel2.setText("时");

hlabel2.setBackground(backcolor);

hlabel2.setForeground(Color.black);

hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的分钟文本框的位置、大小

setpanel.add(setmin);

setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);

setmin.setBackground(Color.white);

//在文本框后加入单位“分”

setpanel.add(mlabel2);

mlabel2.setText("分");

mlabel2.setBackground(backcolor);

mlabel2.setForeground(Color.black);

mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的秒文本框的位置、大小

setpanel.add(setsec);

setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);

setsec.setBackground(Color.white);

//在文本框后加入单位“秒”

setpanel.add(slabel2);

slabel2.setText("秒");

slabel2.setBackground(backcolor);

slabel2.setForeground(Color.black);

slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

//设置闹钟控制按钮(on,off)

onbutton=new Button("开");

offbutton=new Button("关");

setpanel.add(onbutton);

setpanel.add(offbutton);

onbutton.setBounds(90,180,40,20);

offbutton.setBounds(140,180,40,20);

//加入一些附加的信息标签(题头,题尾)

setpanel.add(info1);

info1.setBackground(backcolor);

info1.setForeground(Color.blue);

info1.setBounds(50,50,150,20);

setpanel.add(info2);

info2.setBackground(backcolor);

info2.setForeground(Color.blue);

info2.setBounds(150,280,100,20);

//将面板加入当前容器中,并设置面板的大小和背景色

add(setpanel);

setpanel.setBounds(300,1,250,420);

setpanel.setBackground(backcolor);

//获取声音文件

ipAu=getAudioClip(getDocumentBase(),"bells/仙剑.mid");

danger=getAudioClip(getDocumentBase(),"bells/0.mid");

chirp=getAudioClip(getDocumentBase(),"bells/3.mid");

int xcenter,ycenter,s,m,h;

xcenter=145;

ycenter=162;

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

//初始化指针位置

lastxs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

lasts=s;

MediaTracker mt=new MediaTracker(this);//创建Tracke对象

clockp=getImage(getDocumentBase(),"休闲.png");

gif1=getImage(getDocumentBase(),"gif1.gif");

gif2=getImage(getDocumentBase(),"gif2.gif");

clock6=getImage(getDocumentBase(),"clock6.gif");

clock7=getImage(getDocumentBase(),"clock7.gif");

mt.addImage(clockp,i++);

mt.addImage(gif1,i++);

mt.addImage(gif2,i++);

mt.addImage(clock6,i++);

mt.addImage(clock7,i++);

try{mt.waitForAll();}catch(InterruptedException e){};//等待加载结束

resize(600,420);//设置窗口大小

}

public void paint(Graphics g){//重写paint()方法

int xh,yh,xm,ym,xs,ys,strike_times;

int xcenter,ycenter;

String today;

Integer gif_number;

xcenter=148;

ycenter=186;

dat=new Date();

cal.setTime(dat);

//读取当前时间

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

today=df.format(dat);

//指针位置

xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

//设置字体和颜色

g.setFont(F);

g.setColor(fgcol);

g.setColor(fgcol2);

g.setColor(getBackground());

g.fillRect(1,1,634,419);

g.drawImage(clockp,75,110,this);

g.drawImage(clock6,83,280,this);

g.setColor(fgcol2);

g.setColor(getBackground());

g.setColor(fgcol2);

//以数字方式显示年、月、日和时间

g.drawString(today,55,415);

g.drawLine(xcenter,ycenter,xs,ys);

g.setColor(fgcol);

//画指针

g.drawLine(xcenter,ycenter-1,xm,ym);

g.drawLine(xcenter-1,ycenter,xm,ym);

g.drawLine(xcenter,ycenter-1,xh,yh);

g.drawLine(xcenter-1,ycenter,xh,yh);

lastxs=xs;lastys=ys;

lastxm=xh;lastym=ym;

lastxh=xh;lastyh=yh;

lastdate=today;

if(h<12)hh=h;//将系统时间变换到0-11区间

else hh=h-12;

if(hh==0) strike_times=12;//计算整点时钟声数

else strike_times=hh;

if((s==0&&m==0)||flag){//判断是否整点,是否是主动刷新

if(counter

flag=true;

g.drawImage(gif2,115,35,this);

if(lasts!=s){

if(strike){

counter++;

danger.play();//播放闹铃声

}

if(strike)strike=false;

else strike=true;

}

}

else {

counter=0;

flag=false;

}

}

else

g.drawImage(gif1,115,35,this);

int timedelta;//记录当前时间与闹铃定时的时差

Integer currh,currm,currs;//分别记录当前的时、分、秒

timeNow=new Date();

currh=new Integer(timeNow.getHours());

currm=new Integer(timeNow.getMinutes());

currs=new Integer(timeNow.getSeconds());

//判断是否要更新当前显示的时间,这样可以避免文本框出现频率闪动

if(currh.intValue()!=Integer.valueOf(showhour.getText()).intValue())

showhour.setText(currh.toString());

if(currm.intValue()!=Integer.valueOf(showmin.getText()).intValue())

showmin.setText(currh.toString());

if(currs.intValue()!=Integer.valueOf(showsec.getText()).intValue())

showsec.setText(currh.toString());

if(setalerm){ //判断是否设置了闹钟

//判断当前时间是否为闹钟所定的时间

if((currh.intValue()==fixh)&&(currm.intValue()==fixm)&&(currs.intValue()==fixs))

clickflag=true;

timedelta=currm.intValue()*60+currs.intValue()-fixm*60-fixs;

if((timedelta<60)&&(clickflag==true)){ //若当前时间与闹钟相差时间达到60秒

chirp.play();

g.drawImage(clock7,83,280,this);

}

else{

chirp.stop();

clickflag=false;

}

}

if(lasts!=s)

ipAu.play();//播放滴答声

lasts=s;

if(canPaint){

t+=1;

if(t==12)t=0;

}

canPaint=false;

dat=null;

}

public void start(){

if(timer==null){

timer=new Thread(this);//将timer实例化

timer.start();

}

}

public void stop(){

timer=null;

}

public void run(){

while(timer!=null){

try{timer.sleep(timeout);}catch(InterruptedException e){}

canPaint=true;

repaint();//刷新画面

}

timer=null;

}

public void update(Graphics g){ //采用双缓冲技术的update()方法

if(offscreen_buf==null)

offscreen_buf=createImage(600,420);

Graphics offg=offscreen_buf.getGraphics();

offg.clipRect(1,1,599,419);

paint(offg);

Graphics ong=getGraphics();

ong.clipRect(1,1,599,419);

ong.drawImage(offscreen_buf,0,0,this);

}

public boolean action(Event evt,Object arg){ //按钮事件处理函数

if(evt.target instanceof Button){

String lable=(String)arg;

if(lable.equals("开")){

setalerm=true;

//获取输入的时间

fixh=Integer.valueOf(sethour.getText()).intValue();

fixm=Integer.valueOf(setmin.getText()).intValue();

fixs=Integer.valueOf(setsec.getText()).intValue();

clickflag=false;

}

if(lable.equals("关")){

setalerm=false;

if(chirp!=null)

chirp.stop();

clickflag=false;

}

return true;

}

return false;

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442934.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

摩托罗拉ex232java_摩托罗拉ex232r如何刷机?摩托罗拉ex232r评测

导语&#xff1a;随着 高科 技产业的发展&#xff0c;手机作为一个深受影响的产业&#xff0c;其竞争的激烈程度也是不言而喻的。市场好比战场&#xff0c;而为了在这个手机战场中赢 得胜 利&#xff0c;不论国内或者是国外的各大厂商也都全身心的投入到新技术的开发和新产品的…

java 对象序列化 数组_序列化-将任何对象转换为j中的字节数组

您要执行的操作称为“序列化”。 有几种方法可以做到&#xff0c;但是如果您不需要花哨的东西&#xff0c;我认为使用标准Java对象序列化就可以了。也许您可以使用这样的东西&#xff1f;package com.example;import java.io.ByteArrayInputStream;import java.io.ByteArrayOut…

java中gradlew 命令_gradle命令学习

概述命令学习比较枯燥&#xff0c;全部是例子~gradle版本假设你的本地gradle已经安装配置完成。没有安装配置的&#xff0c;可以参考 gradle安装C:\Users\yueling.DANGDANG>gradle -v------------------------------------------------------------Gradle 4.5.1------------…

java jsp常见问题_Java和Jsp编程中应注意的几个常见问题

1、对应String类型的对象使用println()方法时&#xff0c;如果对象为null&#xff0c;将打印null而不是引发NullPointerException&#xff0c;由此引用的问题是容易造成错觉&#xff0c;对于以后对字符串的操作容易引起问题。2、引发NullPointerException异常&#xff0c;主要原…

JAVA捕捉输入格式异常_Java学习(四).异常处理

异常处理任何一个软件或程序都可能在运行的过程中出现故障&#xff0c;问题的关键是故障出现以后如何处理&#xff1f;谁来处理&#xff1f;怎样处理&#xff1f;处理后系统能否恢复正常的运行&#xff1f;本章在介绍Java处理这类问题基本方法的基础上&#xff0c;讨论包含异常…

java1.5以后新增的特性_jdk1.5之后的一些新特性

oreach与数组加强的for循环(Enhanced forLoop)for(type element : array) {System.out.println(element)....}int[] arr {1, 2, 3, 4, 5};for(int element : arr)System.out.println(element);泛型(Generics)• J2SE5.0之后&#xff0c;针对泛型(Generics)设计的解决方…

php去除html属性,PHP如何去掉所有HTML标签?

PHP如何去掉所有HTML标签&#xff1f;在PHP中可以使用“strip_tags()”函数将字符串中的所有HTML标签去除&#xff0c;该函数用于从字符串中去除HTML和 PHP标记&#xff0c;其语法是“strip_tags(str)”&#xff0c;其参数str表示要进行操作的字符串&#xff0c;返回值为处理后…

php启用 asynchdns,在 PHP 中使用 Promise + co/yield 协程

摘要: 我们知道 JavaScript 自从有了 Generator 之后&#xff0c;就有了各种基于 Generator 封装的协程。其中 hprose 中封装的 Promise 和协程库实现了跟 ES2016 的 async/await 一样的功能&#xff0c;并且更加灵活。我们还知道 PHP 自从 5.5 之后&#xff0c;也引入了 Gener…

php 获取agent,PHP代码 解析HTTP_USER_AGENT 获取客户端操作系统

*** 获取客户端操作系统信息包括win10* param null* author Jea杨* return string*/function GetOS(){$agent $_SERVER[HTTP_USER_AGENT];$os false;if (preg_match(/win/i, $agent) && strpos($agent, 95)){$os Windows 95;}else if (preg_match(/win 9x/i, $age…

php怎么写for循环,PHP for循环的写法和示例

For循环是最近的循环语句之一&#xff0c;无论哪种语言&#xff0c;都有这个循环语句&#xff0c;也是我们工作中常用的循环方法。语法规则&#xff1a;for (expr1; expr2; expr3){要执行的代码}expr1&#xff1a;表示循环开始的地方expr2 &#xff1a;循环的条件&#xff0c;如…

php批量下载网络图片,php批量下载网页图片并替换路径为本地

一篇文章复制过来&#xff0c;发现图片路径都是别人网站的&#xff0c;如何一键下载这些图片到本地&#xff0c;并且修改成为本地的路径呢。代码如下 复制代码/*** 获取替换文章中的图片路径* param string $xstr 内容 采集网页的content* param string $keyword 创建照片的文件…

php获取本机root,通过PHP执行root命令

慕村225694在尝试之前&#xff0c;请阅读整个文章&#xff0c;然后进行选择。使用二进制包装器(带有suid位)的解决方案1)创建一个脚本(最好是.sh)&#xff0c;其中包含要作为root用户运行的脚本。# cat > php_shell.sh < wrapper.c < #include #include int mai…

php位值,php中,如何取得一个整型值的低位和高位值?

整型值可以使用十进制&#xff0c;十六进制&#xff0c;八进制或二进制表示&#xff0c;前面可以加上可选的符号(- 或者 )。二进制表达的 integer 自 PHP 5.4.0 起可用。要使用八进制表达&#xff0c;数字前必须加上 0(零)。要使用十六进制表达&#xff0c;数字前必须加上 0x。…

java寂静岭 攻略,GBA版《寂静岭》HARRY篇图文流程攻略

“Play Novel: Silent Hill”是KONAMI于2001年3月21日在GBA上推出的一款文字冒险游戏&#xff0c;剧情内容取自同社的恐怖冒险游戏——Silent Hill(《寂静岭》)。游戏基本上是纯粹的文字冒险游戏&#xff0c;过程中穿插着几段动画CG作为过场&#xff0c;游戏中绝大部分的图片和…

php 实现百度坐标转换,PHP实现腾讯与百度坐标转换

废话不多说&#xff0c;直接上代码public function coordinate_switch($a,$b){//百度转腾讯坐标转换$x (double)$b - 0.0065;$y (double)$a - 0.006;$x_pi 3.14159265358979324;$z sqrt($x * $x$y * $y) - 0.00002 * sin($y * $x_pi);$theta atan2($y,$x) - 0.000003 * co…

实验一熟悉matlab环境,数字信号处理报告实验一:熟悉MATLAB环境.doc

数字信号处理报告实验一&#xff1a;熟悉MATLAB环境.doc实验一熟悉MATLAB环境一 实验目的1. 熟悉MATLAB的主要操作命令。2. 学会简单的矩阵输入和数据读写。3. 掌握简单的绘图命令。4. 用MATLAB编程并学会创建函数。5. 观察离散系统的频率响应。二 实验内容2.用MATLAB实现下列序…

MySQL中使用CASE出错,如何在MySQL中正确使用CASE..WHEN

如何在MySQL中正确使用CASE..WHEN这里是一个演示查询&#xff0c;注意它非常简单&#xff0c;仅在base_price为0的位置获取&#xff0c;并且仍然select条件3&#xff1a;SELECT CASE course_enrollment_settings.base_price WHEN course_enrollment_settings.base_price 0 THE…

matlab rootdir,Python cfg.ROOT_DIR属性代码示例

# 需要导入模块: from fast_rcnn.config import cfg [as 别名]# 或者: from fast_rcnn.config.cfg import ROOT_DIR [as 别名]def demo(net, image_name, classes):"""Detect object classes in an image using pre-computed object proposals.""&quo…

5g算法matlab怎么用,使用 MATLAB 开发 5G NR 设计

请选择其一AlabamaAlaska美属萨摩亚APO/FPO AAAPO/FPO AEAPO/FPO APArizonaArkansasCaliforniaCaroline IslandsColoradoConnecticutDelawareDistrict of ColumbiaFlorida格鲁吉亚关岛HawaiiIdahoIllinoisIndianaIowaKansasKentuckyLouisianaMaineMariana Islands马绍尔群岛Mar…

微擎cloud.mod.php,微擎“could not resolve ”、“could not resolve host”系列问题处理办法...

今天一个用户跟我说模块安装出现”could not resolve host:update.we10d.cn“报错&#xff0c;昨天在整理最近十个月用户搜索关键词排行的时候也发现”could not resolve host“、”could not resolve“还是占据不小的比例的。其实这类报错的处理方法大致相同&#xff0c;都是/…