I'm not sure exactly how to phrase a search for this, but I don't understand how, java2D at least, handles this aspect of rendering or if it does at all. If you tell it to render an object wholly outside, or even partially outside, does it save itself the cpu and not render it at all? I mean the way I understand it, every one of those draw methods affects some matrix of pixels (I think of it as a 2D array), and then the show method is called and it is shown on the screen, is that correct?
Yup, I should not worry too much about stuff you draw off-screen. Usually for a game you draw the graphics on a 'back buffer' (which is indeed a bit like a matrix of pixels or a 2D array), and then draw the back buffer to the screen you see on the monitor. The rendering API, be it software or hardware makes sure that it doesn't waste time rendering things you cannot see, so I would not worry about that if I were you.
Here's how I do rendering in the Java4K games, which shows the use of a back-buffer in an applet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import java.applet.Applet; import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage;
public class Java2DTest extends Applet implements Runnable {
public void start() { new Thread(this).start(); }
public void run() { BufferedImage screen = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Graphics2D appletGraphics = (Graphics2D) getGraphics();
do {
g.setPaint(Color.black); g.fillRect(0, 0, 800, 600);
g.setPaint(Color.red); g.fillRect(100, 100, 200, 200);
g.setPaint(Color.blue); g.fillRect(3000, 3000, 200, 200);
appletGraphics.drawImage(screen, 0, 0, null);
try { Thread.sleep(10); } catch (Exception e) {
}
} while (isActive()); } |