Solved

Any clean Java Swing double buffering advice?

Posted on 2014-12-21
10
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 28

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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 28

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 28

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

630 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