关于线程的问题!急等!
我想编写一个时钟程序,在JPanel上绘制了时钟的图形,想动态的使它自动走动,可是不知道该怎么办了,我想用线程实现repaint()方法,可是遇到问题了,我的代码如下,希望高手指教,谢谢
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class Clock {
public static void main(String [] args){
ClockFrame frame = new ClockFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ClockFrame extends JFrame
{
public ClockFrame()
{
setTitle("Clock");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
Container contentPane = getContentPane();
setLayout(new BorderLayout());
ClockPanel panel = new ClockPanel();
// DatePanel datePanel = new DatePanel();
contentPane.add(panel,BorderLayout.CENTER);
// contentPane.add(datePanel,BorderLayout.SOUTH);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
}
class ClockPanel extends JPanel{
public ClockPanel()
{
setSize(300,300);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(200,200,350,350);
g2.draw(circle);
drawLines(g2);
}
public void drawLines(Graphics2D g2)
{
Date date = Calendar.getInstance().getTime();
int hour = date.getHours();
int minute = date.getMinutes();
int second = date.getSeconds();
double hourAngle = Math.toRadians(90 - 360 * hour / 12);
double minuteAngle = Math.toRadians(90 - 360 * minute / 60);
double secondAngle = Math.toRadians(90 - 360 * second / 60);
Point2D hourEnd = new Point2D.Double(200 + hourLength * Math.cos(hourAngle),
200 - hourLength * Math.sin(hourAngle));
g2.draw(new Line2D.Double(center,hourEnd));
Point2D minuteEnd = new Point2D.Double(200 + minuteLength * Math.cos(minuteAngle),
200 - minuteLength * Math.sin(minuteAngle));
g2.draw(new Line2D.Double(center,minuteEnd));
Point2D secondEnd = new Point2D.Double(200 + secondLength * Math.cos(secondAngle),
200 - secondLength * Math.sin(secondAngle));
g2.draw(new Line2D.Double(center,secondEnd));
}
private double RADIUS = 150;
private double hourLength = 0.5 * RADIUS;
private double minuteLength = 0.7 * RADIUS;
private double secondLength = 0.9 * RADIUS;
private Point2D center = new Point2D.Double(200,200);
// private Point2D end;
}