// Floo.java - from flong.com's floo import java.applet.Applet; import java.awt.*; import java.awt.image.MemoryImageSource; import java.io.PrintStream; public class Floo extends Applet implements Runnable { public Floo () { _thr = null; _w = 200; _h = 200; _pTime = System.currentTimeMillis (); _mX = 0.0F; _mY = 0.0F; _goin = false; _nSing = 0; _sing = new Singularity [10]; _part = new Particle [290]; _velo = new Velocity (0.0F, 0.0F); } public void init () { setBackground (Color.darkGray); setForeground (Color.lightGray); _w = Integer.parseInt (getParameter ("width")); _h = Integer.parseInt (getParameter ("height")); resize (_w, _h); _img = createImage (_w, _h); _nPxl = _w * _h; _pxl = new int [_nPxl]; for (int i = 0; i < _nPxl; i++) _pxl [i] = 0xff000000; _mis = new MemoryImageSource (_w, _h, _pxl, 0, _w); _mis.setAnimated (true); _lattice = createImage (_mis); for (int j = 0; j < 290; j++) _part [j] = new Particle (); for (int k = 0; k < 10; k++) _sing [k] = new Singularity (); } public void destroy () {} public void start () { _die = false; requestFocus (); if (_thr == null) {_thr = new Thread (this); _thr.start ();} } public void stop () { clearItAll (); update (); _die = true; if (_thr != null) {_thr.stop (); _thr = null;} } public void run () { while (_thr != null) { long l = System.currentTimeMillis (); if (System.currentTimeMillis () - _pTime > 0x927c0L) { clearPixels (); _pTime = System.currentTimeMillis (); } // jitterSingularities for (int i = 0; i < _nSing; i++) if (_sing [i].id == 1) { _sing [i].x += (float)((Math.random () - 0.5D) / 100D); _sing [i].y += (float)((Math.random () - 0.5D) / 100D); } updatePixelsBogus (); try { if (! _die) { try {Thread.sleep (10L);} catch (InterruptedException _ex) { } long l1 = System.currentTimeMillis (); int i = (int)(23L - (l1 - l)); if (i > 0) try {Thread.sleep (i);} catch (InterruptedException _ex) { } continue; } if (_thr == null) continue; _thr.stop (); _thr = null; } catch (Exception _ex) {} break; } } public void update () {paint (getGraphics ());} public void update (Graphics g) {paint (g);} public synchronized void paint (Graphics g) { try { _img.getGraphics ().drawImage (_lattice, 0, 0, this); simulate (); g.drawImage (_img, 0, 0, this); } catch (NullPointerException nullpointerexception) {System.out.println ("error: " + nullpointerexception.getMessage ());} } public synchronized void updatePixelsBogus () { boolean flag = false; boolean flag1 = false; int j1 = (32 + (int)(Math.random () * 3D)) - 1; int k1 = _w - 2; byte byte0 = 2; int l1 = _h - 2; byte byte1 = 2; for (int i2 = 0; i2 < 290; i2++) { Particle particle; if ((particle = _part [i2]).exists) { int i = Math.round (particle.x * 50F); int j = _h - Math.round (particle.y * 50F); if (i < k1 && i > 2 && j < l1 && j > 2) { int l = j * _w + i; int k = _pxl [l]; int i1 = (k & 0xff) + j1; k |= i1 << 16 | i1 << 8 | i1; _pxl [l] = k; blur (i, j); blur (i + 1, j); blur (i - 1, j); blur (i, j + 1); blur (i, j - 1); blur (i + 1, j + 1); blur (i - 1, j - 1); blur (i + 1, j - 1); blur (i - 1, j + 1); } } } for (int l2 = 0; l2 < 100; l2++) { int j2 = (int)(1.0D + Math.random () * (double)k1); int k2 = (int)(1.0D + Math.random () * (double)l1); blur (j2, k2); } _mis.newPixels (0, 0, _w, _h); } public synchronized void blur (int i, int j) { int k = j - 1; int l = j + 1; int i1 = i - 1; int j1 = i + 1; int k1 = k * _w; int l1 = j * _w; int i2 = l * _w; int j2 = 0xff & _pxl [k1 + i1]; int k2 = 0xff & _pxl [k1 + i]; int l2 = 0xff & _pxl [k1 + j1]; int i3 = 0xff & _pxl [l1 + i1]; int j3 = 0xff & _pxl [l1 + i]; int k3 = 0xff & _pxl [l1 + j1]; int l3 = 0xff & _pxl [i2 + i1]; int i4 = 0xff & _pxl [i2 + i]; int j4 = 0xff & _pxl [i2 + j1]; double d = (double)(j2 + 2 * k2 + l2 + 2 * i3 + 4 * j3 + 2 * k3 + l3 + 2 * i4 + j4) / 16D; int k4 = Math.min (212, (int)Math.round (d)); int l4 = Math.min (255, (int)Math.round (d * 1.2D)); int i5 = 0xff000000 | l4 << 16 | k4 << 8 | k4; _pxl [j * _w + i] = i5; } void clearCanvas (Graphics g) { g.setColor (Color.black); g.fillRect (0, 0, _w, _h); } void drawSymbol (Singularity s, Graphics g) { byte b = 6; int i = Math.round (s.x * 50F); int j = _h - Math.round (s.y * 50F); switch (s.id) { case 1: g.setColor (Color.blue); break; case 2: g.setColor (Color.red); break; } g.fillOval (i - b/2, j - b/2, b, b); } void simulate () { scaleSourceStrengths (); if (_nSing > 0) updateParticles (); } void updateParticles () { Graphics g = _img.getGraphics (); float f8 = 0.0F; float f10 = 0.0F; boolean flag = true; for (int l = 0; l < 290; l++) { Particle particle; if (! (particle = _part [l]).exists) continue; float f4 = particle.x; float f5 = particle.y; int i = Math.round (f4 * 50F); int j = _h - Math.round (f5 * 50F); int k = 0; boolean flag1; if (i < _w && i > 0 && j < _h && j > 0) { k = _pxl [j * _w + i] & 0xff; flag1 = true; } else {flag1 = false;} if (flag1) { particle.addPoint (i, j, k); particle.draw (g); float f = 0.0F; float f1 = 0.0F; for (int i1 = 0; i1 < _nSing; i1++) { _velo = _sing [i1].V (f4, f5); f += _velo.u; f1 += _velo.v; if (_velo.inf) break; } if (_velo.inf) break; float f9 = (float)Math.sqrt (f * f + f1 * f1); float f12 = f9 * 0.025F; if (f9 == 0.0F) break; float f6 = f4 + f / f12; float f7 = f5 + f1 / f12; float f2 = 0.0F; float f3 = 0.0F; for (int j1 = 0; j1 < _nSing; j1++) { _velo = _sing [j1].V (f6, f7); f2 += _velo.u; f3 += _velo.v; if (_velo.inf) break; } if (_velo.inf) break; float f11 = (float)Math.sqrt (f2 * f2 + f3 * f3); if ((double)f11 == 0.0D) break; particle.x += (f / f9 + f2 / f11) * 0.0125F; particle.y += (f1 / f9 + f3 / f11) * 0.0125F; } else {particle.reset ();} } } public void initParticles (Singularity singularity, int i) { float f = 0.1F; double d = 0.21666156231653746D; int j = i * 29; int k = j + 29; for (int l = j; l < k; l++) { float f1 = (float)((double)f * Math.cos (d * (double)l)); float f2 = (float)((double)f * Math.sin (d * (double)l)); _part [l].set (f1, f2, singularity); } } public void scaleSourceStrengths () { long l = System.currentTimeMillis (); for (int i = 0; i < _nSing; i++) { long l1 = l - _sing [i].birthday; float f = 0.1F + 0.9F * ((float)l1 / 5000F); _sing [i].s = f; } _sing [0].s *= 1.1F * (float)Math.max (1, _nSing); } public void createSingularity (float f, float f1, float f2, int i) { if (_nSing < 10) { _sing [_currSing].set (f, f1, f2, i); if (i == 1) initParticles (_sing [_currSing], _currSing); } else if (_nSing == 10) { _sing [_currSing].set (f, f1, f2, i); int j = _currSing * 29; int k = j + 29; if (i == 1) { if (_part [j].exists) { for (int l = j; l < k; l++) if (_part [l].exists) _part [l].reset (); } else {initParticles (_sing [_currSing], _currSing);} } else if (_part [j].exists) { for (int i1 = j; i1 < k; i1++) if (_part [i1].exists) _part[i1].exists = false; } } _nSing = Math.min (10, _nSing + 1); _currSing = (_currSing + 1) % 10; if (_currSing == 0 && _goin) _currSing++; } public synchronized void clearItAll () { clearPixels (); for (int i = 0; i < 290; i++) _part [i].exists = false; for (int j = 0; j < 10; j++) _sing [j].exists = false; _nSing = 0; _currSing = 0; _goin = false; } public synchronized void clearPixels () { for (int i = 0; i < _nPxl; i++) _pxl [i] = 0xff000000; _mis.newPixels (0, 0, _w, _h); clearCanvas (_img.getGraphics ()); } public boolean mouseDown (Event event, int i, int j) { if (! _goin) { _goin = true; if (_nSing == 0) { _mX = (float)i / 50F; _mY = (float)(_h - j) / 50F; float f = 0.1F; createSingularity (_mX, _mY, (float)((double)f / 3.1415926535897931D / 2D), 1); } } return true; } public boolean mouseUp (Event event, int i, int j) { _pTime = System.currentTimeMillis (); float f = 0.1F; if (i >= 0 && i <= _w && j >= 0 && j <= _h) { _mX = (float)i / 50F; _mY = (float)(_h - j) / 50F; if ((event.modifiers & 4) > 0) createSingularity (_mX, _mY, (float)((double)f / 3.1415926535897931D / 2D), 2); else createSingularity (_mX, _mY, (float)((double)f / 3.1415926535897931D / 2D), 1); } return true; } public boolean mouseMove (Event event, int i, int j) { _pTime = System.currentTimeMillis (); if (i >= 0 && i <= _w && j >= 0 && j <= _h) { _mX = (float)i / 50F; _mY = (float)(_h - j) / 50F; if (_goin && _nSing > 0) { float f = _sing [0].s; _sing [0].set (_mX, _mY, f, 1); } } return true; } public boolean mouseDrag (Event event, int i, int j) { if (i >= 0 && i <= _w && j >= 0 && j <= _h) { _mX = (float)i / 50F; _mY = (float)(_h - j) / 50F; if (_goin && _nSing > 0) { float f = _sing [0].s; _sing [0].set (_mX, _mY, f, 1); } } return true; } public boolean keyDown (Event event, int i) { if (i == 13 || i == 10 || i == 32) clearItAll (); return true; } private static int _currSing; private Image _img, _lattice; private long _pTime; private int _w, _h, _nPxl, _nSing; private float _mX, _mY; private boolean _goin; private int _pxl []; private Singularity _sing []; private Particle _part []; private Velocity _velo; private MemoryImageSource _mis; Thread _thr; boolean _die; }