java制作桌面时钟

java制作桌面时钟,第1张

如果楼主只是想做个简单的电子显示式的时钟,可以有下面的一段代码: Calendar c=new GregorianCalendar(); int hour=cget(CalendarHOUR); //得到时间中的小时 int minute=cget(CalendarMINUTE); //得到分 int second=cget(CalendarSECOND); //得到秒 当然,上面的代码得导入import javautil;包 再创建个线程用于自动更新就行了,如果想做成带时针,分针,秒针能走动的那就用canvas 类的画图方法,结合一些数学知识就可以完成图形的了,还有二楼说的data类中的很多方法已经被java6列为过时的了

import javautil;

import javaawt;

import javaapplet;

import javatext;

import javaawtevent;

public class Alarm extends Applet implements Runnable

{

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

Image gif1; //clockp:闹钟的外壳,闹铃和报时物

boolean setflag=false,stopflag=false,cancelflag=false;

Panel setpanel;

//获取声音文件

AudioClip ring=getAudioClip(getCodeBase(), "1mid");

Button setbutton=new Button("SET");

Button cancelbutton=new Button("CANCEL");

Button stopbutton=new Button("STOP");

//响应按钮事件

private ActionListener setli=new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

setflag=true;

}

};

private ActionListener cancelli=new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

setflag=true;

}

};

private ActionListener stopli=new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

ringstop();

//清除的方法

//gclearRect(83,280,20,30);

}

};

Label note1=new Label("Alarm clock:");

//GregorianCalendar提供的是一个日历式的东东,上面又多了很多的参数,是方便 *** 作了不少。而Date类的功能远不及其,求个和日期有联系的还要自己计算。

GregorianCalendar cal=new GregorianCalendar();

GregorianCalendar cal2=new GregorianCalendar();

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

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

String lastdate=dfformat(dummy);

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

Date dat=null;

Date timeNow;

Color fgcol=Colorblue;

Color fgcol2=ColordarkGray;

Color backcolor=Colorblue;

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

int i;

int s,m,h;

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

//在Applet程序中,首先自动调用初始化完成必要的初始化工作,紧接着自动调用start,在进入执行程序和返回到该页面时被调用,而从该页面转到别的页面时,stop被调用,关闭浏览器时,执行destroy。

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

{

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

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

setpanel=new Panel();

setpanelsetLayout(null);

setpaneladd(note1);

note1setBounds(30,100,60,20);

note1setBackground(backcolor);

note1setForeground(Colorblack);

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

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

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

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

hlabel2=new Label();

mlabel2=new Label();

slabel2=new Label();

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

setpaneladd(sethour);

sethoursetBounds(fieldx,fieldy2,fieldw,fieldh);

sethoursetBackground(Colorwhite);

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

setpaneladd(hlabel2);

hlabel2setText("h");

hlabel2setBackground(backcolor);

hlabel2setForeground(Colorblack);

hlabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

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

setpaneladd(setmin);

setminsetBounds(fieldx,fieldy2,fieldw,fieldh);

setminsetBackground(Colorwhite);

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

setpaneladd(mlabel2);

mlabel2setText("m");

mlabel2setBackground(backcolor);

mlabel2setForeground(Colorblack);

mlabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

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

setpaneladd(setsec);

setsecsetBounds(fieldx,fieldy2,fieldw,fieldh);

setsecsetBackground(Colorwhite);

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

setpaneladd(slabel2);

slabel2setText("s");

slabel2setBackground(backcolor);

slabel2setForeground(Colorblack);

slabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);

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

setpaneladd(cancelbutton);

setpaneladd(setbutton);

setpaneladd(stopbutton);

cancelbuttonsetBounds(90,180,40,20);

setbuttonsetBounds(140,180,40,20);

stopbuttonsetBounds(522,180,40,20);

setbuttonaddActionListener(setli);

cancelbuttonaddActionListener(cancelli);

stopbuttonaddActionListener(stopli);

stopbuttonsetVisible(false);

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

add(setpanel);

setpanelsetBounds(300,1,250,420);

setpanelsetBackground(backcolor);

/int xcenter,ycenter,s,m,h;

//闹钟中心点所在位置

xcenter=145;

ycenter=162;

s=(int)calget(CalendarSECOND);

m=(int)calget(CalendarMINUTE);

h=(int)calget(CalendarHOUR_OF_DAY);

//初始化指针位置

lastxs=(int)(Mathcos(s314f/30-314f/2)30+xcenter);

lastys=(int)(Mathsin(s314f/30-314f/2)30+ycenter);

lastxm=(int)(Mathcos(m314f/30-314f/2)25+xcenter);

lastym=(int)(Mathsin(m314f/30-314f/2)25+ycenter);

lastxh=(int)(Mathcos((h30+m/2)314f/180-314f/2)18+xcenter);

lastyh=(int)(Mathsin((h30+m/2)314f/180-314f/2)18+ycenter);

lasts=s; /

MediaTracker mt=new MediaTracker(this);//为给定组件创建一个跟踪媒体的MediaTracker对象,把添加到被跟踪的组

//Java允SappletHTML所在的位置(decument base)下dY料,也允Sapplet钠涑淌酱a所在的位置(code base)下dY料。藉由呼叫getDocumentBase()cgotCodeBase()可得到URL物件。@些函湍阏业侥阆胂螺d的n案的位置

//clockp=getImage(getDocumentBase(),"11png");

gif1=getImage(getCodeBase(),"2gif");

//i为id号

mtaddImage(gif1,i++);

try

{

mtwaitForAll();

}

catch(InterruptedException e)

{};//等待加载结束

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

}

//窗口显示有改变的时候调用paint

public void paint(Graphics g)

{//重写paint()方法

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

int xcenter,ycenter;

String today;

xcenter=148;

ycenter=186;

dat=new Date();

//用当前时间初始化日历时间

calsetTime(dat);

//读取当前时间

s=(int)calget(CalendarSECOND);

m=(int)calget(CalendarMINUTE);

h=(int)calget(CalendarHOUR_OF_DAY);

//换一种时间表达形式

today=dfformat(dat);

//指针位置

xs=(int)(Mathcos(s314f/30-314f/2)30+xcenter);

ys=(int)(Mathsin(s314f/30-314f/2)30+ycenter);

xm=(int)(Mathcos(m314f/30-314f/2)25+xcenter);

ym=(int)(Mathsin(m314f/30-314f/2)25+ycenter);

xh=(int)(Mathcos((h30+m/2)314f/180-314f/2)12+xcenter);

yh=(int)(Mathsin((h30+m/2)314f/180-314f/2)12+ycenter);

//设置字体和颜色

gsetFont(F);

//前景色

gsetColor(getBackground()); //取背景色的

gdrawImage(gif1,75,110,this);

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

gdrawString(today,55,415);

//画指针

gdrawLine(xcenter,ycenter,xs,ys);

gdrawLine(xcenter,ycenter-1,xm,ym); //(x1,y1,x2,y2)

gdrawLine(xcenter-1,ycenter,xm,ym);

gdrawLine(xcenter,ycenter-1,xh,yh);

gdrawLine(xcenter-1,ycenter,xh,yh);

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

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

Date dat2=new Date();

cal2setTime(dat2);

//读取当前时间

currh=(int)cal2get(CalendarSECOND);

currm=(int)cal2get(CalendarMINUTE);

currs=(int)cal2get(CalendarHOUR_OF_DAY);

//这样做的话说我API已过时

//timeNow=new Date();

//currh=new Integer(timeNowgetHours());

//currm=new Integer(timeNowgetMinutes());

//currs=new Integer(timeNowgetSeconds());

if(setflag)

{ //判断是否设置了闹钟

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

if((currhintValue()==IntegervalueOf(sethourgetText())intValue())&&(currmintValue()==IntegervalueOf(setmingetText())intValue())&&(currsintValue()==IntegervalueOf(setsecgetText())intValue()))

{

ringplay();

gdrawImage(gif1,83,280,this);

stopbuttonsetVisible(true);

}

timedelta=currmintValue()60+currsintValue()-IntegervalueOf(setmingetText())intValue()60-IntegervalueOf(setsecgetText())intValue();

if((timedelta>=30))

{

//若当前时间与闹钟相差时间超过30秒,闹钟自动停

ringstop();

//清除的方法

gclearRect(83,280,20,30);

}

}

dat=null;

}

public void start()

{

if(timer==null)

{

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

timerstart();

}

}

public void stop()

{

timer=null;

}

//给创建线程后start之后自动执行的函数

public void run()

{

//在run()方法中,调用repaint()方法,以重绘小程序区,进行时钟显示的更新。接着调用sleep方法让当前线程(也就是我们创建的线程clockthread)睡眠1000毫秒,因为我们每秒钟要更新一下显示,所以让它睡眠1秒

while(timer!=null)

{

try

{

timersleep(1000);

}

catch(InterruptedException e)

{}

//调用repaint时,会首先清除掉paint方法之前的画的内容,再调用paint方法

repaint();//刷新画面

}

timer=null;

}

//当AWT接收到一个applet的重绘请求时,它就调用applet的 update(),默认地,update() 清除applet的背景,然后调用 paint()。重载 update(),将以前在paint()中的绘图代码包含在update()中,从而避免每次重绘时将整个区域清除

//有两种方法可以明显地减弱闪烁:重载 update()或使用双缓冲。

//使用双缓冲技术:另一种减小帧之间闪烁的方法是使用双缓冲,它在许多动画Applet中被使用。其主要原理是创建一个后台图像,将需要绘制的一帧画入图像,然后调用DrawImage()将整个图像一次画到屏幕上去;好处是大部分绘制是离屏的,将离屏图像一次绘至屏幕上比直接在屏幕上绘制要有效得多,大大提高做图的性能。

// 双缓冲可以使动画平滑,但有一个缺点,要分配一张后台图像,如果图像相当大,这将需要很大一块内存;当你使用双缓冲技术时,应重载 update()。

public void update(Graphics g)

{

Image offscreen_buf=null;

//采用双缓冲技术的update()方法

if(offscreen_buf==null)

offscreen_buf=createImage(600,420);

Graphics offg=offscreen_bufgetGraphics();

offgclipRect(1,1,599,419);

paint(offg);

Graphics ong=getGraphics();

ongclipRect(1,1,599,419);

ongdrawImage(offscreen_buf,0,0,this);

}

/ Creates a new instance of AlarmClock /

}

这里有一个时钟的类,你看一下吧,不明白你的 100毫秒的时钟 是什么意思。

import javaawtBasicStroke;

import javaawtBorderLayout;

import javaawtCanvas;

import javaawtColor;

import javaawtGraphics;

import javaawtGraphics2D;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaawtgeomLine2D;

import javautilDate;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJPanel;

import javaxswingTimer;

public class ClockAs extends Canvas implements ActionListener {

static JButton jb = new JButton("开始");

static JButton jb1 = new JButton("暂停");

Date date;

Timer secondTime;

int hour, munite, second;

Line2D secondLine, muniteLine, hourLine;

int a, b, c;

double pointSX[] = new double[60],// 用来表示秒针端点坐标的数组

pointSY[] = new double[60], pointMX[] = new double[60], // 用来表示分针端点坐标的数组

pointMY[] = new double[60], pointHX[] = new double[60], // 用来表示时针端点坐标的数组

pointHY[] = new double[60];

ClockAs() {

secondTime = new Timer(1000, this);

pointSX[0] = 0; // 12点秒针位置

pointSY[0] = -100;

pointMX[0] = 0; // 12点分针位置

pointMY[0] = -90;

pointHX[0] = 0; // 12点时针位置

pointHY[0] = -70;

double angle = 6 MathPI / 180; // 刻度为6度

for (int i = 0; i < 59; i++) // 计算出各个数组中的坐标

{

pointSX[i + 1] = pointSX[i] Mathcos(angle) - Mathsin(angle)

pointSY[i];

pointSY[i + 1] = pointSY[i] Mathcos(angle) + pointSX[i]

Mathsin(angle);

pointMX[i + 1] = pointMX[i] Mathcos(angle) - Mathsin(angle)

pointMY[i];

pointMY[i + 1] = pointMY[i] Mathcos(angle) + pointMX[i]

Mathsin(angle);

pointHX[i + 1] = pointHX[i] Mathcos(angle) - Mathsin(angle)

pointHY[i];

pointHY[i + 1] = pointHY[i] Mathcos(angle) + pointHX[i]

Mathsin(angle);

}

for (int i = 0; i < 60; i++) {

pointSX[i] = pointSX[i] + 120; // 坐标平移

pointSY[i] = pointSY[i] + 120;

pointMX[i] = pointMX[i] + 120; // 坐标平移

pointMY[i] = pointMY[i] + 120;

pointHX[i] = pointHX[i] + 120; // 坐标平移

pointHY[i] = pointHY[i] + 120;

}

secondLine = new Line2DDouble(0, 0, 0, 0);

muniteLine = new Line2DDouble(0, 0, 0, 0);

hourLine = new Line2DDouble(0, 0, 0, 0);

secondTimestart(); // 秒针开始计时

}

public void paint(Graphics g) {

for (int i = 0; i < 60; i++) // 绘制表盘上的小刻度和大刻度

{

int m = (int) pointSX[i];

int n = (int) pointSY[i];

if (i % 5 == 0) {

gsetColor(Colorred);

gfillOval(m - 4, n - 4, 8, 8);

} else {

gsetColor(Colorcyan);

gfillOval(m - 2, n - 2, 4, 4);

}

}

gfillOval(115, 115, 10, 10); // 钟表中心的实心圆

Graphics2D g_2d = (Graphics2D) g;

g_2dsetColor(Colorred);

g_2ddraw(secondLine);

BasicStroke bs = new BasicStroke(3f, BasicStrokeCAP_ROUND,

BasicStrokeJOIN_MITER);

g_2dsetStroke(bs);

g_2dsetColor(Colorblue);

g_2ddraw(muniteLine);

bs = new BasicStroke(6f, BasicStrokeCAP_BUTT, BasicStrokeJOIN_MITER);

g_2dsetStroke(bs);

g_2dsetColor(Colorgreen);

g_2ddraw(hourLine);

}

public void actionPerformed(ActionEvent e) {

if (egetSource() == secondTime) {

date = new Date();

String s = datetoString();

hour = IntegerparseInt(ssubstring(11, 13));

munite = IntegerparseInt(ssubstring(14, 16));

second = IntegerparseInt(ssubstring(17, 19)); // 获取时间中的秒

int h = hour % 12;

a = second; // 秒针端点的坐标

b = munite; // 分针端点的坐标

c = h 5 + munite / 12; // 时针端点的坐标

secondLinesetLine(120, 120, (int) pointSX[a], (int) pointSY[a]);

muniteLinesetLine(120, 120, (int) pointMX[b], (int) pointMY[b]);

hourLinesetLine(120, 120, (int) pointHX[c], (int) pointHY[c]);

repaint();

}

if (egetSource() == jb) {

secondTimestart();

}

if (egetSource() == jb1) {

secondTimestop();

}

}

public static void main(String args[]) {

JFrame win = new JFrame("时钟");

JPanel jp = new JPanel();

jpadd(jb);

jpadd(jb1);

ClockAs clock = new ClockAs();

jbaddActionListener(clock);

jb1addActionListener(clock);

winadd(clock, BorderLayoutCENTER);

winadd(jp, "South");

winsetVisible(true);

winsetSize(246, 300);

winsetDefaultCloseOperation(3);

winvalidate();

}

}

你是要设计一个 类 还是要编写一个时钟程序

类的话 没什么实际意义 timer 和 date 一起用就是时钟了

要模仿 个时钟的 话 要写 JS样式就可以 要这个类的话 打开jdK的date类 就能看到 时间类是怎么设计的

package 时钟;

import javaawtColor;

import javaawtFont;

import javaawtGraphics;

import javaawtimageBufferedImage;

import javautilCalendar;

import javautilDate;

import javaxswingJApplet;

/

@author Jiang

/

public class Clock2 extends JApplet implements Runnable{

double PI=MathPI;//圆周率

double hourlen=50;//时针长度

double minutelen=70;//分针长度

double secondlen=90;//秒针长度

int yuanxinx=100,yuanxiny=100;//圆心坐标

int banjin=100;//半径长度

int qix,qiy,zhongxh,zhongxm,zhongxs,zhongyh,zhongym,zhongys;//各指针的起点和终点坐标(起点一致)

Thread th;

BufferedImage buf;

int hour,minute,second;//记录当前时间

Calendar calendar;//用于获取当前时间

Graphics g1;//用于内存绘图

public void init(){

hour=minute=second=0;

buf=new BufferedImage(400,400,BufferedImageTYPE_INT_ARGB);//控制内存绘图区域大小

g1=bufgetGraphics();//创建缓冲绘图区

thissetSize(400,400);

}

public void start(){

if(th==null)

th=new Thread(this);

thstart();//启动线程

}

public void stop(){

th=null;//终止线程

}

public void run(){//线程的覆盖方法

while(true){

try{

thsleep(1000);

}catch(Exception e){}

//用背景色填充绘图区,擦除上次绘图

g1setColor(thisgetBackground());

//g1fillRect(yuanxinx-banjin,yuanxiny-banjin,2banjin,2banjin);

g1fillRect(0,0,400,400);

repaint();

}

}

public void paint(Graphics g){

//将图像先画到缓冲区

g1setColor(thisgetForeground());

//绘制时钟刻盘(刻度长度为8)

g1drawOval(yuanxinx-banjin,yuanxiny-banjin,yuanxinx+banjin,yuanxiny+banjin);

//特殊位置简便画法

//g1drawLine(yuanxinx-banjin,yuanxiny,yuanxinx-banjin+8,yuanxiny);//9

//g1drawLine(yuanxinx,yuanxiny-banjin,yuanxinx,yuanxiny-banjin+8);//12

//g1drawLine(yuanxinx+banjin,yuanxiny,yuanxinx+banjin-8,yuanxiny);//3

//g1drawLine(yuanxinx,yuanxiny+banjin,yuanxinx,yuanxiny+banjin-8);//6

//1

int qixx=(int)(yuanxinx-banjinMathcos((05+10/6)PI));

int qiyy=(int)(yuanxiny-banjinMathsin((05+10/6)PI));

int zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+10/6)PI));

int zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+10/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//2

qixx=(int)(yuanxinx-banjinMathcos((05+20/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+20/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+20/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+20/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//3

qixx=(int)(yuanxinx-banjinMathcos((05+30/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+30/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+30/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+30/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//4

qixx=(int)(yuanxinx-banjinMathcos((05+40/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+40/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+40/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+40/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//5

qixx=(int)(yuanxinx-banjinMathcos((05+50/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+50/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+50/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+50/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//6

qixx=(int)(yuanxinx-banjinMathcos((05+60/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+60/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+60/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+60/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//7

qixx=(int)(yuanxinx-banjinMathcos((05+70/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+70/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+70/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+70/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//8

qixx=(int)(yuanxinx-banjinMathcos((05+80/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+80/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+80/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+80/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//9

qixx=(int)(yuanxinx-banjinMathcos((05+90/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+90/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+90/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+90/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//10

qixx=(int)(yuanxinx-banjinMathcos((05+100/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+100/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+100/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+100/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//11

qixx=(int)(yuanxinx-banjinMathcos((05+110/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+110/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+110/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+110/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//12

qixx=(int)(yuanxinx-banjinMathcos((05+120/6)PI));

qiyy=(int)(yuanxiny-banjinMathsin((05+120/6)PI));

zhongxx=(int)(yuanxinx-(banjin-8)Mathcos((05+120/6)PI));

zhongyy=(int)(yuanxiny-(banjin-8)Mathsin((05+120/6)PI));

g1drawLine(qixx,qiyy,zhongxx,zhongyy);

//g1drawLine(150,15,140,25);

//g1drawString(""+qixx+" "+qiyy+" "+zhongxx+" "+zhongyy,200,300);

//获取当前时间

calendar=CalendargetInstance();

hour=calendarget(CalendarHOUR);

minute=calendarget(CalendarMINUTE);

second=calendarget(CalendarSECOND);

//计算指针坐标

qix=qiy=yuanxinx;

zhongxh=(int)(yuanxinx-hourlenMathcos((05+20hour/12)MathPI));

zhongyh=(int)(yuanxiny-hourlenMathsin((05+20hour/12)MathPI));

zhongxm=(int)(yuanxinx-minutelenMathcos((05+20minute/60)MathPI));

zhongym=(int)(yuanxiny-minutelenMathsin((05+20minute/60)MathPI));

zhongxs=(int)(yuanxinx-secondlenMathcos((05+20second/60)MathPI));

zhongys=(int)(yuanxiny-secondlenMathsin((05+20second/60)MathPI));

//绘制指针

Font oldfont=thisgetFont();

g1setColor(Colorred);

g1setFont(new Font(oldfontgetName(),FontBOLD,oldfontgetStyle()+20));

g1drawLine(qix,qiy,zhongxh,zhongyh);

g1setColor(Colorgreen);

g1setFont(new Font(oldfontgetName(),FontBOLD,oldfontgetStyle()+15));

g1drawLine(qix,qiy,zhongxm,zhongym);

g1setColor(Colorblue);

g1setFont(new Font(oldfontgetName(),FontBOLD,oldfontgetStyle()+10));

g1drawLine(qix,qiy,zhongxs,zhongys);

//输出时间、日期

g1setFont(new Font("Time New Roman",FontBOLD,25));

g1setColor(ColorPINK);

g1drawString(""+hour+":"+minute+":"+second,150,240);

g1setFont(new Font("Time New Roman",FontBOLD,18));

g1setColor(Colorgreen);

g1drawString("中国标准时间 Chinese Standard Time",25,280);

g1setFont(new Font("Time New Roman",FontBOLD,20));

g1setColor(Colorblue);

g1drawString(new Date()toString(), 25,320);

//g1drawString(""+hour+" "+minute+" "+second, 200,200);

gdrawImage(buf, 0,0, this);//将缓冲区图像画到JApplet上

}

public void update(Graphics g){//覆盖方法

paint(g);

}

}

import javautil;

import javaawt;

import javaapplet;

import javatext;

public class AlarmClock extends Applet implements Runnable

{

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

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

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=dfformat(dummy);

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

Date dat=null;

Date timeNow=null;

Color fgcol=Colorblue;

Color fgcol2=ColordarkGray;

Panel setpanel;

Color backcolor=Colorpink;

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();

setpanelsetLayout(null);

setpaneladd(note1);

setpaneladd(note2);

note1setBounds(30,100,60,20);

note1setBackground(backcolor);

note1setForeground(Colorblack);

note2setBounds(30,180,60,20);

note2setBackground(backcolor);

note2setForeground(Colorblack);

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);

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

setpaneladd(showhour);

showhoursetBounds(fieldx,fieldy1,fieldw,fieldh);

showhoursetBackground(Colorwhite);

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

setpaneladd(hlabel1);

hlabel1setText("时");

hlabel1setBackground(backcolor);

hlabel1setForeground(Colorblack);

hlabel1setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

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

setpaneladd(showmin);

showminsetBounds(fieldx,fieldy1,fieldw,fieldh);

showminsetBackground(Colorwhite);

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

setpaneladd(mlabel1);

mlabel1setText("分");

mlabel1setBackground(backcolor);

mlabel1setForeground(Colorblack);

mlabel1setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

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

setpaneladd(showsec);

showsecsetBounds(fieldx,fieldy1,fieldw,fieldh);

showsecsetBackground(Colorwhite);

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

setpaneladd(slabel1);

slabel1setText("秒");

slabel1setBackground(backcolor);

slabel1setForeground(Colorblack);

slabel1setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=50;

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

setpaneladd(sethour);

sethoursetBounds(fieldx,fieldy2,fieldw,fieldh);

sethoursetBackground(Colorwhite);

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

setpaneladd(hlabel2);

hlabel2setText("时");

hlabel2setBackground(backcolor);

hlabel2setForeground(Colorblack);

hlabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

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

setpaneladd(setmin);

setminsetBounds(fieldx,fieldy2,fieldw,fieldh);

setminsetBackground(Colorwhite);

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

setpaneladd(mlabel2);

mlabel2setText("分");

mlabel2setBackground(backcolor);

mlabel2setForeground(Colorblack);

mlabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

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

setpaneladd(setsec);

setsecsetBounds(fieldx,fieldy2,fieldw,fieldh);

setsecsetBackground(Colorwhite);

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

setpaneladd(slabel2);

slabel2setText("秒");

slabel2setBackground(backcolor);

slabel2setForeground(Colorblack);

slabel2setBounds(fieldx+fieldw+3,fieldy2,14,20);

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

onbutton=new Button("开");

offbutton=new Button("关");

setpaneladd(onbutton);

setpaneladd(offbutton);

onbuttonsetBounds(90,180,40,20);

offbuttonsetBounds(140,180,40,20);

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

setpaneladd(info1);

info1setBackground(backcolor);

info1setForeground(Colorblue);

info1setBounds(50,50,150,20);

setpaneladd(info2);

info2setBackground(backcolor);

info2setForeground(Colorblue);

info2setBounds(150,280,100,20);

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

add(setpanel);

setpanelsetBounds(300,1,250,420);

setpanelsetBackground(backcolor);

//获取声音文件

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

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

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

int xcenter,ycenter,s,m,h;

xcenter=145;

ycenter=162;

s=(int)calget(CalendarSECOND);

m=(int)calget(CalendarMINUTE);

h=(int)calget(CalendarHOUR_OF_DAY);

//初始化指针位置

lastxs=(int)(Mathcos(s314f/30-314f/2)30+xcenter);

lastys=(int)(Mathsin(s314f/30-314f/2)30+ycenter);

lastxm=(int)(Mathcos(m314f/30-314f/2)25+xcenter);

lastym=(int)(Mathsin(m314f/30-314f/2)25+ycenter);

lastxh=(int)(Mathcos((h30+m/2)314f/180-314f/2)18+xcenter);

lastyh=(int)(Mathsin((h30+m/2)314f/180-314f/2)18+ycenter);

lasts=s;

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

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

gif1=getImage(getDocumentBase(),"gif1gif");

gif2=getImage(getDocumentBase(),"gif2gif");

clock6=getImage(getDocumentBase(),"clock6gif");

clock7=getImage(getDocumentBase(),"clock7gif");

mtaddImage(clockp,i++);

mtaddImage(gif1,i++);

mtaddImage(gif2,i++);

mtaddImage(clock6,i++);

mtaddImage(clock7,i++);

try{mtwaitForAll();}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();

calsetTime(dat);

//读取当前时间

s=(int)calget(CalendarSECOND);

m=(int)calget(CalendarMINUTE);

h=(int)calget(CalendarHOUR_OF_DAY);

today=dfformat(dat);

//指针位置

xs=(int)(Mathcos(s314f/30-314f/2)30+xcenter);

ys=(int)(Mathsin(s314f/30-314f/2)30+ycenter);

xm=(int)(Mathcos(m314f/30-314f/2)25+xcenter);

ym=(int)(Mathsin(m314f/30-314f/2)25+ycenter);

xh=(int)(Mathcos((h30+m/2)314f/180-314f/2)18+xcenter);

yh=(int)(Mathsin((h30+m/2)314f/180-314f/2)18+ycenter);

//设置字体和颜色

gsetFont(F);

gsetColor(fgcol);

gsetColor(fgcol2);

gsetColor(getBackground());

gfillRect(1,1,634,419);

gdrawImage(clockp,75,110,this);

gdrawImage(clock6,83,280,this);

gsetColor(fgcol2);

gsetColor(getBackground());

gsetColor(fgcol2);

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

gdrawString(today,55,415);

gdrawLine(xcenter,ycenter,xs,ys);

gsetColor(fgcol);

//画指针

gdrawLine(xcenter,ycenter-1,xm,ym);

gdrawLine(xcenter-1,ycenter,xm,ym);

gdrawLine(xcenter,ycenter-1,xh,yh);

gdrawLine(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<strike_times){

flag=true;

gdrawImage(gif2,115,35,this);

if(lasts!=s){

if(strike){

counter++;

dangerplay();//播放闹铃声

}

if(strike)strike=false;

else strike=true;

}

}

else {

counter=0;

flag=false;

}

}

else

gdrawImage(gif1,115,35,this);

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

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

timeNow=new Date();

currh=new Integer(timeNowgetHours());

currm=new Integer(timeNowgetMinutes());

currs=new Integer(timeNowgetSeconds());

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

if(currhintValue()!=IntegervalueOf(showhourgetText())intValue())

showhoursetText(currhtoString());

if(currmintValue()!=IntegervalueOf(showmingetText())intValue())

showminsetText(currhtoString());

if(currsintValue()!=IntegervalueOf(showsecgetText())intValue())

showsecsetText(currhtoString());

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

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

if((currhintValue()==fixh)&&(currmintValue()==fixm)&&(currsintValue()==fixs))

clickflag=true;

timedelta=currmintValue()60+currsintValue()-fixm60-fixs;

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

chirpplay();

gdrawImage(clock7,83,280,this);

}

else{

chirpstop();

clickflag=false;

}

}

if(lasts!=s)

ipAuplay();//播放滴答声

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实例化

timerstart();

}

}

public void stop(){

timer=null;

}

public void run(){

while(timer!=null){

try{timersleep(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_bufgetGraphics();

offgclipRect(1,1,599,419);

paint(offg);

Graphics ong=getGraphics();

ongclipRect(1,1,599,419);

ongdrawImage(offscreen_buf,0,0,this);

}

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

if(evttarget instanceof Button){

String lable=(String)arg;

if(lableequals("开")){

setalerm=true;

//获取输入的时间

fixh=IntegervalueOf(sethourgetText())intValue();

fixm=IntegervalueOf(setmingetText())intValue();

fixs=IntegervalueOf(setsecgetText())intValue();

clickflag=false;

}

if(lableequals("关")){

setalerm=false;

if(chirp!=null)

chirpstop();

clickflag=false;

}

return true;

}

return false;

}

}

import javaawt;

import javaappletApplet;

import javautilCalendar;

import javatextSimpleDateFormat;

import javautilDate;

public class ClockApplet extends Applet implements Runnable //Applet支持线程

{

private Thread athread; //线程

private SimpleDateFormat sdateformat; //日期格式

public void init()

{

thissetBackground(Colorwhite);//背景颜色设为白色

thisathread = null;

}

public void paint(Graphics g)

{

thissdateformat = new SimpleDateFormat("hh时mm分ss秒");

gdrawString(thissdateformatformat(new Date()),25,131);

Calendar rightnow = CalendargetInstance();

int second = rightnowget(CalendarSECOND);

int minute = rightnowget(CalendarMINUTE);

int hour = rightnowget(CalendarHOUR);

//半径

int R_H = 20,R_M = 4,R_S = 4;

//时针的坐标

//x ====(9-3)[0-6] (3-9)[6-0]

//y ====(12-6)[0-6] (6-12)[6-0]

int H_x ;

int H_y;

//x

if(hour == 0)

{

hour = 12;

}

if( hour >= 3 && hour <= 9 )

{

H_x = R_HMathabs(hour - 9);

}

else

{

if(hour > 9)

{

H_x = R_HMathabs(hour - 9);

}

else

{

H_x = R_HMathabs(hour+3);

}

}

//y

if( hour >= 6 && hour <= 12 )

{

H_y = R_HMathabs(hour - 12);

}

else

{

H_y = R_Hhour;

}

//分针的坐标

int M_x;

int M_y;

if(minute == 0)

{

minute = 60;

}

if( minute >= 15 && minute <= 45 )

{

M_x = R_MMathabs(minute - 45);

}

else

{

if(minute > 45)

{

M_x = R_MMathabs(minute - 45);

}

else

{

M_x = R_MMathabs(minute+15);

}

}

//y

if( minute >= 30 && minute < 60 )

{

M_y = R_MMathabs(minute - 60);

}

else

{

M_y = R_Mminute;

}

//秒针的坐标

int S_x;

int S_y;

if(second == 0)

{

second = 60;

}

if( second >= 15 && second <= 45 )

{

S_x = R_SMathabs(second - 45);

}

else

{

if(second > 45)

{

S_x = R_SMathabs(second - 45);

}

else

{

S_x = R_SMathabs(second+15);

}

}

//y

if( second >= 30 && second <= 60 )

{

S_y = R_SMathabs(second - 60);

}

else

{

S_y = R_Ssecond;

}

// gdrawString(StringvalueOf(second),25,50);

// gdrawString(StringvalueOf(minute),25,60);

// gdrawString(StringvalueOf(hour),25,70);

// gdrawString(StringvalueOf(H_x),25,80);

// gdrawString(StringvalueOf(H_y),25,90);

gdrawOval(0,0,120,120);//距离相差10像素

gsetColor(ColordarkGray);

gdrawString("9",5,65);

gdrawString("3",110,65);

gdrawString("12",55,15);

gdrawString("6",55,115);

gdrawString("1",80,20);

gdrawString("2",100,40);

gdrawString("4",100,90);

gdrawString("5",80,110);

gdrawString("7",30,110);

gdrawString("8",10,90);

gdrawString("10",10,40);

gdrawString("11",30,20);

gsetColor(Colorred);

gdrawLine(60,60,H_x,H_y);//前一个点表示起点,另一个表示终点

gsetColor(Colorblue);

gdrawLine(60,60,M_x,M_y);

gsetColor(Coloryellow);

gdrawLine(60,60,S_x,S_y);

}

public void start()

{

if(athread == null)

{

athread = new Thread(this);

athreadstart();

}

}

public void stop()

{

if(athread != null)

{

athreadinterrupt();

athread = null;

}

}

public void run()

{

while(athread != null)

{

repaint();

try

{

athreadsleep(1000);

}

catch(InterruptedException e)

{

}

}

}

}

以上就是关于java制作桌面时钟全部的内容,包括:java制作桌面时钟、JAVA 闹钟程序、用 java 写一个clock的类,100毫秒的时钟 求代码。。越简单越好。。最好有注释等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9680009.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存