Active Rendering

Hi,
I recently made a game using the Repaint() method for my game animation, but then I read a chapter from Killer Game Programming about active rendering. I tried to implement this into my game code. It displays everything ok but my game no longer responds to my action listener. I have no idea what is wrong. There is no runtime or compilation errors.

Here is a snippet of my Active Rendering method.

	public void mousePressed(MouseEvent event)
	{
		int mousex = event.getX();
		int mousey = event.getY();
		
		if(mousex >= 310 && mousex <= 470 && mousey >= 300 && mousey <= 335 && gameover && !credits && !loading)
		{
			gameover = false;
			firstlevel = true;
		}
		if(mousex >= 310 && mousex <= 470 && mousey >= 336 && mousey <= 370 && gameover && !credits && !loading)
		{
			credits = true;
		}
		if(mousex >= 200 && mousex <= 280 && mousey >= 225 && mousey <= 260 && gameover && credits && !loading)
		{
			credits = false;
		}
		if(mousex >= 150 && mousex <= 340 && mousey >= 250 && mousey <= 285 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
		{
			firstlevel = false;
			firstlevelbegin = true;
		}
		if(mousex >= 200 && mousex <= 280 && mousey >= 300 && mousey <= 335 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
		{
			firstlevel = false;
			gameover = true;
		}
	}
	public void mouseReleased(MouseEvent event)
	{
		
	}
	public void mouseClicked(MouseEvent event)
	{
		
	}
	public void mouseEntered(MouseEvent event)
	{
		
	}
	public void mouseExited(MouseEvent event)
	{
		
	}
	
	public void gameupdate()
	{
		
	}
	
	public void run()
	{
		long  starttime;
		while(running)
		{
			starttime=System.currentTimeMillis();
			gameupdate();
			gamerender();
			paintscreen();
			try
			{
				starttime += pausefor;
				Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
			}
			catch(InterruptedException interruptedexception) { }
		}
	}
	
	public void gamerender()
	{
		if(dbImage == null)
		{
			dbImage = createImage(getSize().width, getSize().height);
			if(dbImage == null)
			{
				System.out.println("dbImage is null");
				return;
			}
			else
			dbg = dbImage.getGraphics();
			dbg.setColor(getBackground());
			dbg.fillRect(0, 0, getSize().width, getSize().height);
			
			if(loading)
			{
				loading(dbg);
			}
			if(gameover && !credits && !loading)
			{
				titlescreen(dbg);
			}
			if(gameover && credits && !loading)
			{
				credits(dbg);
			}
			if(!gameover && firstlevel && !loading && !credits && !firstlevelbegin)
			{
				firstlevel(dbg);
			}
			if(ispaused)
			{
				paused(dbg);
			}
		}
	}
	
	public void paintscreen()
	{
		Graphics g;
		try
		{
			g = this.getGraphics();
			if((g != null) && (dbImage != null))
			g.drawImage(dbImage, 0, 0, null);
			Toolkit.getDefaultToolkit().sync();
			g.dispose();
		}
		catch (Exception e)
		{
			System.out.println("Graphics context error: " + e);
		}
	}

Thanks.

The problem isn’t in the snippet you gave here. Please post the entire class (it should be GPanel or something similar if you followed KGPJ)

Did you add the listeners onto the new GPanel class (the one made in KGPJ) or onto the parent JFrame or whatever?
Did you implement Runnable into the panel?
Do you mean action listener i.e. listeners on JButton’s and stuff or the key and mouse listeners used in KGPJ?

After working through the 2D part of KGPJ a while I’m sure I’ll be able to spot your problem if I see the rest of your code.

Here is all of the code:

import java.applet.*;
import java.awt.*;
import java.applet.AudioClip;
import java.awt.geom.*;
import java.awt.event.*;

public class area51 extends Applet implements Runnable, MouseListener, FocusListener
{
	// Ints
	int myfps = 50;
	int pausefor;
	int onesecond = 1000;
	
	// Booleans
	private volatile boolean gameover = true;
	private volatile boolean running = true;
	private volatile boolean loading;
	private volatile boolean ispaused;
	private volatile boolean credits = false;
	private volatile boolean firstlevel = false;
	private volatile boolean firstlevelbegin = false;
	
	// Strings
	
	
	// Floats
	float size = 25f;
	
	// Fonts
	Font titlefont;
	
	// Arrays
	
	
	//Images
	private Image dbImage;
	private Graphics dbg;
	Image backgroundimage;
	Image blankbackground;
	Image firstlevelimage;

	// Sounds
	AudioClip xfiles;
	
	// Threads
	Thread runner;

	public void init()
	{
		pausefor = onesecond/myfps;
		xfiles = getAudioClip(getCodeBase(), "xfiles.mid");
		backgroundimage = getImage(getDocumentBase(), "area51.gif");
		blankbackground = getImage(getDocumentBase(), "blankbackground.gif");
		firstlevelimage = getImage(getDocumentBase(), "firstlevelimage.gif");
		MediaTracker mediatracker = new MediaTracker(this);
		mediatracker.addImage(backgroundimage, 0);
		mediatracker.addImage(blankbackground, 1);
		mediatracker.addImage(firstlevelimage, 2);
		try
		{
			loading = true;
			mediatracker.waitForID(0);
			mediatracker.waitForID(1);
			mediatracker.waitForID(2);
			loading = false;
		}
		catch(InterruptedException interruptedexception)
		{
			System.out.println("Download Error");
		}
	
		try
		{
			ClassLoader cl = this.getClass().getClassLoader();
			titlefont = Font.createFont(Font.TRUETYPE_FONT,
			cl.getResourceAsStream("Vera.ttf"));
			titlefont = titlefont.deriveFont(size);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		
		addMouseListener(this);
		addFocusListener(this);
	}
	
	public void start()
	{
		setVisible(true);
		requestFocus();
		running = true;
		runner = new Thread(this);
		runner.start();
		xfiles.loop();
	}
	
	public void stopgame()
	{
		if(running != false)
		running = false;
	}
	
	public void destroy()
	{
		xfiles.stop();
	}
	
	public void focusGained(FocusEvent focusevent)
	{
		ispaused = false;
	}

	public void focusLost(FocusEvent focusevent)
	{
		ispaused = true;
	}
	
	public void mousePressed(MouseEvent event)
	{
		int mousex = event.getX();
		int mousey = event.getY();
		
		if(mousex >= 310 && mousex <= 470 && mousey >= 300 && mousey <= 335 && gameover && !credits && !loading)
		{
			gameover = false;
			firstlevel = true;
		}
		if(mousex >= 310 && mousex <= 470 && mousey >= 336 && mousey <= 370 && gameover && !credits && !loading)
		{
			credits = true;
		}
		if(mousex >= 200 && mousex <= 280 && mousey >= 225 && mousey <= 260 && gameover && credits && !loading)
		{
			credits = false;
		}
		if(mousex >= 150 && mousex <= 340 && mousey >= 250 && mousey <= 285 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
		{
			firstlevel = false;
			firstlevelbegin = true;
		}
		if(mousex >= 200 && mousex <= 280 && mousey >= 300 && mousey <= 335 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
		{
			firstlevel = false;
			gameover = true;
		}
	}
	public void mouseReleased(MouseEvent event)
	{
		
	}
	public void mouseClicked(MouseEvent event)
	{
		
	}
	public void mouseEntered(MouseEvent event)
	{
		
	}
	public void mouseExited(MouseEvent event)
	{
		
	}
	
	public void gameupdate()
	{
		
	}
	
	public void run()
	{
		long  starttime;
		while(running)
		{
			starttime=System.currentTimeMillis();
			gameupdate();
			gamerender();
			paintscreen();
			try
			{
				starttime += pausefor;
				Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
			}
			catch(InterruptedException interruptedexception) { }
		}
	}
	
	public void gamerender()
	{
		if(dbImage == null)
		{
			dbImage = createImage(getSize().width, getSize().height);
			if(dbImage == null)
			{
				System.out.println("dbImage is null");
				return;
			}
			else
			dbg = dbImage.getGraphics();
			dbg.setColor(getBackground());
			dbg.fillRect(0, 0, getSize().width, getSize().height);
			
			if(loading)
			{
				loading(dbg);
			}
			if(gameover && !credits && !loading)
			{
				titlescreen(dbg);
			}
			if(gameover && credits && !loading)
			{
				credits(dbg);
			}
			if(!gameover && firstlevel && !loading && !credits && !firstlevelbegin)
			{
				firstlevel(dbg);
			}
			if(ispaused)
			{
				paused(dbg);
			}
		}
	}
	
	public void paintscreen()
	{
		Graphics g;
		try
		{
			g = this.getGraphics();
			if((g != null) && (dbImage != null))
			g.drawImage(dbImage, 0, 0, null);
			Toolkit.getDefaultToolkit().sync();
			g.dispose();
		}
		catch (Exception e)
		{
			System.out.println("Graphics context error: " + e);
		}
	}
	
	public void loading(Graphics g)
	{
		g.drawString("Loading...", 200, 200);
	}
	
	public void titlescreen(Graphics g)
	{
		g.drawImage(backgroundimage, 0, 0, this);
		g.setFont(titlefont);
		g.setColor(Color.green);
		g.drawString("Start Game", 320, 325);
		g.drawString("Credits", 320, 360);
		Graphics2D painter2D = (Graphics2D) g;
		Rectangle2D.Float box = new Rectangle2D.Float(310F, 300F, 160F, 35F);
		Rectangle2D.Float box1 = new Rectangle2D.Float(310F, 335F, 160F, 35F);
		painter2D.draw(box);
		painter2D.draw(box1);
	}
	
	public void firstlevel(Graphics g)
	{
		g.drawImage(blankbackground, 0, 0, this);
		g.setColor(Color.green);
		g.drawString("Your mission is to spy on Area 51.", 40, 70);
		g.drawString("To do this you will have to get", 40, 100);
		g.drawString("past the security and pretend you", 40, 130);
		g.drawString("are an engineer for the latest UFO!", 40, 160);
		g.drawString("Move with the arrow keys.", 80, 220);
		g.drawString("Start Mission! ", 160, 275);
		g.drawString("Back", 210, 325);
		Graphics2D painter2D = (Graphics2D) g;
		Rectangle2D.Float box = new Rectangle2D.Float(150F, 250F, 190F, 35F);
		Rectangle2D.Float box1 = new Rectangle2D.Float(200F, 300F, 80F, 35F);
		painter2D.draw(box);
		painter2D.draw(box1);

		if(firstlevelbegin && !firstlevel && !gameover && !loading && !credits)
		{
			g.drawImage(firstlevelimage, -250, -500, this);
		}
	}
	
	public void credits(Graphics g)
	{
		g.drawImage(blankbackground, 0, 0, this);
		g.setColor(Color.green);
		g.drawString("CREDITS", 190, 100);
		g.drawString("Ronan McCann-Jackson", 100, 150);
		g.drawString("Back", 210, 250);
		Graphics2D painter2D = (Graphics2D) g;
		Rectangle2D.Float box = new Rectangle2D.Float(200F, 225F, 80F, 35F);
		painter2D.draw(box);
	}
	
	public void paused(Graphics g)
	{
		g.setColor(Color.yellow);
		g.drawString("The game is paused!", 110, 20);
	}
}

Thanks.

does the start() method exits right after you called it or does it exit only after complete all Threading ops ? Swing and AWT can be blocked if your start() or loops do execute on the same thread.

You had a bracket problem. In thhis part


public void gamerender()
{
	if(dbImage == null)
	{
		dbImage = createImage(getSize().width, getSize().height);
		if(dbImage == null)
		{
			System.out.println("dbImage is null");
			return;
		}
		else
		dbg = dbImage.getGraphics();
		dbg.setColor(getBackground());
		dbg.fillRect(0, 0, getSize().width, getSize().height);
		
		.
		.
		.
	}
}

your code is only going to draw if dbImage is null. The correction is:


public void gamerender()
{
	if(dbImage == null)
	{
		dbImage = createImage(getSize().width, getSize().height);
		if(dbImage == null)
		{
			System.out.println("dbImage is null");
			return;
		}
		else
		dbg = dbImage.getGraphics();
	}
	dbg.setColor(getBackground());
	dbg.fillRect(0, 0, getSize().width, getSize().height);
	
	if(loading)
	{
		loading(dbg);
	}
	if(gameover && !credits && !loading)
	{
		titlescreen(dbg);
	}
	if(gameover && credits && !loading)
	{
		credits(dbg);
	}
	if(!gameover && firstlevel && !loading && !credits && !firstlevelbegin)
	{
		firstlevel(dbg);
	}
	if(ispaused)
	{
		paused(dbg);
	}
}

Thanks!!! It works now. What are the advantages of using active rendering apart from full controll over the repainting of your app?
Thanks again!

It get’s excecuted as you start the function, and not placed on a queu in a Swing Thread
Wich should give better performance.

The main difference is that it removes flickering by using an off-screen buffer (aka double buffering). When you use the normal paint() method for drawing then everytime you draw something it is drawn on the on-screen buffer. This means that everytime you draw the entire visible screen is redrawn. That is when you get flickering.

An off-screen buffer works by drawing on an image that’s only in memory. Only when you have finished drawing everything is that image drawn onto the screen. Thus the visible screen is drawn to only once and there’s no flicker.
(The KGPJ book explains this process with pictures if you want to see a visual representation of it)

The performance difference is not as a result of not being placed on a queue in a swing thread. The reason you get “faster drawing” is because you draw to the visible screen only once whereas in non-active rendering you would draw to visible screen after every item drawn.

Say for example you wanted to draw a stick man with a colored head and 5 lines. You would do something like
Set color
Fill head
Draw head
Draw line
Draw line
Draw line
Draw line
Draw line
= 8 instructions

Now in active rendering you would have the exact same code except 1 extra “draw to visible screen” instruction. This is because your drawing is done onto an Image that resides in memory. Since that Image is shown nowhere it need not be redrawn everytime you add something.

In non-active rendering there would be “draw to visible screen” after every instruction. Now when you get to drawing much more items its going to be slower. This happens because the Graphics object used in the paint method refers to the actual visible screen. So any change to the Graphics means a direct change to the screen and that can only be done by updating the whole Graphics to the screen and not just the last item drawn.