怎样用java 程序写一个时钟程序?最好有代码!谢谢
若以下回答无法解决问题,邀请你更新回答
展开全部
import java.awt.Color;
import java.util.*;
import java.awt.*;
import java.applet.*;
public class Clock extends Applet implements Runnable
{
Thread timer=null;
Label label;
int lastxs=50,lastys=30,lastxm=50,lastym=30,lastxh=50,lastyh=30;
public void init()
{
label=new Label(" ");
setBackground(Color.white);
add(label);
}
public void paint(Graphics g)
{
int xh,yh,xm,ym,xs,ys,s,m,h,xcenter,ycenter;
Date rightnow=new Date();
String today=rightnow.toLocaleString();
label.setText(today);
s=rightnow.getSeconds();
m=rightnow.getMinutes();
h=rightnow.getHours();
xcenter=100;
ycenter=80;
xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*45+xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*45+ycenter);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*45+xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*45+ycenter);
xh=(int)(Math.cos((h*30+m*2)*3.14f/180-3.14f/2)*30+xcenter);
yh=(int)(Math.sin((h*30+m*2)*3.14f/180-3.14f/2)*30+ycenter);
g.setFont(new Font("TimesToman",Font.PLAIN,14));
g.setColor(Color.orange);
g.fill3DRect(xcenter-50,ycenter-50,100,100,true);
g.setColor(Color.darkGray);
g.drawString("12",xcenter-5,ycenter-37);
g.drawString("3",xcenter+40,ycenter+3);
g.drawString("6",xcenter-3,ycenter+45);
g.drawString("9",xcenter-45,ycenter+3);
g.setColor(Color.orange);
if(xs!=lastxs||ys!=lastys)
{
g.drawLine(xcenter,ycenter,lastxs,lastys);
}
if(xm!=lastxm||ym!=lastym)
{
g.drawLine(xcenter,ycenter-1,lastxm,lastym);
g.drawLine(xcenter-1,ycenter,lastxm,lastym);
}
if(xh!=lastxh||yh!=lastyh)
{
g.drawLine(xcenter,ycenter-1,lastxh,lastyh);
g.drawLine(xcenter-1,ycenter,lastxh,lastyh);
}
g.setColor(Color.red);
g.drawLine(xcenter,ycenter,xs,ys);
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=xm;
lastym=ym;
lastxh=xh;
lastyh=yh;
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);
timer.start();
}
}
public void stop()
{
timer=null;
}
public void run()
{
while(timer!=null)
{
try
{
Thread.sleep(1000);
}catch(InterruptedException ie){}
repaint();
}
timer=null;
}
public void update(Graphics g)
{
paint(g);
}
}
import java.util.*;
import java.awt.*;
import java.applet.*;
public class Clock extends Applet implements Runnable
{
Thread timer=null;
Label label;
int lastxs=50,lastys=30,lastxm=50,lastym=30,lastxh=50,lastyh=30;
public void init()
{
label=new Label(" ");
setBackground(Color.white);
add(label);
}
public void paint(Graphics g)
{
int xh,yh,xm,ym,xs,ys,s,m,h,xcenter,ycenter;
Date rightnow=new Date();
String today=rightnow.toLocaleString();
label.setText(today);
s=rightnow.getSeconds();
m=rightnow.getMinutes();
h=rightnow.getHours();
xcenter=100;
ycenter=80;
xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*45+xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*45+ycenter);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*45+xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*45+ycenter);
xh=(int)(Math.cos((h*30+m*2)*3.14f/180-3.14f/2)*30+xcenter);
yh=(int)(Math.sin((h*30+m*2)*3.14f/180-3.14f/2)*30+ycenter);
g.setFont(new Font("TimesToman",Font.PLAIN,14));
g.setColor(Color.orange);
g.fill3DRect(xcenter-50,ycenter-50,100,100,true);
g.setColor(Color.darkGray);
g.drawString("12",xcenter-5,ycenter-37);
g.drawString("3",xcenter+40,ycenter+3);
g.drawString("6",xcenter-3,ycenter+45);
g.drawString("9",xcenter-45,ycenter+3);
g.setColor(Color.orange);
if(xs!=lastxs||ys!=lastys)
{
g.drawLine(xcenter,ycenter,lastxs,lastys);
}
if(xm!=lastxm||ym!=lastym)
{
g.drawLine(xcenter,ycenter-1,lastxm,lastym);
g.drawLine(xcenter-1,ycenter,lastxm,lastym);
}
if(xh!=lastxh||yh!=lastyh)
{
g.drawLine(xcenter,ycenter-1,lastxh,lastyh);
g.drawLine(xcenter-1,ycenter,lastxh,lastyh);
}
g.setColor(Color.red);
g.drawLine(xcenter,ycenter,xs,ys);
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=xm;
lastym=ym;
lastxh=xh;
lastyh=yh;
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);
timer.start();
}
}
public void stop()
{
timer=null;
}
public void run()
{
while(timer!=null)
{
try
{
Thread.sleep(1000);
}catch(InterruptedException ie){}
repaint();
}
timer=null;
}
public void update(Graphics g)
{
paint(g);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用applet写.图形方面和三角函数有关,时间和进程的延迟方面有关,大学java教材上面有这个题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
awt 包画图
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询