The previous step solved our problem with the images, but it is not very elegant because it is a pain having
to check if an image is null each time we need to access it.
To avoid this, we'll encapsulate the access to an image inside a method which we'll call getSprite().
This method will receive as a parameter the name of the sprite we need to use, and will load it if it is not already
in memory. Since there could be hundreds of images, the method should know which of them are loaded and which are not.
For this, we'll use a HashMap that will store pairs of the type (name-of-sprite,loaded-image)
Invaders.java
1 package version06;
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 import java.util.HashMap;
20
21 import javax.imageio.ImageIO;
22 import javax.swing.JFrame;
23 import javax.swing.JPanel;
24
25 public class Invaders extends Canvas {
26 public static final int WIDTH = 800;
27 public static final int HEIGHT = 600;
28