// file: BaseApplet.java // author: Robert Keller // purpose: Base class for simple applets, with flicker-free graphics /** * BaseApplet.java * @version 1 * @author Robert M. Keller * $Id* */ import java.applet.*; // applet classes import java.awt.*; // Abstract Window Toolkit classes import java.awt.event.*; /** * BaseApplet with methods intended to be over-ridden to create games, etc. */ public class BaseApplet extends Applet implements Runnable, MouseListener, MouseMotionListener, KeyListener { protected Color backgroundColor = Color.white, foregroundColor = Color.black; protected int currentDelay = 10; // default cycle delay in milliseconds protected Thread myThread; // the Thread for this Applet protected Image image; // Image containing the offScreen buffer protected Graphics offScreen; // offScreen buffer for flicker-free display /** * Initialize the applet. Can be over-ridden in a derived class, but * if so, the over-riding method should call super.init();. */ public void init() { setBackground(backgroundColor); createOffScreenBuffer(); addMouseListener(this); addMouseMotionListener(this); addKeyListener(this); } /** * Start the applet. * Called when the applet is first started and when the web-page is re-entered. */ public void start() { if( myThread == null ) { myThread = new Thread(this); myThread.start(); } } /** * Run the applet by running its thread. * This over-rides the run() method in class Applet. */ public void run() { requestFocusInWindow(); while( continueRunning() ) { step(); // Update the state of the game, including any drawing. sleep(); // Wait awhile before the next step. } } /** * Sleep for the amount specified in current delay. */ public void sleep() { sleep(currentDelay); } /** * Sleep for a specified number of milliseconds. */ public void sleep(int amount) { try { myThread.sleep(amount); } catch( Exception e ) { } } /** * Set the current delay value in milliseconds. */ public void setDelay(int delay) { currentDelay = delay; } /** * Create the off-screen buffer. */ void createOffScreenBuffer() { image = createImage(getWidth(), getHeight()); offScreen = image.getGraphics(); } /** * Clear the off-screen buffer. */ public void clearOffScreenBuffer() { offScreen.setColor(backgroundColor); offScreen.fillRect(0, 0, getWidth(), getHeight()); } /** * update is implicitly called by repaint() * It calls paint(Graphics). * This method intentionally over-rides update() in class Applet so that * the background is not blanked, which will tend to cause flicker. * * It is probably best not to change this unless you really know what * you are doing. */ public void update(Graphics g) { paint(g); } /** * paint(Graphics) is is called by update(Graphics). * This is where the off-screen buffer gets painted to the screen. * image contains the Graphics for what is to be painted. * * It is probably best not to change this unless you really know what * you are doing. */ public void paint(Graphics g) { g.drawImage(image, 0, 0, null); } /** * Over-ride this in the derived class. It gives the condition under * which the applet will continue calling step in the main loop. */ public boolean continueRunning() { return true; } /** * Over-ride this in the derived class. It indicates what the will be * done as a single step in the main loop. */ public void step() { } /** * Paint the off-screen buffer onto the screen */ void paintBuffer() { repaint(); } /** * These are required for implementing MouseListener. * They can be over-ridden in a derived class at your option. */ public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } /** * These are required for implementing MouseMotionListener. * They can be over-ridden in a derived class at your option. */ public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } /** * These are required for implementing KeyListener. * They can be over-ridden in a derived class at your option. */ public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { switch( e.getKeyCode() ) { case java.awt.event.KeyEvent.VK_DOWN: break; case java.awt.event.KeyEvent.VK_UP: break; case java.awt.event.KeyEvent.VK_LEFT: break; case java.awt.event.KeyEvent.VK_RIGHT: break; default: } } public void keyReleased(KeyEvent e) { } } // BaseApplet