2017-05-18 189 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方法中的重繪不是實現動畫的方法。對於實際問題,充其量只是一種破解。 –