日历

2008 11.22 Sat
      1
2345678
9101112131415
16171819202122
23242526272829
30      
«» 2008 - 11 «»

日志分类

文章搜索

日志文章

2007年12月01日 12:33:36

JAVA多线程

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.applet.*;
import java.io.*;
public class TimerTest
{
public static void main(String[] args)
{
JFrame f = new TimerTestFrame();
f.show();
}
}
class TimerTestFrame extends JFrame
{
public TimerTestFrame()
{
    setSize(550, 550);
setTitle("TimerTest");
addWindowListener(new WindowAdapter()
{
   public void windowClosing(WindowEvent e)
{
   System.exit(0);
}
});
Container c = getContentPane();
c.setLayout(new GridLayout(2, 4));
c.add(new ClockCanvas("San Jose", "GMT-8"));
c.add(new ClockCanvas("Taipei", "GMT+8"));
c.add(new ClockCanvas("Berlin", "GMT+1"));
c.add(new ClockCanvas("New York", "GMT-5"));
c.add(new ClockCanvas("Cairo", "GMT+2"));
c.add(new ClockCanvas("Bombay", "GMT+5"));
c.add(new ClockCanvas("Beijing", "GMT+4"));
c.add(new ClockCanvas("Huanong", "GMT+3"));
}
}
interface TimerListener
{
void timeElapsed(Timer t);
}
class Timer extends Thread
{
private TimerListener target;
private int interval;
public Timer(int i, TimerListener t)
{
    target = t;
interval = i;
setDaemon(true);
}
public void run()
{
    try
{
    while (!interrupted())
{
   sleep(1);
target.timeElapsed(this);
}
}
catch(InterruptedException e) {}
}
}
class ClockCanvas extends JPanel implements TimerListener
{
private int seconds = 0;
private int seconds1 = 0;
private String city;
private int offset;
private GregorianCalendar calendar;
private final int LOCAL = 16;

public ClockCanvas(String c, String tz)
{
    city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer t = new Timer(1000, this);
t.start();
setSize(200, 200);
}
public void paintComponent(Graphics g)
{

/* BufferedReader din = new BufferedReader(new InputStreamReader(System.in));
try
{
g.drawString("请设置开始时间:", 35, 125);
i=Integer.parseInt(din.readLine());
}
catch(IOException ae)
{
   System.out.println("caught a io exception"+ae);
   }*/    
    super.paintComponent(g);    
    g.setColor(Color.black);
    g.drawOval(0, 0, 100, 100);
    g.drawString("12", 46, 12);
    g.drawString("3", 90, 55);
    g.drawString("6", 46, 98);
    g.drawString("9", 2, 54);    
double hourAngle = 2 * Math.PI
* (seconds - 3 * 60 * 60) / (12 * 60 * 60);
double minuteAngle = 2 * Math.PI
* (seconds - 15 * 60) / (60 * 60);
double secondAngle = 2 * Math.PI
* (seconds - 15) / 60;
g.setColor(Color.RED);
g.drawLine(50, 50, 50 + (int)(30
* Math.cos(hourAngle)),
50 + (int)(30 * Math.sin(hourAngle)));
g.setColor(Color.green);
g.drawLine(50, 50, 50 + (int)((40) * Math.cos(minuteAngle)),
50 + (int)(40 * Math.sin(minuteAngle)));
g.setColor(Color.CYAN);
g.drawLine(50, 50, 50 + (int)((50) * Math.cos(secondAngle)),
50 + (int)(45 * Math.sin(secondAngle)));


g.drawString(city, 35, 115);

}
public void timeElapsed(Timer t)
{
    calendar.setTime(new Date());
seconds = calendar.get(Calendar.HOUR) * 60 * 60+ calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND);
// calendar.setTime(new Date());
// seconds2 = calendar.get(Calendar.HOUR) * 60 * 60+ calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND);
// if( seconds2-seconds==1)
repaint();
}

}

类别: 无分类 |  评论(0) |  浏览(1650) |  收藏
发表评论
看不清楚,换一张