怎么去消除java程序的对象的闪烁,请给我具体方案
importjava.awt.*;publicclassBallGameextendsFrame{Imagesun=Toolkit.getDefaultToolkit()...
import java.awt.*;
public class BallGame extends Frame{
Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg");
double x=100;
double y=100;
double degree=3.14/3;
public void paint(Graphics g){
System.out.println("被重画了一次");
g.drawImage(sun,(int)x,(int)y,null);
x=x+10*Math.cos(degree);
y=y+10*Math.sin(degree);
if(y>300-30){
degree=-degree;
}
if(x>500-30){
degree=3.14-degree;
}
if(y<30){
degree=-degree;
}
if(x<0){
degree=3.14-degree;
}
}
void launchFrame(){
setSize(500,300);
setLocation(50,50);
setTitle("我的作品");
setBackground(Color.black);
setVisible(true);
new PaintThread().start();
}
public static void main(String[] args){
new BallGame().launchFrame();
System.out.println("我是这个游戏的创建者");
}
class PaintThread extends Thread {
public void run(){
while(true){
repaint();
try{
Thread.sleep(40);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
} 展开
public class BallGame extends Frame{
Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg");
double x=100;
double y=100;
double degree=3.14/3;
public void paint(Graphics g){
System.out.println("被重画了一次");
g.drawImage(sun,(int)x,(int)y,null);
x=x+10*Math.cos(degree);
y=y+10*Math.sin(degree);
if(y>300-30){
degree=-degree;
}
if(x>500-30){
degree=3.14-degree;
}
if(y<30){
degree=-degree;
}
if(x<0){
degree=3.14-degree;
}
}
void launchFrame(){
setSize(500,300);
setLocation(50,50);
setTitle("我的作品");
setBackground(Color.black);
setVisible(true);
new PaintThread().start();
}
public static void main(String[] args){
new BallGame().launchFrame();
System.out.println("我是这个游戏的创建者");
}
class PaintThread extends Thread {
public void run(){
while(true){
repaint();
try{
Thread.sleep(40);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
} 展开
1个回答
展开全部
双缓冲技术。
public class StartMain extends Applet{
private Image bufferPage;
public int p_x = 50;
public int p_y = 50;
@Override
public void init() {
resize(800, 600);
MouseListener ml = new MyActionController(this);
addMouseListener(ml);
//start();
RunController runcon = new RunController(this);
new Thread(runcon).start();
PlayerController.addEnemy();
//runcon.run();
}
// @Override
// public void paint(Graphics g){
// MyDrawController.drawAll();
// }
//private int num = 0;
@Override
public void update(Graphics g) {
if(bufferPage==null){
//System.out.println("================"+num);
//num++;
bufferPage = createImage(800, 600);
Graphics gbuffer = bufferPage.getGraphics();
MyDrawController.g = gbuffer;
}
// MyDrawController.p_x = p_x;
// MyDrawController.p_y = p_y;
//paint(MyDrawController.g);
MyDrawController.drawAll();
g.drawImage(bufferPage, 0, 0, null);
}
}
主要是重写update方法
public class StartMain extends Applet{
private Image bufferPage;
public int p_x = 50;
public int p_y = 50;
@Override
public void init() {
resize(800, 600);
MouseListener ml = new MyActionController(this);
addMouseListener(ml);
//start();
RunController runcon = new RunController(this);
new Thread(runcon).start();
PlayerController.addEnemy();
//runcon.run();
}
// @Override
// public void paint(Graphics g){
// MyDrawController.drawAll();
// }
//private int num = 0;
@Override
public void update(Graphics g) {
if(bufferPage==null){
//System.out.println("================"+num);
//num++;
bufferPage = createImage(800, 600);
Graphics gbuffer = bufferPage.getGraphics();
MyDrawController.g = gbuffer;
}
// MyDrawController.p_x = p_x;
// MyDrawController.p_y = p_y;
//paint(MyDrawController.g);
MyDrawController.drawAll();
g.drawImage(bufferPage, 0, 0, null);
}
}
主要是重写update方法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询