Sign in to follow this  
BlackLiquid

[java] drawing 101

Recommended Posts

I've got the backBuffer blues. I can't seem to get things to draw smoothly. Been testing a bit with a volatileimage and bufferedimage as offscreen buffers drawing a little blue rectangle on a black background but with the same result. When scrolling the blue rectangle becomes blurry and seems to "flicker" or tear when scrolling horizontally. I'm testing with a very basic applet (sorry about the code dump), so my question is ... am I doing something fundamentally wrong?
public class test extends Applet implements KeyListener
{
	private BufferedImage vImgOffscreen;
	private int width = 640, height = 480;
	private int x = 50,y = 50;
	private Image player;
	protected boolean m_bKeyboard[] = new boolean[KeyEvent.KEY_LAST+1];

	public void init()
	{
		addKeyListener(this);
		vImgOffscreen = getGraphicsConfiguration().createCompatibleImage(width, height);
		player = getImage(getDocumentBase(), "images/player.png");
		setSize(width,height);
	}

	public void keyPressed(KeyEvent e)
	{
		m_bKeyboard[e.getKeyCode()] = true;

		if (m_bKeyboard[KeyEvent.VK_RIGHT])
			x+=5;
		if (m_bKeyboard[KeyEvent.VK_LEFT])
			x-=5;
		if (m_bKeyboard[KeyEvent.VK_UP])
			y-=5;
		if (m_bKeyboard[KeyEvent.VK_DOWN])
			y+=5;

		repaint();
	}
	public void keyReleased(KeyEvent e) {   m_bKeyboard[e.getKeyCode()] = false;   }
	public void keyTyped(KeyEvent e){}

	public void update(Graphics g) {   paint(g);   }

	public void paint(Graphics g)
	{
	    	renderOffscreen();
	    	// back buffer to the screen
	    	g.drawImage(vImgOffscreen, 0, 0, this);
	}

	public void renderOffscreen()
	{
		Graphics2D g = vImgOffscreen.createGraphics();
		g.setColor(Color.black);
	    g.fillRect(0, 0, width, height);
		g.setColor(Color.blue);
	    g.fillRect(x, y, 16, 32);
		g.dispose();
	}
}

Share this post


Link to post
Share on other sites
Seems fine to me, test it in my pc and no problemo
Anyway what's the keyboard boolean for, if you do it like that
And where's the animation thread? Is that to make event based game?

Share this post


Link to post
Share on other sites
It runs smoothly? No blurring or anything? Are you keeping a close eye on the offending blue square? :) Hmphf .. well, that's good news I guess. Maybe it's just my eyes playing tricks on me. By the by running the applet on an AMD XP 1800, 1gb ram and a GeForce FX 5700 (latest nVidia drivers installed).

Parts of the code is taken from a game I'm working on, and that's where I use the boolean keyboard array. I don't use any animation thread though .. just one thread to control the FPS so each frame everything gets updated and drawn. I'm using an image there but I'm seeing the same thing there which is why I made this little applet to test different off screen buffers.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Check your java version; the one that is actually called from the browser. If you have multiple jre's installed sometimes its not the one you think it is.....

Share this post


Link to post
Share on other sites
Get the repaint out of the key event, and put that in a main loop. Use a little sleep after the repaint. I've had similar problems, I think the key event is triggered when drawing the previous frame is not finished yet.

Share this post


Link to post
Share on other sites
Your problem lies in the fact that while you have overridden the method paint(Graphics g), you are still using the event dispatcher by using repaint() and update(). Both these methods are not run when called, but simply generate an event which is placed in a queue and is run at some future point in time (sometimes, not at all).

This will have the effect of occasional skipped frames and jerkiness on slower machines which is what you are experiencing.

Solution is to override paint() with a blank method, place what you currently have there in a seperate method and call that instead.

- Oscar [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this