Skip to main content
gamedev.net
🔒 Locked

[java] Attempting Java 2D Graphics, extreme flickering error

Started by Gage Jul 15, 2004 at 1:01 AM 4 replies 2.8k views
Original Post
Gage
Gage
Howdy Guys, this is my first post here although probably not the last. I have begun writing a 2D game engine in pure java. It is being written from scratch as I am doing it for a class at school, and so far I've gotten some good subcomponents completed, and am now moving to graphics. Now I've tried to implement buffered drawing to reduce flickering as well as have per-pixel capabilities, however I have very extreme flickering that has to be done through the game update loop (making the thread sleep for a second makes the flickers spread to a second, having it run as fast as possible makes the screen almost black by comparison). Here is the code for my GraphicsDriver class: ============================================== package altercation.graphics; import java.awt.*; import java.awt.image.MemoryImageSource; import altercation.core.*; import altercation.engine.*; // Basic pure java double buffered renderer public class GraphicsDriver implements Driver { private Engine engine; private GraphicsEnvironment gEnv; private GraphicsDevice gDev; private int pixels[]; private MemoryImageSource memImage; private Image image; public GraphicsDriver( Engine engine ) { this.engine = engine; gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment(); gDev = gEnv.getDefaultScreenDevice(); pixels = new int[640*480]; memImage = new MemoryImageSource( 640, 480, pixels, 0, 640 ); // TODO: Perhaps make source resizeable memImage.setAnimated( true ); memImage.setFullBufferUpdates( true ); image = Toolkit.getDefaultToolkit().createImage( memImage ); for( int i = 0;i < 640*480;i++ ) pixels = 0xff0000ff; } public void init() { } public void runFrame( Graphics g ) { memImage.newPixels(); g.setColor( Color.black ); g.fillRect( 0, 0, 640, 480 ); g.drawImage( image,0,0,null); } } ============================================== runFrame is called from the Engine classes runFrame method, which in turn is called from the application cores update loop. for testing it merely puts a blue hue onto the entire image source and draws to the screen. Hopefully you guys could help me out with this. I tried looking in the GameFrame code for clues but its so advanced it was nearly impossible to look around. I've looked at several different tutorials but im probably missing like 1 or 2 lines of code i bet. Thanks, Josh Coyne
Gage
Gage
both the frame's paint and update methods are empty. I'll look into bufferedImage, and granted i know a blank screend oesnt need to be refreshed but im testing it because it will have to be updated because its gonna be animated.

Thank you for your time, ill look into bufferedImage :)
CaptainJester
CaptainJester
You might also want to try calling setIgnoreRepaint() on your uppermost component(JFrame).
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
capn_midnight
capn_midnight
this is generally what I do:
class MyFrame extends JFrame{private BufferStrategy bs;public MyFrame(){	try	{		//initializing a front and back buffer, both with hardware		// acceleration when possible.		this.createBufferStrategy(2, new BufferCapabilities(				new ImageCapabilities(true), new ImageCapabilities(true), null)); 		//and save the buffer strategy for later use		bs = this.getBufferStrategy();	}	catch (Exception e)	{		System.err.println("Double buffering not setup, dieing now...");		e.printStackTrace();		System.exit(-1);	}//setup the painting timer	new Timer(30, new ActionListener()	{		public void actionPerformed(ActionEvent ae)		{			paint();		}	}).start();	this.setIgnoreRepaint(true);}public void paint(){Graphics2D g = (Graphics2D)bs.getDrawGraphics();//whatever drawing I need to dobs.show();}}


I do it this way because it can take advantage of hardware acceleration with double buffering
Gage
Gage
thank you all for your help. My biggest offending line was clearing the screen using g.fillRect, and im using that bufferedimage class to display the image now :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.