// approach.java import java.applet.Applet; import java.applet.AppletContext; import java.awt.*; import java.awt.event.*; import java.net.MalformedURLException; import java.net.URL; import java.util.Random; public class approach extends Applet implements Runnable { class mml extends MouseMotionAdapter { public void mouseMoved (MouseEvent e) {_msX=e.getX(); _msY=e.getY();} public void mouseDragged (MouseEvent e) {_msX=e.getX(); _msY=e.getY();} mml () {} } class msl extends MouseAdapter { public void mousePressed (MouseEvent e) {_msX=e.getX(); _msY=e.getY(); _msDn = true; } public void mouseReleased (MouseEvent e) {_msDn = false;} msl () {} } class g2 { public void clr (int r, int g, int b) { _gfx.setColor (new Color (r, g, b)); _gfx.fillRect (0, 0, _w, _h); for (_y = 0; _y < _h; _y++) for (_x = 0; _x < _w; _x++) {_r[_x][_y] = r; _g[_x][_y] = g;} } public void draw (double x, double y, int r, int g, int b, double f) { if (x <= 0.0D || x >= (double)(_w-1) || y <= 0.0D || y >= (double)(_h-1)) return; int ix = (int)x, jx = ix + 1; int iy = (int)y, jy = iy + 1; double dx = (double)jx - x, ex = 1.0D - dx; double dy = (double)jy - y, ey = 1.0D - dy; double f1; f1 = dx * dy * f; _r[ix][iy] = (int)(f1 * (double)r + (double)_r[ix][iy]); if (_r[ix][iy] > 255) _r[ix][iy] = 255; _g[ix][iy] = (int)(f1 * (double)g + (double)_g[ix][iy]); if (_g[ix][iy] > 255) _g[ix][iy] = 255; f1 = ex * dy * f; _r[jx][iy] = (int)(f1 * (double)r + (double)_r[jx][iy]); if (_r[jx][iy] > 255) _r[jx][iy] = 255; _g[jx][iy] = (int)(f1 * (double)g + (double)_g[jx][iy]); if (_g[jx][iy] > 255) _g[jx][iy] = 255; f1 = dx * ey * f; _r[ix][jy] = (int)(f1 * (double)r + (double)_r[ix][jy]); if (_r[ix][jy] > 255) _r[ix][jy] = 255; _g[ix][jy] = (int)(f1 * (double)g + (double)_g[ix][jy]); if (_g[ix][jy] > 255) _g[ix][jy] = 255; f1 = ex * ey * f; _r[jx][jy] = (int)(f1 * (double)r + (double)_r[jx][jy]); if (_r[jx][jy] > 255) _r[jx][jy] = 255; _g[jx][jy] = (int)(f1 * (double)g + (double)_g[jx][jy]); if (_g[jx][jy] > 255) _g[jx][jy] = 255; _gfx.setColor (new Color (_r[ix][iy], _g[ix][iy], 0)); _gfx.drawLine (ix, iy, ix, iy); _gfx.setColor (new Color (_r[jx][iy], _g[jx][iy], 0)); _gfx.drawLine (jx, iy, jx, iy); _gfx.setColor (new Color (_r[ix][jy], _g[ix][jy], 0)); _gfx.drawLine (ix, jy, ix, jy); _gfx.setColor (new Color (_r[jx][jy], _g[jx][jy], 0)); _gfx.drawLine (jx, jy, jx, jy); } protected int _r[][]; protected int _g[][]; protected int _w; protected int _h; protected int _x; protected int _y; public g2 (int w, int h) { _w = w; _h = h; _r = new int[_w][_h]; _g = new int[_w][_h]; } } public approach () { _inited = false; _msDn = false; _msX = 0; _msY = 0; } public void init () { _w = getWidth (); _h = getHeight (); _img = createImage (_w, _h); _gfx = _img.getGraphics (); _inited = true; _g = new g2 (_w, _h); addMouseListener (new msl ()); addMouseMotionListener (new mml ()); } public void start () { if (_thr == null) {_thr = new Thread (this); _thr.start ();} } public void update (Graphics g) {paint (g);} public void paint (Graphics g) {if (_inited) g.drawImage (_img,0,0,this);} public void run () { int ln = 30; int dt = 10; double x [] = new double[ln]; double y [] = new double[ln]; double xf[] = new double[ln]; double yf[] = new double[ln]; for (int n = 0; n < ln; n++) {x[n] = 0.0D; y[n] = 0.0D; xf[n] = 0.0D; yf[n] = 0.0D;} _g.clr (0, 0, 0); while (true) { for (int n = 0; n < dt; n++) for (int i = 0; i < ln; i++) { double dx = (double)_msX - x[i]; double dy = (double)_msY - y[i]; xf[i] += 0.01 * dx * (1.0 - (0.75 * (double)i) / (double)ln); xf[i] *= 0.995; yf[i] += 0.01 * dy * (1.0 - (0.75 * (double)i) / (double)ln); yf[i] *= 0.995; x [i] += 0.01 * xf[i]; y [i] += 0.01 * yf[i]; _g.draw (x[i], y[i], 255, 80, 0, 0.5); } if (_msDn) { _msDn = false; _g.clr (0, 0, 0); dt = 3 + _msX * 18 / _w; // dt betw 3 and 20 based on _msX } repaint (); try {Thread.sleep (20L);} catch (InterruptedException interruptedexception) {} } } Thread _thr; Image _img; Graphics _gfx; int _w, _h; boolean _msDn, _inited; int _msX, _msY; g2 _g; }