急!我毕业设计做的一个时钟不能运动!可能是少了一个刷新的方法。请教高手指点!
原代码如下:
import java.awt.*;
import java.awt.geom.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
public class app {
public static void main(String[] args) {
TextTestFrame frame=new TextTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TextTestFrame extends JFrame
{
public TextTestFrame()
{
setTitle("Clock");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
Container contentPane=getContentPane();
JPanel panel=new JPanel();
contentPane.add(panel,BorderLayout.SOUTH);
clock=new ClockPanel();
contentPane.add(clock,BorderLayout.CENTER);
GregorianCalendar calendar=new GregorianCalendar();
int hours=calendar.get(Calendar.HOUR);
int minutes=calendar.get(Calendar.MINUTE);
int seconds=calendar.get(Calendar.SECOND);
clock.setTime(hours,minutes);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=300;
private JTextField hourField;
private JTextField minuteField;
private ClockPanel clock;
}
class ClockPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
Ellipse2D circle=new Ellipse2D.Double(100,100,2*RADIUS,2*RADIUS);
g2.draw(circle);
double hourAngle=Math.toRadians(90-360*minutes/(12*60));
drawHand(g2,hourAngle,HOUR_HAND_LENGTH);
double minuteAngle=Math.toRadians(90-360*minutes/60);
drawHand(g2,minuteAngle,MINUTE_HAND_LENGTH);
double secondAngle=Math.toRadians(90-6*seconds);
drawHand(g2,secondAngle,SECOND_HAND_LENGTH);
}
public void drawHand(Graphics2D g2,double angle,double handLength)
{
Point2D end=new Point2D.Double(2*RADIUS+handLength*Math.cos(angle),2*RADIUS-handLength*Math.sin(angle));
Point2D center=new Point2D.Double(2*RADIUS,2*RADIUS);
g2.draw(new Line2D.Double(center,end));
}
public void setTime(int h,int m)
{
minutes=h*60+m;
repaint();
}
private double minutes=0;
private double seconds=0;
private double RADIUS=100;
private double MINUTE_HAND_LENGTH=0.8*RADIUS;
private double HOUR_HAND_LENGTH=0.6*RADIUS;
private double SECOND_HAND_LENGTH=1*RADIUS;
}