Hello
I am having having hard time figuring out why my whole screen flickers when I use bufferstrategy. The screen flickers whenever I move my character, and if i move him slowly I can see the page switching between default and black background. The thing is I haven’t even set the background color… isPageFlipping() returns true when i check it. Using 2 buffers when creating bufferstrategy.
The link required for the image to work:
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class GamePractice extends JFrame{
final float SPEED;
final float JUMP_SPEED;
final float X_SPEED;
final long PERIOD;
final int NUM_BUFFERS;
GamePanel mainPane;
private GraphicsDevice gd;
private BufferStrategy bufferStrategy;
int jumpUpCount;
int jumpDownCount;
Action actionJumpKey;
float xV;
float yV;
private volatile boolean running;
boolean didPaint;
boolean bufferStratReady;
boolean moveRight;
boolean moveLeft;
boolean jumpDown;
boolean isJumping;
float totalFPS = 1;
long fpscount1 = 0;
long fpscount2 = 0;
public GamePractice(){
PERIOD = 16;
NUM_BUFFERS = 2;
SPEED = 15;
JUMP_SPEED = 10;
//PERIOD = 17000000L; //17ms
X_SPEED = 6;
jumpUpCount = 0;
jumpDownCount = 0;
}
public static void main(String[] args) {
GamePractice gp = new GamePractice();
gp.go();
}
public void go(){
getRootPane().setDoubleBuffered(false);
setFocusTraversalKeysEnabled(false);
initFullScreen();
mainPane = new GamePanel();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
actionJumpKey = new ActionKeys("jump");
((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("pressed SPACE"),"jump");
((JComponent)getContentPane()).getActionMap().put("jump",actionJumpKey);
((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("pressed LEFT"),"moveLeft");
((JComponent)getContentPane()).getActionMap().put("moveLeft",new ActionKeys("moveLeft"));
((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("pressed RIGHT"),"moveRight");
((JComponent)getContentPane()).getActionMap().put("moveRight",new ActionKeys("moveRight"));
//*************************************************************************************************************
//*************************************************************************************************************
((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released LEFT"),"stopLeft");
((JComponent)getContentPane()).getActionMap().put("stopLeft",new ActionKeys("stopLeft"));
((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released RIGHT"),"stopRight");
((JComponent)getContentPane()).getActionMap().put("stopRight",new ActionKeys("stopRight"));
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"),"quit");
((JComponent)getContentPane()).getActionMap().put("quit",new QuitAction());
mainPane.startGame();
}
private void initFullScreen() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
setUndecorated(true);
setIgnoreRepaint(true);
setResizable(false);
if (!gd.isFullScreenSupported( )) {
System.out.println("Full-screen exclusive mode not supported");
System.exit(0);
}
gd.setFullScreenWindow(this);
setDisplayMode(1920, 1080, 32);
setBufferStrategy();
}
private void setDisplayMode(int w, int h, int d){
DisplayMode dMode = new DisplayMode(w, h, d,
DisplayMode.REFRESH_RATE_UNKNOWN);
if(gd.isDisplayChangeSupported()){
gd.setDisplayMode(dMode);
}else{
System.out.println("HAHA!");
System.exit(0);
}
}
private void setBufferStrategy(){
{ try {
EventQueue.invokeAndWait( new Runnable( ) {
public void run( )
{ createBufferStrategy(NUM_BUFFERS); }
});
}
catch (Exception e) {
System.out.println("Error while creating buffer strategy");
System.exit(0);
}
try {
Thread.sleep(500);
}catch(InterruptedException ex){}
bufferStrategy = getBufferStrategy( );
bufferStratReady = true;
//System.out.println(bufferStrategy.getCapabilities().isPageFlipping());
}
}
/*class TimerLoop implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
mainPane.reDraw();
}
}*/
class GamePanel {
Player mainPlayer;
BufferedImage image = null;
int paintOnce;
public GamePanel(){
try {
image = ImageIO.read(new File("Super Paper Mario_128.png"));
} catch (IOException e) {
e.printStackTrace();
}
mainPlayer = new Player(192, 822, image);
paintOnce = 0;
}
public void startGame() {
// TODO Auto-generated method stub
//Thread loop = new Thread( new MainLoop());
//loop.start();
if (bufferStratReady){
Timer t = new Timer( );
t.scheduleAtFixedRate(new MainLoop() , 0, PERIOD);
running = true;
}
}
private void gameUpdate(){
xV = 0;
//yV = 0;
if (isJumping){
yV = JUMP_SPEED;
if(moveLeft) xV = -X_SPEED;//-SPEED + 4;
if(moveRight) xV = X_SPEED;//SPEED - 4;
} else {
if(moveLeft) xV = -SPEED;
if(moveRight) xV = SPEED;
}
}
private void reDraw(){
final float POSITION_X = mainPlayer.getX();
final float POSITION_Y = mainPlayer.getY();
final int IMAGE_W = mainPlayer.getWidth();
final int IMAGE_H = mainPlayer.getHeight();
didPaint = false;
if (POSITION_X >= 1792){
mainPlayer.setX(1791);
} else if (POSITION_X <= 0){
mainPlayer.setX(1);
} else {
Graphics2D gScr;
try {
gScr = (Graphics2D) bufferStrategy.getDrawGraphics();
if(paintOnce < 1){
mainPlayer.paintImage(gScr);
didPaint = true;
}
paintOnce = 1;
if(xV != 0){
gScr.clearRect((int)POSITION_X, (int)POSITION_Y, IMAGE_W, IMAGE_H);
//gScr.fillRect((int)POSITION_X, (int)POSITION_Y, IMAGE_W, IMAGE_H);
mainPlayer.setX(POSITION_X + xV);
mainPlayer.paintImage(gScr);
didPaint = true;
}
if(jumpDown && jumpDownCount < 15){
didPaint = true;
gScr.clearRect((int)POSITION_X, (int)POSITION_Y - 7, IMAGE_W, IMAGE_H);
//gScr.fillRect((int)POSITION_X, (int)POSITION_Y - 7, IMAGE_W , IMAGE_H );
mainPlayer.setY(POSITION_Y + yV);
mainPlayer.paintImage(gScr);
if (jumpDownCount == 14){
jumpDown = false;
isJumping = false;
actionJumpKey.setEnabled(true);
}
jumpDownCount++;
}
if(isJumping && jumpUpCount < 15){
didPaint = true;
gScr.fillRect((int)POSITION_X, (int)POSITION_Y + 6, IMAGE_W, IMAGE_H);
mainPlayer.setY(POSITION_Y - yV);
mainPlayer.paintImage(gScr);
if (jumpUpCount == 14){
jumpDown = true;
}
jumpUpCount++;
}
gScr.dispose();
if (!bufferStrategy.contentsLost() && didPaint)
bufferStrategy.show( );
else
System.out.println("Contents Lost");
Toolkit.getDefaultToolkit( ).sync( );
}catch (Exception e){
//System.out.println("Graphics context error: " + e);
e.printStackTrace();
running = false;
}
}
}
}