// SplineFun - a Java version of xsplinefun // // 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.*; public class SplineFun 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() { setBackground( Color.black ); } Thread thread = null; Graphics graphics = null; // 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; } } private static final int points = 5; // 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 ); int i; Dimension size = size(); // Dimension size = getSize(); Acme.IntGlide[] xG = new Acme.IntGlide[points]; Acme.IntGlide[] yG = new Acme.IntGlide[points]; Acme.IntGlide rG = new Acme.IntGlide( 0, 255, 2, 0 ); Acme.IntGlide gG = new Acme.IntGlide( 0, 255, 2, 0 ); Acme.IntGlide bG = new Acme.IntGlide( 0, 255, 2, 0 ); int r, g, b; for ( i = 0; i < points; ++i ) { xG[i] = new Acme.IntGlide( 0, size.width - 1, 3 ); yG[i] = new Acme.IntGlide( 0, size.height - 1, 3 ); } int[] x = new int[points]; int[] y = new int[points]; int px, py, zx, zy, nx, ny; int loops = 0; while ( thread == me ) { if ( graphics != null ) { for ( i = 0; i < points; ++i ) { x[i] = xG[i].next(); y[i] = yG[i].next(); } r = rG.next(); g = gG.next(); b = bG.next(); graphics.setColor( new Color( r, g, b ) ); px = ( x[0] + x[points-1] ) / 2; py = ( y[0] + y[points-1] ) / 2; zx = px; zy = py; for ( i = 0; i < points - 1; ++i ) { nx = ( x[i+1] + x[i] ) / 2; ny = ( y[i+1] + y[i] ) / 2; Acme.GuiUtils.drawSpline( graphics, px, py, x[i], y[i], nx, ny ); px = nx; py = ny; } Acme.GuiUtils.drawSpline( graphics, px, py, x[points-1], y[points-1], zx, zy ); } // 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 graphics ) { // Create a new graphics context for use by the run() routine. this.graphics = graphics.create(); } // Main program, so we can run as an application too. public static void main( String[] args ) { new Acme.MainFrame( new SplineFun(), args, 400, 400 ); } }