在AWT中绘制矩形会导致出现矩形的闪烁

时间:2017-05-18 22:50:24

标签: java awt

当我试图绘制它时,我很难弄清楚为什么桨会闪烁。我知道我可以使用JFrame,但我使用awt是出于某种特定原因。任何帮助将不胜感激!

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;

public class Pong extends Applet implements Runnable,KeyListener{

//applet height and width
final int WIDTH = 700;
final int HEIGHT = 500;
Thread thread;
HumanPaddle p1;

//initialize the applet
public void init(){
    this.resize(WIDTH, HEIGHT);

    this.addKeyListener(this);
    p1 = new HumanPaddle(1);
    thread = new Thread(this);
    thread.start();
}

//paints the applet
public void paint(Graphics g){
    g.setColor(Color.black);
    g.fillRect(0,0,WIDTH,HEIGHT);

    p1.draw(g);
}

//continually updated within the program
public void update(Graphics g){
    paint(g);
}


public void run() {
    //infinite loop with a error try and catch
    for(;;){

        p1.move();

        repaint();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }



    }

}

public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_UP){
        p1.setUpAccel(true);
    }
    else if(e.getKeyCode() == KeyEvent.VK_DOWN){
        p1.setDownAccel(true);
    }

}


public void keyReleased(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_UP){
        p1.setUpAccel(false);
    }
    else if(e.getKeyCode() == KeyEvent.VK_DOWN){
        p1.setDownAccel(false);
    }


}


public void keyTyped(KeyEvent arg0) {


}

}

并且HumanPaddle类是:

import java.awt.Color;
import java.awt.Graphics;

public class HumanPaddle implements Paddle{

//declare variables
double y;
double yVel;
boolean upAccel;
boolean downAccel;
int player;
int x;
final double GRAVITY = 0.94;

public HumanPaddle(int player){
    upAccel = false;
    downAccel = false;
    y = 210;
    yVel = 0;
    if(player == 1)
        x = 20;
    else    
        x = 660;
}

public void draw(Graphics g) {
    g.setColor(Color.white);
    g.fillRect(x, (int)y, 20, 80);

 }


 public void move() {
    if(upAccel){
        yVel -= 2;
    }
    else if(downAccel){
        yVel += 2;
    }
    else if(!upAccel && !downAccel){
        yVel *= GRAVITY;
    }
    y += yVel;

}


//setters
public void setY(double y){
    this.y = y;
}
public void setYVel(double yVel){
    this.yVel = yVel;
}
public void setUpAccel(boolean upAccel){
    this.upAccel = upAccel;
}
public void setDownAccel(boolean downAccel){
    this.downAccel = downAccel;
}
public void setPlayer(int player){
    this.player = player;
}
public void setX(int x){
    this.x = x;
}

//getters
public int getY(){
    return (int)y;
}
public double getYVel(){
    return yVel;
}
public boolean getUpAccel(){
    return upAccel;
}
public boolean getDownAccel(){
    return downAccel;
}
public int getPlayer(){
    return player;
}
public int getX(){
    return x;
}




}

1 个答案:

答案 0 :(得分:-1)

如果你遇到这个问题,我解决它的方法是我添加了repaint(),它在paint方法中调用了update方法。它必须在您按顺序绘制之后,以便递归循环不会将该部分保留。我希望我解释说得对,有点像新手程序员!