运行起来会闪烁。把
super.paint(g);去掉的话 钟面上走过的指针画的线不会消失。求解答拜托了
package com.ttc;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class test10 extends JFrame {
private JPanel contentPane;
Point2D.Double[] points = new Point2D.Double[60];
Point p = new Point(300,300);
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test10 frame = new test10();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public test10() {
addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
do_this_windowActivated(e);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 302);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
}
public void paint(Graphics g){
super.paint(g);
int radius = 200;
g.setColor(Color.blue);
for(int i=0;i<=59;i++){
points[i] = new Point2D.Double(p.x+radius*Math.sin(Math.PI/30*i),p.y-radius*Math.cos(Math.PI/30*i));
}
for(int i=0;i<=59;i++){
if(i%5==0){
g.fillOval((int)points[i].x-8, (int)points[i].y-8, 16, 16);
}else{
g.fillOval((int)points[i].x-4,(int)points[i].y-4,8,8);
}
}
Calendar time = new GregorianCalendar();
int hour = time.get(Calendar.HOUR);
int minute = time.get(Calendar.MINUTE);
int second = time.get(Calendar.SECOND);
Graphics2D g1 = (Graphics2D)g;
g1.setColor(Color.green);
g1.draw(new Line2D.Double(p,points[second]));
g1.setColor(Color.blue);
g1.draw(new Line2D.Double(p,points[minute]));
g1.setColor(Color.red);
g1.draw(new Line2D.Double(p,points[hour*5+minute/12]));
}
protected void do_this_windowActivated(WindowEvent e) {
new Thread() {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
} |