I don’t like JFrame, and BufferStrategy. Sure it render really fast. But I’m so annoyed that 0,0 isn’t the top left of the screen since the decorations is taking that spot, and that BufferStrategy is always the same size as the window. I want to make a game with resolution options, and I feel JFrame and BufferStrategy prevents me from doing that. So is it any alternative, or a way to make them behave like I want?
I think you’re asking for an alternative to Java2D? Which would be LWJGL, Slick2D or LibGDX (the latter 2 are frameworks on top of LWJGL).
Put a Canvas in the JFrame (or Frame) and get the BufferStrategy from that.
Thanks, that fixed one of my problems.
The problem I have left it’s the re-size one. The canvas still render more when someone resize the JFrame. But I want it to stretch. The reason why I want stretch, rather than render more. is because it would be much easier to do HUD’s since I know the game always going to be a set resolution, instead of a width and height that could be anything. I could just do it with a BufferedImage. But I want to see if it’s any better alternative first.
Edit: I couldn’t fix it with a BufferedImage, I tried and the frame rate went down from 1300 fps, to 150 fps.
Can we see some code? You shouldn’t see that drastic of an fps drop just for using a bufferedimage. The problem with stretching; you’ll need to make sure you resize your graphics based on the new height and width.
Create a VolatileImage and draw with its graphics object. Then simply render that to the canvas/jframe.
You can render to a targeted resolution and then just stretch at a given aspect ratio. This is what Retro does. Hehe Retro is basically a gold mine for java2D hacks/tricks.
150~ fps
GraphicsConfiguration gConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage img = gConfig.createCompatibleImage(res.width, res.height, Transparency.TRANSLUCENT);
Graphics2D g2 = (Graphics2D)img.getGraphics();
550~ fps
GraphicsConfiguration gConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
VolatileImage img = gConfig.createCompatibleVolatileImage(res.width, res.height);
Graphics2D g2 = (Graphics2D)img.getGraphics();
1300~ fps
Graphics2D g2 = (Graphics2D)buffer.getDrawGraphics();
Here is the whole render method, keep in mind that it’s stayed the same for all the above examples.
//Long code
I manage to think out of a fix, it’s was the time it took to run .createCompatibleVolatileImage() that slowed down the rendering. So I just create it once, and it fixed it. it runs around 1200 fps instead of 1300, so it’s still totally acceptable.
//Create BufferStrategy
BufferStrategy buffer = this.getBufferStrategy();
if(buffer == null){ this.createBufferStrategy(2); return; }
//Create bufferImage
if(bufferImage == null){
GraphicsConfiguration gConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
bufferImage = gConfig.createCompatibleVolatileImage(res.width, res.height); return;
}
code to make the bufferImage stretch
g2.dispose();
buffer.getDrawGraphics().drawImage(bufferImage, 0, 0, getWidth(), getHeight(), null);
buffer.show();