Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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 3920 times)
0 Members and 1 Guest are viewing this topic.
Offline Yemto

Junior Devvie


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).
Offline 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 - open-source intermedia toolkit and live interactive visual editor
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


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

JGO Kernel


Medals: 160
Projects: 7
Exp: 3 years


JumpButton Studios


« 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


Exp: 3 years



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

150~ fps
1  
2  
3  
GraphicsConfiguration gConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage img = gConfig.createCompatibleImage(res.width, res.height, Transparency.TRANSLUCENT);
Graphics2D g2 = (Graphics2D)img.getGraphics();


550~ fps
1  
2  
3  
GraphicsConfiguration gConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
VolatileImage img = gConfig.createCompatibleVolatileImage(res.width, res.height);
Graphics2D g2 = (Graphics2D)img.getGraphics();


1300~ fps
1  
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.
1  
//Long code
Offline Yemto

Junior Devvie


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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
//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
1  
2  
3  
g2.dispose();
buffer.getDrawGraphics().drawImage(bufferImage, 0, 0, getWidth(), getHeight(), null);
buffer.show();
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!