如下内容,为什么我的JFrame里会有残影效果?

import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;
import javax.swing.JFrame;
public class BallGame extends JFrame { Image sun = Toolkit.getDefaultToolkit().createImage("images/sun.jpg"); double x=100; double y=100; double degree = 3.14/3; public void paint(Graphics g){ g.drawImage(sun, (int)x,(int)y, null); x = x+ 10*Math.cos(degree); y = y+10*Math.sin(degree); if(y>300-80){ degree = - degree; } if(x>500-100){ degree = 3.14-degree; } if(x<0){ degree = 3.14-degree; } if(y<30){ degree = -degree; } } void launchJFrame(){ setSize(500, 300); //设置窗口的长宽 setLocation(50, 50); //坐标 setTitle("My JFrame"); //标题 setBackground(Color.blue); //背景色 setVisible(true); //可见性 new PaintThread().start(); } public static void main(String[] args){ new BallGame().launchJFrame(); JFrame launchJrame= new JFrame(); }
class PaintThread extends Thread { public void run(){ while(true){ repaint();//重画窗口! try{ Thread.sleep(40); //40ms 1s=1000ms }catch (Exception e) { e.printStackTrace(); } }
}
}
}



偶然的你
浏览 246回答 1
1回答

犯罪嫌疑人X

在这个方法 上 添加public void paint(Graphics g){super.paint(g);...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MySQL
Java