Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Previous Page - The First Window Current Page - 02 - Closing the window   Next Page - First attempts at painting
  Return to the index of free Java tutorials  

Closing the window

(c) Alexander Hristov
English Version proofreading and corrections : Jeff Lunt

The next step consists of adding the necessary code in order to make the program terminate when the window is closed. As usual, we use the WindowClosing event, with a call to System.exit(0) inside


1     package version02;
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.event.WindowAdapter;
13    import java.awt.event.WindowEvent;
14    import javax.swing.JFrame;
15    
16    public class Invaders  {
17      public static final int WIDTH = 800;
18      public static final int HEIGHT = 600;
19      
20      public Invaders() {
21        JFrame ventana = new JFrame("Invaders");
22        ventana.setBounds(0,0,WIDTH,HEIGHT);
23        ventana.setVisible(true);
24 ventana.addWindowListener( new WindowAdapter() { 25 public void windowClosing(WindowEvent e) { 26 System.exit(0); 27 } 28 });
29 30 } 31 32 public static void main(String[] args) { 33 Invaders inv = new Invaders(); 34 } 35 } 36


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 - The First Window Current Page - 02 - Closing the window   Next Page - First attempts at painting
  Return to the index of free Java tutorials  

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