// RectToy2 // // Faster version of the simple animation applet. Here we break the // rule that says all drawing must be done from the paint() routine. // Instead, all the paint() routine does is save a copy of the graphics // context that it was passed. The thread loop uses this saved object to // draw directly. As a result, the animation runs much faster. // // Copyright (C)1996,1998 by Jef Poskanzer . All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF // SUCH DAMAGE. import java.applet.*; import java.awt.*; import java.util.Random; public class RectToy2 extends Applet implements Runnable { // Applet info. public String getAppletInfo() { return getClass().getName() + " - Copyright (C) 1996 by Jef Poskanzer . All rights reserved."; } // Called when the applet is first created. public void init() { Acme.GuiUtils.handleBgcolor( this ); } Thread thread = null; Graphics graphics = null; Random random = new Random(); int loops = 0; // Called when the applet should start itself. public void start() { if ( thread == null ) { // Start the thread. thread = new Thread(this); thread.start(); } } // Called when the applet should stop itself. public void stop() { if ( thread != null ) { // Stop the thread. thread.stop(); thread = null; } } // This is the part of Runnable that we implement - the routine that // gets called when the thread is started. public void run() { Thread me = Thread.currentThread(); me.setPriority( Thread.MIN_PRIORITY ); while ( thread == me ) { if ( graphics != null ) { Dimension d = size(); // Dimension d = getSize(); int w = Math.abs( random.nextInt() ) % ( d.width / 2 ); int h = Math.abs( random.nextInt() ) % ( d.height / 2 ); int x = Math.abs( random.nextInt() ) % ( d.width - w ); int y = Math.abs( random.nextInt() ) % ( d.height - h ); int r = Math.abs( random.nextInt() ) % 256; int g = Math.abs( random.nextInt() ) % 256; int b = Math.abs( random.nextInt() ) % 256; graphics.setColor( new Color( r, g, b ) ); graphics.fillRect( x, y, w, h ); } // Occasional sleeps to work around an X11 Netscape bug. ++loops; if ( loops % 10 == 0 ) { try { Thread.sleep( 10 ); } catch ( InterruptedException e ) {} } } if ( graphics != null ) graphics.dispose(); graphics = null; } // Called when the applet should paint itself. public void paint( Graphics g ) { // Create a new graphics context for use by the run() routine. graphics = g.create(); } }