Solved

Any clean Java Swing double buffering advice?

Posted on 2014-12-21
10
248 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now