If you already have a lot of experience programming Java, probably you've noticed that the place where we
load the image - in the paint method - is nothing short of pathetic. The effect is loading
the image each time the window is redrawn - not very elegant.
You could thing that the ideal way is to load the image in the constructor, but let's think for a moment what
would happen if our game is an applet, for example, and we have a hundred images of different monsters, powerups,
terrains, levels and so on. When the game starts, the user will have to wait an eternity while all those images
are being downloaded, when some of them really might never be used because the player might never get to the level
in which they first appear.
For these reasons, a technique called deferred loading, is usually applied. This technique
consists in loading the image a single time, but only when it is really needed:
Invaders.java
1 package version05;
2 /**
3 * Curso B?sico de desarrollo de Juegos en Java - Invaders
4 *
5 * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n
6 *
7 * http://www.planetalia.com
8 *
9 */
10
11
12 import java.awt.Canvas;
13 import java.awt.Dimension;
14 import java.awt.Graphics;
15 import java.awt.event.WindowAdapter;
16 import java.awt.event.WindowEvent;
17 import java.awt.image.BufferedImage;
18 import java.net.URL;
19
20 import javax.imageio.ImageIO;
21 import javax.swing.JFrame;
22 import javax.swing.JPanel;
23
24 public class Invaders extends Canvas {
25 public static final int WIDTH = 800;
26 public static final int HEIGHT = 600;
27