Best frame setup for drawing

Hi… I’m trying to write a basic tile engine game, and I’m wondering what the best way is to set up a frame for drawing. What I was doing was: create a JFrame and then add another JFrame to that one, and set the first’s content pane to be the 2nd one. JPanels use double buffering as default, so I did not specify any specific buffer strategy. Then I just overwrote the paint function of the 2nd one to do the drawing, but while it was pretty smooth, it didn’t seem smooth enough. I have played some games that just felt ‘super-smooth’. I don’t know if this is because it was updating passively at times? I had set both frames to ignore repaint.

So what I tried now is to create only one main JFrame, creating a bufferstrategy(2), and then have a draw function like:


public void draw()
{
        Graphics2D gr2D = (Graphics2D) bufferStrategy.getDrawGraphics();

        // do drawing stuff

        bufferStrategy.show();            
        gr2D.dispose(); 
}

It nows feels a whole lot smoother, no frame skipping at all, and is also about 30 fps faster … but sometimes when I close the application, it leaves a black background on the screen where the window was. I did some googling but only found one site with a brief comment “It’s considered bad practice to draw directly to the main JFrame’s content…”, or something to that effect.

So my question is, why is this black background left behind occassionally? And is it considered bad practice to do the drawing straight to the content pane of the main JFrame? If so, what’s the best practice?

Ok maybe a few more… why does using a buffer strategy give a slightly higher frame rate, when a JFrame uses a default buffer strategy already?

And finally… I’ve tested my game on Linux (I’m using Java 1.5) and the frame rate compared to running in windows is pretty awful. In windows, currently its about 200 fps, tiling an 800x600 window with a tile size of 64… under linux, the fps is about 30 or 40. That’s quite horrid. Why is performance so bad? This is with opengl enabled, but disabling it produces similar results (aside from huge slowdowns when anything translucent appears on screen).

Oh! Final final question… is there a way I can enable vsync in the app? Because the results are just wow when I set wait for vsync to true in my ati drivers, but the program being able to specify it wants to wait for vsync would be nicer. Using default java/java2d though… no opengl bindings.

Thanks for any help. I browse this forum quite regularly and it definately seems to be the best source of info for Java on the web.

I believe that vsync will take place automatically when using fullscreen with bufferstrategy as long the person running the app doesn’t explicitly disable it or their computer doesn’t support it for some reason.

did you say you were adding a JFrame to a JFrame!? can you even do that? :wink: If you don’t want to draw directly onto the JFrame, but you want to use BufferStrategy, I would suggest that you use a java.awt.Canvas. I’ve had no problems with drawing onto the BufferStrategy given to me by the JFrame though.