當前位置:編程學習大全網 - 源碼下載 - Java編寫世界時鐘

Java編寫世界時鐘

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

}

}

寫了好久,把分給我吧~~

  • 上一篇:重慶到季芳島自駕攻略季芳島兩日遊攻略
  • 下一篇:長安逸達上市之後,十萬級家轎市場的內卷態勢“加劇在望”?
  • copyright 2024編程學習大全網