(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:
Invaders.java
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");