Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Previous Page - First images Current Page - 05 - Optimizing the loading of images   Next Page - Optimizing even more our image loading process
  Return to the index of free Java tutorials  

Optimizing the loading of images

(c) Alexander Hristov

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:


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


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

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

  Previous Page - First images Current Page - 05 - Optimizing the loading of images   Next Page - Optimizing even more our image loading process
  Return to the index of free Java tutorials  

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