//****************************************************************************** // StringArt.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; //============================================================================== // Main Class for applet StringArt // // The String art applet demonstrates the incremental sinusoidal // calculation given by linear prediction theory. Apparently // sinusoids are very predictable, requiring the calculation of // only two points using the Math.cos() routine. // // // Consider the sinusoid // // x = A cos [ p - n w ] // n // // where // // A = amplitude // p = initial phase // w = frequency // n = integer from 0 to N-1 // // Starting with the two points: // // x = A cos [ p - w ] // 0 // // x = A cos [ p - 2w ] // 1 // // // The remaining points can be calculated with // // x = 2*cos[ w ] ( x - x ) // n n-1 n-2 // // Note that 2*cos[ w ] may be calculated beforehand. // //============================================================================== public class StringArt extends Applet implements Runnable { // THREAD SUPPORT: // m_StringArt is the Thread object for the applet //-------------------------------------------------------------------------- Thread m_StringArt = null; // StringArt Class Constructor //-------------------------------------------------------------------------- public StringArt() { } // APPLET INFO SUPPORT: // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. //-------------------------------------------------------------------------- public String getAppletInfo() { return "Name: StringArt\r\n" + "Author: Michael Svihura\r\n" + "Created with Microsoft Visual J++ Version 1.0"; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. //-------------------------------------------------------------------------- public void init() { Dimension dim = size(); nWidth = dim.width; nHeight = dim.height; int i; lineEndPointParams = new CurveParameter[4]; for ( i=0;i<4;i++ ) lineEndPointParams[i] = new CurveParameter(); lineEndPointValues = new CurveValue[4]; for ( i=0;i<4;i++ ) lineEndPointValues[i] = new CurveValue(); l1x = new double[ 2*nLines ]; l1y = new double[ 2*nLines ]; l2x = new double[ 2*nLines ]; l2y = new double[ 2*nLines ]; SetRainbowScale(); } // destroy() is called when when you applet is terminating // and being unloaded. //------------------------------------------------------------------------- public void destroy() { } // Compute a rainbow-like colormap //-------------------------------------------------------------------------- private void SetRainbowScale() { int i; double x; for ( i = 0; i < nLines*2; i++ ) { x = (double)( i ) / (double)( nLines*2 ) * 2.0 * Math.PI; R[i] = (int)((Math.cos(x + 0.0) + 1.0) * 127.0); G[i] = (int)((Math.cos(x + 2.094395) + 1.0) * 127.0); B[i] = (int)((Math.cos(x - 2.094395) + 1.0) * 127.0); } } // Helper routine to convert a line point into an integer value. // For speed, this is implemented as a final static method. //-------------------------------------------------------------------------- private final static int ToPoint( double x, int iRes ) { return (int)(( x + 1.0 ) / 2.0 * (double)(iRes - iRes/10) + iRes/20 ); } // StringArt Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { Dimension dim = size(); g.setColor( new Color( 0, 0, 0 ) ); g.fillRect( 0, 0, nWidth, nHeight ); int i; for ( i = 0; i