Solved

Any clean Java Swing double buffering advice?

Posted on 2014-12-21
10
244 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven java path setting 5 50
Eclipse Neon and jdk 1.8.0 11 67
groovy example issue 10 66
What is the latest versions eclipse neon 2 120
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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

18 Experts available now in Live!

Get 1:1 Help Now