Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Previous Page - Optimizing the loading of images Current Page - 06 - Optimizing even more our image loading process   Next Page - The Game Main Loop
  Return to the index of free Java tutorials  

Optimizing even more our image loading process

(c) Alexander Hristov

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)


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      
29 public HashMap sprites;
30 31 public Invaders() {
32 sprites = new HashMap();
33 34 JFrame ventana = new JFrame("Invaders"); 35 JPanel panel = (JPanel)ventana.getContentPane(); 36 setBounds(0,0,WIDTH,HEIGHT); 37 panel.setPreferredSize(new Dimension(WIDTH,HEIGHT)); 38 panel.setLayout(null); 39 panel.add(this); 40 ventana.setBounds(0,0,WIDTH,HEIGHT); 41 ventana.setVisible(true); 42 ventana.addWindowListener( new WindowAdapter() { 43 public void windowClosing(WindowEvent e) { 44 System.exit(0); 45 } 46 }); 47 } 48 49 public BufferedImage loadImage(String nombre) { 50 URL url=null; 51 try { 52 url = getClass().getClassLoader().getResource(nombre); 53 return ImageIO.read(url); 54 } catch (Exception e) { 55 System.out.println("No se pudo cargar la imagen " + nombre +" de "+url); 56 System.out.println("El error fue : "+e.getClass().getName()+" "+e.getMessage()); 57 System.exit(0); 58 return null; 59 } 60 } 61
62 public BufferedImage getSprite(String nombre) { 63 BufferedImage img = (BufferedImage)sprites.get(nombre); 64 if (img == null) { 65 img = loadImage("res/"+nombre); 66 sprites.put(nombre,img); 67 } 68 return img; 69 }
70 71 72 public void paint(Graphics g) {
73 g.drawImage(getSprite("bicho.gif"), 40, 40,this);
74 } 75 76 public static void main(String[] args) { 77 Invaders inv = new Invaders(); 78 } 79 } 80


Do you want to be notified when new tutorials or lessons are published? Press here


Full list of Java source files for this step

Invaders.java      

Full list of resources

explosion.wav disparo2.gif bombUL.gif oceano.gif
bombDR.gif disparo0.gif bicho0.gif bombD.gif
missile.wav test.gif disparo1.gif bombU.gif
bombDL.gif bombL.gif bicho.gif Thumbs.db
bombR.gif bicho2.gif bicho1.gif disparo.gif
musica.wav bombUR.gif photon.wav misil.gif
nave.gif      

  Previous Page - Optimizing the loading of images Current Page - 06 - Optimizing even more our image loading process   Next Page - The Game Main Loop
  Return to the index of free Java tutorials  

(c) 2004 Planetalia S.L. All rights reserved. Unauthorized reproduction and/or mirroring is not permitted