2014-06-17 5 views
0

Java에서 JApplet을 코딩하고 있지만 더블 버퍼링으로 깜박임이 제거되지 않습니다!Java Applet_Dubble 버퍼링이 작동하지 않습니다.

어떻게해야합니까?

이 코드의 중요한 부분이다, 내 생각 (당신이 더 알고하십시오 필요하면 말해) :

// Background (dubble buffering) 
    private Image backbuffer; 
    private Graphics backg; 

//Init method 
      // Dubble-Buffering 
     backbuffer = createImage(getWidth(), getHeight()); 
     backg = backbuffer.getGraphics(); 
     backg.setColor(this.getBackground()); 

//Overrided update method 
     public void update(Graphics g) { 
      g.drawImage(backbuffer, 0, 0, this); 
      getToolkit().sync(); 
     } 

를 내가 얻을 수있는 모든 도움을 주셔서 감사합니다! :)

+0

'Swing'이 ([기본적으로 더블 버퍼링]입니다 http://docs.oracle.com/javase/tutorial /extra/fullscreen/doublebuf.html), IMHO. 수동으로'update()'메소드를 사용하는 이유는 무엇입니까? 당신의 결정을 자극 한 것은 무엇입니까? 당신이 무엇을하려고했는지에 대한 어떤 생각은 당신이 생각한 것에 관해서 큰 도움이 될 것입니다. –

+0

'Java Docs Container' 클래스의 작은 인용문 : __ "컨테이너를 업데이트합니다.이 컨테이너의 자식 인 경량 구성 요소로 업데이트를 전달합니다.이 메소드가 다시 구현되면 super.update (g)를 호출해야합니다 가벼운 구성 요소가 올바르게 렌더링되거나 paint (g)를 직접 호출 할 수 있습니다. 자식 구성 요소가 g의 현재 클리핑 설정으로 완전히 클리핑되면 update()가 해당 자식으로 전달되지 않습니다. "__ –

+0

미안하지만 저는 Java로 게임 프로그래밍을 처음 접했을 뿐이므로 문제를 해결하기 위해 무언가를 시도했습니다. 분명히 그것이 제대로 작동하지 않는 것처럼 당신이 맞습니다,하지만 어떻게 깜박임을 고쳐야합니까? 모든 코드를 볼 필요가 있습니까? –

답변

0

나는 (앤드류 톰슨 덕분에) 당신에게 더 나은 통찰력을주기 위해 MCVE를 만들었습니다!

"메인"-class :

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JApplet; 
import javax.swing.JButton; 
import javax.swing.Timer; 

public class Test extends JApplet implements ActionListener { 

JButton start; 

int delay; 
Timer tm; 

// Falling balls 
int n; // Total balls 
Ball[] ball; // Array with the balls in 

int score; 

// The amount of balls falling at the same time (increases by one every 
// 10:th score) 
int ballNr; 

// Comment to the game 
String comment; 

public void init() { 

    this.setLayout(null); 
    this.setSize(600, 500); 
    this.getContentPane().setBackground(Color.cyan); 
    this.setFocusable(true); 

    score = 0; 
    ballNr = 3; 
    comment = "Avoid the balls!"; 

    // Buttons 
    start = new JButton("START"); 
    add(start); 
    start.setBounds(0, 400, 200, 100); 
    start.addActionListener(this); 

    // The timer 
    delay = 12; 
    tm = new Timer(delay, this); 

    // The falling balls 
    n = 12; // Number of balls in total 
    ball = new Ball[n]; 
    // Declaring twelve new instances of the ball-object with a 
    // "reference array" 
    for (int i = 0; i < n; i++) { 
     ball[i] = new Ball(); 
    } 
} 

// Paint-method // 
public void paint(Graphics g) { 
    super.paint(g); 

    // Every 10:th score, the game adds one ball until you reach 100 = 
    // win! (ballNr + (int)(score * 0.1) -> ballNr increases by one 
    // every 10:th score 
    for (int i = 0; i < ballNr + (int) (score * 0.1); i++) { 
     // Score can't be higher than 100 
     if (score < 100) { 
      g.setColor(ball[i].getCol()); 
      g.fillOval(ball[i].getXLoc(), ball[i].getYLoc(), 
        ball[i].getSize(), ball[i].getSize()); 
     } 
    } 

    // Draw the score and the comment 
    g.setColor(Color.black); 
    g.setFont(new Font("Arial", Font.BOLD, 24)); 
    g.drawString("SCORE: " + score, 440, 40); 

    g.setColor(Color.red); 
    g.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 28)); 
    g.drawString(comment, 0, 40); 

} 

// ACTIONLISTENER // 
public void actionPerformed(ActionEvent e) { 

    if (e.getSource() == start) { 
     tm.start(); 
     this.requestFocusInWindow(); // Make the runner component have focus 
    } 

    // Every 10:th score, the game adds one ball until you reach 100 = 
    // win! (ballNr + (int)(score * 0.1) -> ballNr increases by one 
    // every 10:th score 
    for (int i = 0; i < ballNr + (int) (score * 0.1); i++) { 
     // Score can't pass 100, because then you have won the game 
     if (score < 100) { 
      ball[i].setYLoc(ball[i].getYLoc() + ball[i].getVel()); 
     } 
    } 

    // If any ball is out of bounds (then the score increases by one) 
    for (int i = 0; i < n; i++) { 
     if (outOfBounds(ball[i])) { 
      ball[i] = new Ball(); 
     } 
    } 
    repaint(); 
} 

// If the ball is out of the screen 
public boolean outOfBounds(Ball ball) { 
    if (ball.getYLoc() >= 500) { 
     score++; 
     return true; 
    } else { 
     return false; 
    } 
} 

// Updates new balls 
public void updateBalls() { 
    for (int i = 0; i < n; i++) { 
     ball[i] = new Ball(); 
    } 
} 
} 

하위 클래스

import java.awt.Color; 
import java.util.Random; 

public class Ball { 
// Private variables 
private Random r; // Generating random positions and 
        // sizes; 
private int size; // Size of the ball 
private int vel; // Ball velocity 
private int nrOfCol; // Color code (see ballColor-method) 
private Color col; 
private int xLoc; 
private int yLoc; 

// Constructor 
public Ball() { 
    r = new Random(); 
    size = r.nextInt(40) + 10; // 10px - 50 px 
    vel = r.nextInt(6) + 1; // Speed btw 1-5 px/delay 
    nrOfCol = r.nextInt(8) + 1; // Specific nr from 1-9 
    col = ballColor(); 
    xLoc = r.nextInt(550); 
    yLoc = 0;} 

public Ball(int xPos, int yPos, int size, int vel, Color col) { 
    this.xLoc = xPos; 
    this.yLoc = yPos; 
    this.size = size; 
    this.vel = vel; 
    this.col = col; 
} 

// A method to generate different colors of the balls 
public Color ballColor() { 
    Color col; 
    switch (nrOfCol) { 
    case 1: 
     col = Color.black; 
     break; 
    case 2: 
     col = Color.red; 
     break; 
    case 3: 
     col = Color.green; 
     break; 
    case 4: 
     col = Color.yellow; 
     break; 
    case 5: 
     col = Color.pink; 
     break; 
    case 6: 
     col = Color.magenta; 
     break; 
    case 7: 
     col = Color.white; 
     break; 
    case 8: 
     col = Color.orange; 
     break; 
    case 9: 
     col = Color.blue; 
     break; 
    default: 
     col = Color.black; 
     // All colors except cyan as it is the background color 
    } 
    return col; 
} 

// Getters & setters 

public int getXLoc() { 
    return xLoc; 
} 

public int getYLoc() { 
    return yLoc; 
} 

public void setYLoc(int y) { 
    yLoc = y; 
} 

public int getSize() { 
    return size; 
} 

public int getVel() { 
    return vel; 
} 

public Color getCol() { 
    return col; 
} 
}