2017-05-18 3 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

이 문제가있는 경우는, 그런데 내가 그것을 내가 페인트 방법의 update 메소드를 호출하는,() 재 페인트를 추가 한 것이 었 해결했다. 순환 루프가 그 부분을 빠져 나가지 않도록 순서대로 그려야합니다. 나는 그 말이 맞다는 것을, newby 프로그래머라고 설명했으면 좋겠다.

+0

페인트 메서드에서 다시 그리기를 호출하는 것은 애니메이션을 얻는 방법이 아닙니다. 실제 문제는 기껏해야 해킹입니다. –

관련 문제