import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.image.MemoryImageSource; import javax.swing.JApplet; /** *
* This applet shows how to use Java standard classes to write fast animations.
* Instead of using the methods defined in java.awt.Graphics
to
* render the pictures, an integer array will be filled.
*
* This source code was originally published at http://jakobvogel.net/ * and may be freely passed on and used for non-commercial purposes as long as * this comment remains as it is. *
** Copyright (c) 2005. Please visit my homepage at http://jakobvogel.net/ for * further details! *
* * @author Jakob Vogel <jakob[dot]vogel[at]mytum[dot]de> */ public class FastGraphics extends JApplet implements Runnable { private static final long serialVersionUID = -1060277656462873556L; private Thread thread; private boolean threadRun = true; private Image doubleBuffer; private Image tripleBuffer; private Image backgroundBuffer; /** ** Converted image. Its contents are read from the integer array defined * below. *
*/ private Image fastPixelBuffer; private Graphics doubleGraphics; private Graphics tripleGraphics; private boolean screenRepaint = false; private int width; private int height; /** ** Integer array. *
*/ private int [ ] screenBuffer; /** ** Conversion object. *
*/ private MemoryImageSource screenConverter; /** * @see java.applet.Applet#init() */ public void init ( ) { thread = new Thread ( this ); thread.setPriority ( Thread.MIN_PRIORITY ); setFont ( new Font ( "Verdana", Font.PLAIN, 10 ) ); setBackground ( new Color ( 255, 255, 255 ) ); width = this.getSize ( ).width; height = this.getSize ( ).height - 25; screenBuffer = new int [ width * height ]; screenConverter = new MemoryImageSource ( width, height, screenBuffer, 0, width ); screenConverter.setAnimated ( true ); screenConverter.setFullBufferUpdates ( false ); fastPixelBuffer = createImage ( screenConverter ); backgroundBuffer = createImage ( this.getSize ( ).width, this.getSize ( ).height ); renderBackground ( backgroundBuffer.getGraphics ( ) ); doubleBuffer = createImage ( this.getSize ( ).width, this.getSize ( ).height ); tripleBuffer = createImage ( this.getSize ( ).width, this.getSize ( ).height ); doubleGraphics = doubleBuffer.getGraphics ( ); tripleGraphics = tripleBuffer.getGraphics ( ); } /** * @see java.applet.Applet#start() */ public void start ( ) { threadRun = true; thread.start ( ); } /** * @see java.applet.Applet#stop() */ public void stop ( ) { threadRun = false; while ( thread.isAlive ( ) ) { try { Thread.sleep ( 500 ); } catch ( InterruptedException e ) { } } } /** * @see java.awt.Component#paint(java.awt.Graphics) */ public void paint ( Graphics g ) { if ( screenRepaint ) { g.drawImage ( doubleBuffer, 0, 0, this ); screenRepaint = false; } } /** * @see java.applet.Applet#getAppletInfo() */ public String getAppletInfo ( ) { return "FastGraphics 3.3 - Copyright (c) 2005 by Jakob Vogel* This function is used to create a static background buffer. This image * contains a copyright message. *
* * @param g * The graphics object to use for rendering. */ private void renderBackground ( Graphics g ) { g.setColor ( new Color ( 255, 255, 255 ) ); g.fillRect ( 0, 0, getSize ( ).width, getSize ( ).height ); g.setColor ( new Color ( 85, 85, 85 ) ); g.fillRect ( 0, getSize ( ).height - 25, getSize ( ).width, 25 ); g.setColor ( new Color ( 255, 255, 255 ) ); g.drawString ( "FastGraphics 3.3 - (c) 2005 by Jakob Vogel", 10, getSize ( ).height - 10 ); } }