Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (593)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (646)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] JFrame and BufferStrategy  (Read 4783 times)
0 Members and 1 Guest are viewing this topic.
Offline Yemto

Junior Devvie

Medals: 2
Exp: 3 years

« Posted 2013-09-27 21:35:16 »

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?
Offline Troncoso

JGO Coder

Medals: 20

« Reply #1 - Posted 2013-09-27 21:39:59 »

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).
Online nsigma
« Reply #2 - Posted 2013-09-27 21:40:43 »

Put a Canvas in the JFrame (or Frame) and get the BufferStrategy from that.

Praxis LIVE - hybrid visual IDE for live creative coding
Digital Prisoners - interactive spaces and projections
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Yemto

Junior Devvie

Medals: 2
Exp: 3 years

« Reply #3 - Posted 2013-09-27 22:07:52 »

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.
Offline opiop65
« Reply #4 - Posted 2013-09-27 22:43:30 »

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.

Offline StumpyStrust
« Reply #5 - Posted 2013-09-27 22:55:56 »

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.

Offline Yemto

Junior Devvie

Medals: 2
Exp: 3 years

« Reply #6 - Posted 2013-09-27 23:04:49 »

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
Offline Yemto

Junior Devvie

Medals: 2
Exp: 3 years

« Reply #7 - Posted 2013-09-28 02:37:28 »

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
buffer.getDrawGraphics().drawImage(bufferImage, 0, 0, getWidth(), getHeight(), null);;
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

deepthought (31 views)
2015-06-30 15:39:44

deepthought (34 views)
2015-06-30 15:39:09

deepthought (45 views)
2015-06-30 15:36:52

Za\'Anzabar (21 views)
2015-06-29 05:44:54

TritonDreyja (36 views)
2015-06-24 17:10:40

CopyableCougar4 (35 views)
2015-06-23 00:34:45

BurntPizza (39 views)
2015-06-21 20:36:46

cookiecompiler (82 views)
2015-06-11 15:42:53

cookiecompiler (45 views)
2015-06-11 15:41:14

NegativeZero (75 views)
2015-06-11 09:49:18
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!