import java.awt.*; import java.awt.event.*; import java.lang.Math.*; import java.lang.*; import java.net.*; import java.security.*; public class DragonSirds extends java.applet.Applet implements Runnable, MouseListener { int i = 0; int c_i = 0; volatile int changed = 0; volatile int threadStop = 0; volatile int threadSuspend = 0; int old_width = 0; int old_height = 0; // Sirds variables int width = 0; int height = 0; //final int DPI = 72; final int DPI = 96; // HP has ~96 dpi //final int DPI = 600; // hi res printer //final int DPI = 300; // med res printer final double E = 2.5 * DPI; final double mu = 1.0 / 3.0; //Fractal variables volatile double a = 0.5; volatile double b = 0.5; final double MaxH = 3.0; final double MaxV = 3.0; final int MaxIt = 255; Thread myThread = null; Image im; Graphics buf; synchronized void mySuspend() { try { threadSuspend = 1; wait(); } catch (InterruptedException e) { } } synchronized void mySuspend( long timeout ) { try { threadSuspend = 1; wait( timeout ); threadSuspend = 0; } catch (InterruptedException e) { } } synchronized void myResume() { threadSuspend = 0; notify(); } public DragonSirds() { } public void run() { while( myThread != null && myThread.isAlive() && threadStop != 1 ) { Dimension d = getSize(); if ( d.width != old_width || d.height != old_height ) { im = createImage( getSize().width, getSize().height ); buf = im.getGraphics(); old_width = d.width; old_height = d.height; height = d.height; width = d.width; } try { myThread.sleep(1000); } catch (InterruptedException e) { } doIt( buf ); } threadStop = 0; System.out.println( "Dead Thread does suicide!" ); } public void init() { System.out.println( "init called" ); im = createImage( getSize().width, getSize().height ); buf = im.getGraphics(); width = getSize().width; height = getSize().height; myThread = new Thread(this); myThread.start(); threadSuspend = 1; addMouseListener( this ); } public void start() { System.out.println( "start called" ); if ( myThread != null ) { myResume(); } } public void stop() { System.out.println( "stop called" ); if ( myThread != null && myThread.isAlive() ) { threadSuspend = 1; } } public void destroy() { if ( myThread != null && myThread.isAlive() ) { threadStop = 1; myResume(); } System.out.println( "destroy called" ); } // Sirds methods double separation( double Z ) { return( ( 1.0 - mu * Z ) * E / ( 2.0 - mu * Z ) ); } void drawAutoStereogram( double Z[][], Graphics g ) { int x; int y; Graphics screen = getGraphics(); g.setColor( Color.white ); screen.setColor( Color.white ); g.fillRect( 0,0, width, height ); screen.fillRect( 0,0, width, height ); g.setColor( Color.black ); screen.setColor( Color.black ); for ( y = 0; y < height; y++ ) { int pix[] = new int[ width ]; int same[] = new int[ width ]; double s; int left; int right; for ( x = 0; x < width; x++ ) { same[x] = x; } for ( x = 0; x < width; x++ ) { s = separation( Z[x][y] ); left = x - (int)java.lang.Math.rint( s / 2.0 ); right = left + (int)java.lang.Math.rint( s ); if ( 0 <= left && right < width ) { boolean visible; int t = 1; double zt; do { zt = Z[x][y] + 2.0 * ( 2.0 - mu * Z[x][y]) * t / ( mu * E ); visible = Z[x-t][y] < zt && Z[x+t][y] < zt; t++; } while( visible && zt < 1.0 ); if ( visible ) { int l = same[ left ]; while ( l != left && l != right ) { if ( l < right ) { left = l; l = same[ left ]; } else { same[ left ] = right; left = right; l = same[ left ]; right = l; } } same[ left ] = right; } } } for ( x = width - 1; x >= 0; x-- ) { if ( same[x] == x ) { pix[x] = (int)( java.lang.Math.random() * Integer.MAX_VALUE ) & 1; } else { pix[x] = pix[same[x]]; } //Draw pixel if ( pix[x] == 0 ) { g.drawLine( x, y, x, y ); screen.drawLine( x, y, x, y ); } } } } void Zplot ( double Z[][], int x, int y, double z ) { if ( x < 0 ) x = 0; if ( y < 0 ) y = 0; x = java.lang.Math.min( x, width - 1 ); y = java.lang.Math.min( y, height - 1 ); //if ( Z[x][y] < z ) { Z[x][y] = z; } } //Fractal methods int iterate( double x, double y ) { int i; double NewX; boolean success; double localA = a; double localB = b; success = true; i = 0; while ( success && ( i < MaxIt ) ) { NewX = (x*x) - ( y*y ) - a; y = 2*x*y-b; x = NewX; success = ( java.lang.Math.abs(x) <= MaxH ) || ( java.lang.Math.abs(y) <= MaxV ); i++; } return i; } public void computeFractal ( double Z[][], Graphics g ) { //g.setColor( Color.red ); //g.setColor( Color(i)); Graphics screen = getGraphics(); //Color bg = getBackground(); Color bg = new Color( 0 ); g.setColor( bg ); screen.setColor( bg ); Dimension d = getSize(); //g.clearRect( 0,0, d.width, d.height ); g.fillRect( 0,0, d.width, d.height ); //screen.clearRect( 0,0, d.width, d.height ); screen.fillRect( 0,0, d.width, d.height ); int x; int y; double z; double h; double v; double HalfH = MaxH / 2.0; double HalfV = MaxV / 2.0; double StepH = MaxH / ( d.width ); double StepV = MaxH / ( d.height ); double ConX = ( d.width ) / MaxH; double ConY = ( d.height ) / MaxV; Color c; h = 0; while ( h < MaxH ) { v = 0; while ( v < MaxV ) { if ( myThread != null && !myThread.isAlive() ) { System.out.println( "Dead Thread Running!" ); return; } if ( changed == 1 ) { return; } if ( threadSuspend == 1 ) { System.out.println( "Thread Suspended" ); mySuspend(); } if ( threadStop == 1 ) { return; } c_i = iterate( h - HalfH, v - HalfV ); x = (int) java.lang.Math.rint( h*ConX ); y = (int) java.lang.Math.rint( v*ConY ); z = ((double) c_i) / ((double) MaxIt ); Zplot( Z, x, y, z ); if ( c_i > 1 ) //Paint only non black { //System.out.print( "i = " ); //System.out.println( c_i ); c_i = c_i << 8; // make it green c = new Color( c_i ); g.setColor( c ); screen.setColor( c ); g.drawLine( x, y, x, y ); screen.drawLine( x, y, x, y ); } v += StepV; } h += StepH; } } public void doIt ( Graphics g ) { double Z[][] = new double[width][height]; computeFractal( Z, g ); if ( changed == 0 ) { drawAutoStereogram( Z, g ); } if ( changed == 0 ) { mySuspend(100000); } if ( changed == 0 ) { a = java.lang.Math.random(); b = java.lang.Math.random(); } changed = 0; } public void paint ( Graphics g ) { System.out.println( "paint called" ); g.drawImage( im, 0,0, this ); } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { System.out.println( "mouseDown called" ); int x = e.getX(); int y = e.getY(); Dimension d = getSize(); a = ( (double) x ) / ( (double) d.width ); b = ( (double) y ) / ( (double) d.height ); changed = 1; myResume(); } public void mouseReleased( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } }