我写了一个JAVA时钟程序,请高手帮我看看问题出在哪
运行起来会闪烁。把super.paint(g);去掉的话钟面上走过的指针画的线不会消失。求解答拜托了publicclassClockextendsJFrame{priva...
运行起来会闪烁。把
super.paint(g);去掉的话 钟面上走过的指针画的线不会消失。求解答拜托了
public class Clock 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 {
Clock frame = new Clock();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Clock() {
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();
}
} 展开
super.paint(g);去掉的话 钟面上走过的指针画的线不会消失。求解答拜托了
public class Clock 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 {
Clock frame = new Clock();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Clock() {
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();
}
} 展开
4个回答
展开全部
paint();方法是通过继承得到(java.awt. Window)
api中是这样说的:
绘制容器。该方法将 paint 转发给任意一个此容器子组件的轻量级组件。如果重新实现此方法,那么应该调用 super.paint(g) 方法,从而可以正确地呈现轻量级组件。如果通过 g 中的当前剪切设置完全剪切某个子组件,则不会将 paint() 转发给这个子组件。
不知道对你有没有帮助
api中是这样说的:
绘制容器。该方法将 paint 转发给任意一个此容器子组件的轻量级组件。如果重新实现此方法,那么应该调用 super.paint(g) 方法,从而可以正确地呈现轻量级组件。如果通过 g 中的当前剪切设置完全剪切某个子组件,则不会将 paint() 转发给这个子组件。
不知道对你有没有帮助
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为什么要添加windows的侦听器?
去掉它直接new 线程就可以了。
---------------------------------------------------------------------------------------
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.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Clock 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 {
Clock frame = new Clock();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Clock() {
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));
new Thread() {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
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]));
}
}
去掉它直接new 线程就可以了。
---------------------------------------------------------------------------------------
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.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Clock 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 {
Clock frame = new Clock();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Clock() {
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));
new Thread() {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
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]));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
运行过,感觉有点闪烁,如果弄个双缓冲图像就应该不会这样了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
答案:38、独立,永远。不管,感情还是金钱。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询