BufferStrategy problems

Hi, i’m new to these forums & game programming.

I decided to try using BufferStrategy, but ran across some problems.

GameBoard.java:


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JPanel;

public class GameBoard extends JPanel implements Runnable
{
	private BufferStrategy myBufferStrategy;
	private Graphics g;
	private Thread t;
	
	private final int PERIOD = 10;
	
	private Ball ball;
	
	public GameBoard()
	{
		ball = new Ball();
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e)
			{
				int keyCode = e.getKeyCode();
				if (keyCode == e.VK_LEFT)
					ball.keyPressed(e);
				if (keyCode == e.VK_RIGHT)
					ball.keyPressed(e);
				if (keyCode == e.VK_UP)
					ball.keyPressed(e);
				if (keyCode == e.VK_DOWN)
					ball.keyPressed(e);
			}
			@Override
			public void keyReleased(KeyEvent e)
			{
				int keyCode = e.getKeyCode();
				if (keyCode == e.VK_LEFT)
					ball.keyReleased(e);
				if (keyCode == e.VK_RIGHT)
					ball.keyReleased(e);
				if (keyCode == e.VK_UP)
					ball.keyReleased(e);
				if (keyCode == e.VK_DOWN)
					ball.keyReleased(e);
			}
		});
	}
	
	public void addNotify()
	{
		super.addNotify();
		startGame();
	}
	
	public void startGame()
	{
		t = new Thread(this);
		t.start();
	}
	
	public void updateGameState()
	{
		ball.move();
	}

	private void render()
	{
		g = myBufferStrategy.getDrawGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, 500, 400);
		g.dispose();
		myBufferStrategy.show();
	}


	public void draw()
	{
		g.setColor(Color.red);
		g.fillOval(ball.x, ball.y, 50, 50);
	}
	
	@Override
	public void run()
	{
		while (true)
		{
			long beforeTime = System.currentTimeMillis();
			
			updateGameState();
			render();
			draw();
			
			long passedTime = System.currentTimeMillis() - beforeTime;
			long sleepTime = PERIOD - passedTime;
			if (sleepTime <= 0) sleepTime = 5;
			try
			{
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) { }
		}
	}
}

The error:

Any idea’s what is wrong? :clue:

Thanks,
Mike/Icedust

Your “myBufferStrategy.getDrawGraphics()” return null object so the “g” was not initted. Why? because your myBufferStrategy itself was also null. You should get the BufferStrategy first from a container, like Canvas, then obtain Graphics object from it.

For example


//global variable
BufferStrategy myStrategy;

//init/constructor
JFrame container = new JFrame("my game");
JPanel myJPanel = (JPanel) container.getContentPane();
Canvas myCanvas = new Canvas();
myCanvas.setIgnoreRepaint(true);
myCanvas.requestFocus();
myCanvas.createBufferStrategy(2);
myStrategy = getBufferStrategy();
myJPanel.add(myCanvas);

//render/draw method
Graphics2D g = (Graphics2D) myStrategy.getDrawGraphics();

Thanks.

Seems I got to read some more about BufferStrategy. I noticed another error, I disposed of graphics before I was done using them… Embarassing ;D

Oh no that’s what you are supposed to do with the Graphics object :stuck_out_tongue:


Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
//drawing code
g.dispose();
strategy.show();

Also, it is best to follow BufferStrategy’s JavaDoc on how to properly set up the rendering code.

Put ra4king’s code right below mine and you ready :point:
Sorry ra4king, if there’s medal then it’s mine :stuck_out_tongue:

Unfortunately, your code doesn’t work. You have to add the canvas and make the frame visible before you can call canvas.createBufferStrategy(2) or else you will get an IllegalStateException :wink:

Also, it is pointless to put the Canvas in a JPanel. Just put it directly on the JFrame’s contentPane (which in itself is a JPanel). :stuck_out_tongue:

Unfortunately, your code doesn't work.

both of us already know that’s pseudocode. Why make it hard? I named it myStrategy and yours is strategy, it won’t work from to begin. It’s my first time to answer faster than you so may I please? 8)

[nitpick]
This isn’t quite true. All you have to do is the Canvas displayable displayable (call pack() on the parent window, for example) to call Canvas.createBufferStrategy(). It doesn’t actually have to be visible on the screen.
[/nitpick]

pack() is such a lousy hack that no one should ever ever use it :slight_smile:

@ReBirth ROFLMAO :point:

Well my combi of setVisible, setResizable and pack always work and no problem :slight_smile:

How so? It allows your window to be sized properly for any localization, desktop settings, etc. I would argue that nobody should ever use setSize() (talking GUI’s here, not necessarily games).

pack() is the best way to make a window exactly fit the layout.

I’m talking about using it to bypass the setVisible(true) thing!

Of course pack() is excellent for GUIs :stuck_out_tongue:

thank you so much for all the replies, I have gotten it working.

I have another problem, though… Sometimes KeyEvents are slow to respond, or dont respond at all, this happens most often with multiple key presses, it sometimes acts as if I released key even though i didn’t. Don’t know how to explain it exactly. Anyway, any ideas what could be wrong?

@OP
well I have similiar keyListener as yours, but there’re differences. Mine uses boolean to store a key’s state and “else” to break the checking sooner. I think you should use boolean.