//******************************************************************************** // Spangler.java // // (c) 1998 Maeda Mameo http://hw001.gate01.com/frog/ //******************************************************************************** public class Spangler extends java.applet.Applet implements Runnable{ private int appW, appH; private java.awt.Image imageIt, imageOff; private java.awt.Graphics gOff; private Thread threadIt; static final int MAXDUSTNUM = 100; private int dustState[] = new int[MAXDUSTNUM]; private int dustMaxSize[] = new int[MAXDUSTNUM]; private int dustSize[] = new int[MAXDUSTNUM]; private int dustX[] = new int[MAXDUSTNUM]; private int dustY[] = new int[MAXDUSTNUM]; private java.awt.Color dustColor; private double dustMagnification, dustDensity; private boolean isFPS; private int timeMillisPointer; static final int TIMEMILLISNUM = 50; private long[] latestTimeMillis = new long[TIMEMILLISNUM]; //******************************************************************************** //******************************************************************************** public void init(){ String s; java.awt.Dimension d; d = size(); appW = d.width; appH = d.height; imageOff = createImage( appW, appH); gOff = imageOff.getGraphics(); imageIt = getImage( getDocumentBase(), getParameter("image")); s = getParameter("size"); if( s == null) s = "5"; dustMagnification = (double)Integer.parseInt(s) / 5; if( dustMagnification <= 0 || dustMagnification > 2) dustMagnification = 1; dustMagnification = Math.pow( dustMagnification, 1.5); s = getParameter("density"); if( s == null) s = "5"; dustDensity = (double)Integer.parseInt(s) / 5; if( dustDensity <= 0 || dustDensity > 2) dustMagnification = 1; s = getParameter("color"); if( s == null) s = "ffff00"; dustColor = new java.awt.Color( Integer.parseInt( s, 16)); isFPS = false; } public void start(){ threadIt = new Thread(this); threadIt.start();} public void stop(){ threadIt.stop(); threadIt = null;} public void update( java.awt.Graphics g){ paint(g);} public void paint( java.awt.Graphics g){ gOff.drawImage( imageIt, 0, 0, this); drawDust(); g.drawImage( imageOff, 0, 0, this); } public boolean mouseEnter( java.awt.Event e, int x, int y){ isFPS = true; return true; } public boolean mouseExit( java.awt.Event e, int x, int y){ isFPS = false; showStatus(null); return true; } //******************************************************************************** //******************************************************************************** public void run(){ int i; double a; long t; resetDust(); timeMillisPointer = 0; latestTimeMillis[0] = System.currentTimeMillis(); for( i = 1; i < TIMEMILLISNUM; i++) latestTimeMillis[i] = 0; while(true){ if( Math.random() < .2 * dustDensity) addDust( (int)( Math.random() * appW), (int)( Math.random() * appH)); repaint(); sleeper(17); while(( t = System.currentTimeMillis()) - latestTimeMillis[timeMillisPointer] < 1000 / 20) sleeper(1); timeMillisPointer = ++timeMillisPointer % TIMEMILLISNUM; if(isFPS) showStatus( "" + ( (double)( 1000 * 1000 * TIMEMILLISNUM / ( t - latestTimeMillis[timeMillisPointer])) / 1000) + " frames/sec."); latestTimeMillis[timeMillisPointer] = t; } } private void resetDust(){ int i; for( i = 0; i < MAXDUSTNUM; i++) dustState[i] = 0; } private void addDust( int x, int y){ int i; for( i = 0; i < MAXDUSTNUM; i++) if( dustState[i] == 0){ dustX[i] = x; dustY[i] = y; dustMaxSize[i] = 10 + (int)( Math.random() * 30); dustSize[i] = 0; dustState[i] = 5; return; } } private void drawDust(){ int a, b, c, i, x, y; java.awt.Polygon p; for( i = 0; i < MAXDUSTNUM; i++) if( dustState[i] != 0){ dustSize[i] += dustState[i]; if( dustSize[i] >= dustMaxSize[i]){ dustSize[i] = dustMaxSize[i]; dustState[i] = -1; } a = dustSize[i]; if( a > 0){ x = dustX[i]; y = dustY[i]; b = (int)( dustMagnification * a / 5); c = (int)( dustMagnification * a / 2); p = new java.awt.Polygon(); p.addPoint( x, y - (int)( dustMagnification * 3 * a / 2)); p.addPoint( x + b, y - c); p.addPoint( x + c, y - b); p.addPoint( x + (int)( dustMagnification * a), y); p.addPoint( x + c, y + b); p.addPoint( x + b, y + c); p.addPoint( x, y + (int)( dustMagnification * 3 * a / 2)); p.addPoint( x - b, y + c); p.addPoint( x - c, y + b); p.addPoint( x - (int)( dustMagnification * a), y); p.addPoint( x - c, y - b); p.addPoint( x - b, y - c); gOff.setColor(dustColor); gOff.fillPolygon(p); gOff.drawLine( x, y - (int)( dustMagnification * 2 * a), x, y + (int)( dustMagnification * 2 * a)); gOff.drawLine( x - (int)( dustMagnification * 5 * a / 4), y, x + (int)( dustMagnification * 5 * a / 4), y); } else dustState[i] = 0; } } private void sleeper( int t){ try Thread.sleep(t); catch( InterruptedException e);} }