Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Previous Page - Closing the window Current Page - 03 - First attempts at painting   Next Page - First images
  Return to the index of free Java tutorials  

First attempts at painting

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

The next step which we must do as soon as possible is to acquire the ability to draw things on our window.
If you have followed the standard books on Java, painting means overriding the paint(...) method of "someone" - a window, an applet, whatever... However, our Invaders class is not a window, and even if it was, since we are displaying a different window, overriding its paint method would be of no use.

A possible solution is making our class extend from the Canvas class - a traditional and favourite starting point for developing visual components. Being a Canvas, our class will inherit the paint() method which we can override.
But how to we make the association between our class and the window we have just built? Easy : since our class is a Canvas , it is also a Component , and as such can be added to the contents pane of the frame:


1     package version03;
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.Color;
14    import java.awt.Dimension;
15    import java.awt.Graphics;
16    import java.awt.event.WindowAdapter;
17    import java.awt.event.WindowEvent;
18    
19    import javax.swing.JFrame;
20    import javax.swing.JPanel;
21    
22    public class Invaders extends Canvas {
23      public static final int WIDTH = 800;
24      public static final int HEIGHT = 600;
25      
26      public Invaders() {
27        JFrame ventana = new JFrame("Invaders");
28 JPanel panel = (JPanel)ventana.getContentPane(); 29 setBounds(0,0,WIDTH,HEIGHT); 30 panel.setPreferredSize(new Dimension(WIDTH,HEIGHT)); 31 panel.setLayout(null); 32 panel.add(this);
33 ventana.setBounds(0,0,WIDTH,HEIGHT); 34 ventana.setVisible(true); 35 ventana.addWindowListener( new WindowAdapter() { 36 public void windowClosing(WindowEvent e) { 37 System.exit(0); 38 } 39 }); 40 } 41
42 public void paint(Graphics g) { 43 g.setColor(Color.red); 44 g.fillOval( WIDTH/2-10, HEIGHT/2-10,20,20); 45 } 46
47 public static void main(String[] args) { 48 Invaders inv = new Invaders(); 49 } 50 } 51


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

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