// RectToy1 // // A simple animation applet. This version plays by the rules, drawing // only from within the paint() routine. All the thread routine does is // repeatedly ask for repaints. Because of the extra overhead and because // multiple repaint requests can get combined into one call to paint(), // this version runs fairly slowly. // // 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 RectToy1 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; 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 ) { repaint(); // Occasional sleeps to work around an X11 Netscape bug. ++loops; if ( loops % 10 == 0 ) { try { Thread.sleep( 10 ); } catch ( InterruptedException e ) {} } } } // The default update() clears the background and then calls paint(). // This one doesn't clear the background. public void update( Graphics graphics ) { paint( graphics ); } // Called when the applet should paint itself. public void paint( Graphics graphics ) { 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 ); } }