Solved

Any clean Java Swing double buffering advice?

Posted on 2014-12-21
10
249 Views
Last Modified: 2015-01-08
Hi

My RTS game is ready for graphics. I have found some good sprites to use, for dev at least. I am having issues with Jframe double buffering and flickering. I draw my Mutalisk, - an Image object - onto the getGraphics() of the JFrame. It flickers :(
Should I be creating an Image the size of the window, drawing on it and setting the image as the window's graphics (an off-screen buffer?) how?

Thanks
0
Comment
Question by:beavoid
  • 7
  • 3
10 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 40512427
I think you want to look into the BufferStrategy class (http://docs.oracle.com/javase/tutorial/extra/fullscreen/bufferstrategy.html)

which is part of the general double buffering solution in Java:
http://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html

You shouldn't have to do too much work yourself to support this.

Doug
0
 

Author Comment

by:beavoid
ID: 40517717
Thanks
How do I un-neglect this question?
0
 

Author Comment

by:beavoid
ID: 40518113
Thanks

Which Java strategies automatically use graphics hardware on the machine?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 26

Expert Comment

by:dpearson
ID: 40518139
I think they'll all attempt to use hardware acceleration when possible.  I think you can test if they're actually available or not in hardware through the BufferCapabilities class - e.g.

http://docs.oracle.com/javase/7/docs/api/java/awt/BufferCapabilities.html#isPageFlipping()
0
 

Author Comment

by:beavoid
ID: 40519793
just a comment to keep this question alive ;) they annihilate inactive questions :) Will very soon get to this issue.

Thanks
0
 

Author Comment

by:beavoid
ID: 40526350
Thanks
Someone suggested I have everything on a giant .PNG file, load that in and extract the images I need from that. Obviously that retains transparency? How do I make those into frames - animated Image Objects?
Is PNG quality type exactly what games are done with?
0
 
LVL 26

Assisted Solution

by:dpearson
dpearson earned 500 total points
ID: 40526366
PNGs are fine since they're a lossless format (JPEG for example uses a lossy compression - so the image isn't exactly what you created, while PNGs are).

Here's a little code snippet that loads a PNG and saves it out at a different size - keeping the transparency.
You should be able to adapt it pretty easily into something that extracts just a portion of the original image and rather than writing it out as a new png, just keeps it around for drawing in the game.

	void scaleImage(File srcFile, File destFile, int targetWidth, int targetHeight) throws Exception
	{
		 BufferedImage in = ImageIO.read(srcFile);
		 GraphicsConfiguration gc = in.createGraphics().getDeviceConfiguration();
		 BufferedImage out = gc.createCompatibleImage(targetWidth, targetHeight, Transparency.BITMASK);
		 Graphics2D g2d = out.createGraphics();	
                 g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) ;
		 g2d.setComposite(AlphaComposite.Src);		// This is the key step to setting the transparent fields correctly.
		 g2d.drawImage(in, 0, 0, targetWidth, targetHeight, null);   You can pass something other than 0,0 here...
		 g2d.dispose();
		 ImageIO.write(out, "png", destFile);		
          }

Open in new window


Doug
0
 

Author Comment

by:beavoid
ID: 40530617
Just A comment just to keep this question alive til I get home from vacation soon
0
 

Author Closing Comment

by:beavoid
ID: 40539187
Thanks

The links are clean and straightforward.

There is a guy on Youtube who makes clear videos :

Java Game Programming

episodes, that had one that was very helpful - a BufferStrategy one.
My mutalisks are complacently flapping aground, not knowing that they were formerly neon lights due to flickering.
0
 

Author Comment

by:beavoid
ID: 40539191
A link to the Buffer Strategy Youtube is here
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
print map entry 34 69
more than one jdk and one jre 1 41
Way to decrease size of apk file 9 67
How do I remove an object from a 3 26
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question