Java编写世界时钟

 我来答
tuteming
2010-12-02 · 超过13用户采纳过TA的回答
知道答主
回答量:19
采纳率:0%
帮助的人:30.6万
展开全部
package Time;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class TimerTest extends JFrame{
TimerPanel tp;
TimerTest(){
setTitle("世界时钟");
setSize(500,300);
tp=new TimerPanel();
Thread t=new Thread(tp);
t.start();
this.add(tp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TimerTest();
}

}
class TimerPanel extends JPanel implements Runnable{
Time[] t=new Time[6];
TimerPanel(){
t[0]=new Time(0,0,"北京",8);
t[1]=new Time(150,0,"巴黎",1);
t[2]=new Time(300,0,"华盛顿",8);
t[3]=new Time(0,150,"洛杉矶",5);
t[4]=new Time(150,150,"伦敦",0);
t[5]=new Time(300,150,"芝加哥",7);
setBackground(Color.black);
}
public void paint(Graphics g){
super.paint(g);
for(int i=0;i<t.length;i++)
t[i].draw(g);
}
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
}
}
}
/*上面就不注释了,下面的是Time类*/
class Time{
private int x,y;/*每个钟表的坐标*/
private String place;/*每个钟表的名字*/
private int timezone;/*每个钟表的时区,东x区为+x,西x区为-x*/
private Date d;
private long time;
private double hour,minite,second;
Time(int x,int y,String place,int timezone){
this.x=x;
this.y=y;
this.place=place;
this.timezone=timezone;
}
public void draw(Graphics g){
g.setColor(Color.green);

d=new Date();
time=d.getTime();/*获得0时区1970年1月1日0点到现在的毫秒数*/
hour=(((time/1000)+3600*timezone)%43200)*2*Math.PI/3600/12;/*计算时针弧度*/
minite=(time/1000)%3600*2*Math.PI/3600;/*计算分针弧度*/
second=(time/1000)%60*2*Math.PI/60;/*计算秒针弧度*/

/*画出钟表轮廓和时针*/
((Graphics2D)g).setStroke(new BasicStroke(3.0f));
((Graphics2D)g).drawOval(x, y, 100, 100);
((Graphics2D)g).drawLine(x+50, y, x+50, y+5);
((Graphics2D)g).drawLine(x+50, y+100, x+50, y+95);
((Graphics2D)g).drawLine(x, y+50, x+5, y+50);
((Graphics2D)g).drawLine(x+100,y+50, x+95, y+50);
((Graphics2D)g).drawLine(x+50,y+50,(int)(x+50+25*Math.sin(hour)),(int)(y+50-25*Math.cos(hour)));
/*画出分针*/
((Graphics2D)g).setStroke(new BasicStroke(2.0f));
((Graphics2D)g).drawLine(x+50,y+50,(int)(x+50+35*Math.sin(minite)),(int)(y+50-35*Math.cos(minite)));
/*画出秒针*/
((Graphics2D)g).setStroke(new BasicStroke(1.0f));
((Graphics2D)g).drawLine(x+50,y+50,(int)(x+50+45*Math.sin(second)),(int)(y+50-45*Math.cos(second)));
/*画出钟表名字*/
g.setColor(Color.red);
g.drawString(place, x+35, y+120);
}
}

写了好久,把分给我吧~~
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
miracle998
2010-12-03 · 超过17用户采纳过TA的回答
知道答主
回答量:69
采纳率:0%
帮助的人:51.9万
展开全部
嗯,这个答案不错啊,是你自己写的吗?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
汲凡桃083
2010-12-02 · 超过29用户采纳过TA的回答
知道答主
回答量:240
采纳率:0%
帮助的人:87.8万
展开全部
你写不就行了吗!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式