Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Any clean Java Swing double buffering advice?

Posted on 2014-12-21
10
Medium Priority
?
276 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

715 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