2017-05-18 192 views
0

我很难弄清楚为什么当我试图绘制它时桨会闪烁。我知道我可以使用JFrame,但是我使用awt的原因是特定的。任何帮助,将不胜感激!在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; 
} 




} 
+2

小程序没有双缓冲,因此闪烁;他们也被弃用,不再支持,所以我建议不要一般使用它们。我建议改变策略并使用'JPanel'作为基本组件,因为它默认是双缓冲。然后,您可以将其添加到您想要的任何容器中。看看[Swing中的自定义绘画](https://docs.oracle.com/javase/tutorial/uiswing/painting/)以获取更多详细信息 – MadProgrammer

+0

谢谢,这只是我遵循的教程而已。我肯定会研究JPanel,看起来好多了! –

回答

-1

如果你有这个问题,我的方式解决它是我加入重绘(),它调用paint方法的更新方法。它必须在你按顺序绘制之后,以便递归循环不会离开该部分。我希望我解释说得对,有点新手程序员!

+0

调用paint方法中的重绘不是实现动画的方法。对于实际问题,充其量只是一种破解。 –