Unless we have an extremely fast computer and a monitor with an extremely high refresh frequency, probably by
now we can clearly see the problem with our approach regarding the treatment of the screen: The dreaded flicker.
Flickering takes place because for some instants the eye is seeing the "just cleaned" screen, upon which we have still
not painted any actors.
Solving this problem involves a standard approach called double buffering. This technique
consists in having an image in memory with the same characteristics (height, width, color depth, etc..) as the screen,
and performing all the drawing in this in-memory, off-screen image. Once everything is ready, we just copy this in-memory image to
the screen. In this way, the user never really sees the intermediate steps that take place when we are redrawing the
world. The off-screen image by the way is usually called image buffer
Invaders.java
1 package version10;
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 = 640;
27 public static final int HEIGHT = 480;
28 public static final int SPEED = 10;
29
30 public HashMap sprites;
31 public int posX,posY,vX;