Text not rendering with bufferstrategy

Hi,

I’m currently coding a basic test for a multiplayer game and I was having trouble with variable rendering speeds. Sometimes it’d run very smoothly on my desktop, sometimes it would be very slow with the same thing happening on my laptop. Another desktop seemed pretty consistent, low framerate but not really slow. While I was looking for a solution I happened upon the coke and code space invaders tutorial (again) and using that as a base, rewrote my rendering code.

I basically went from this:
JFrame containing JPanel > custom repaint > calls the game rendering function after setting scale etc
to this:
JFrame > Custom rendering class > calls the game rendering function after setting scale etc.

However, I’ve developed a slight problem that I can’t quite work out.
In the game I have a camera which I use to translate the entire Graphics2D object like so:

g.translate( -camera.x, -camera.y );
// Level and objects etc get rendered here
g.translate( camera.x, camera.y );
//UI/Text here

The 2nd version is MUCH smoother but any on screen text that I draw after I reposition the camera doesn’t appear. I did a test where I didn’t move the camera back before the text drawing and it appears, but it stays stuck in place to the top left of the level instead of the top left of the screen.

I did a second test where I quickly reimplemented the JPanel code (uncommenting it all) and suddenly the text draws where it’s supposed to. The only thing I’ve actually changed is what calls the main rendering method.

Here’s the relevant code:
http://pastebin.java-gaming.org/c39a74f5d99

The camera object is just a Rectangle that’s updated each update loop to match the player’s position.

At the end of my update loop which runs in its own thread I call either pnl.repaint(); or renderer.render();

e.g.


public void update() {
 // player update
 // camera update
 // update everything else
 //pnl.repaint();
 renderer.render();
}

As far as I can see I haven’t done anything massively different between the rendering class and the custom paintComponent method. Any help would be greatly appreciated as I just find this odd given that the only code that doesn’t seem to be working happens in the method that hasn’t changed. Interestingly the g.drawString() calls in the renderer.render() and GamePanel paintComponent() methods both work so it seems to be related to the 2nd translation of the camera.

If you need more code let me know and I’ll add more but I think this is all the code that’s actually relevant to the problem.

Thanks in advance.

Hi all,

D’oh! Feel kinda stupid now. It ended up being super obvious.

This thread explains it http://www.java-gaming.org/index.php/topic,15077.

Basically the text was drawing under the JFrame title bar but it was the only thing that was supposed to be stuck to the screen rather than moving with the camera so everything else seemed to be working properly.

In my case using a JPanel with the BufferStrategy was causing hideous flickering so my code is slightly different to the solution in the other thread as I’m drawing straight to the JFrame. I’ll post it here in case it helps anyone else.

In the code, game.wnd is the JFrame to which we’ll be drawing, game.strat is the strategy we’ve previously set from the JFrame and game.width and game.height are the base resolution you’re targetting.
Knowing this you should be able to apply this to your own code structure.


public void render() {
        Graphics2D g = (Graphics2D) game.strat.getDrawGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
        // Moves the graphics object inside the content pane
        Point frameLoc = game.wnd.getLocationOnScreen();
        Point viewLoc = game.wnd.getContentPane().getLocationOnScreen();
        g.translate( viewLoc.x - frameLoc.x, viewLoc.y - frameLoc.y );
        // Sets the rendering scale between base resolution and window size
        g.scale( (double) game.wnd.getWidth() / (double) game.width, (double) game.wnd.getHeight() / (double) game.height );
}

Hope this helps someone else.