// wire.java 電線アプレット // Dr.DOTIMPACT 2000/8/19 import java.applet.Applet; import java.awt.*; public class wire extends Applet implements Runnable { // スレッド Thread th; // ダブルバッファ Image buf; Graphics bufg; // 画面には同時に2車両しか見えないと仮定 int tx1; boolean go; public void init() { th = null; // ダブルバッファ準備 buf = createImage(size().width, size().height); bufg = buf.getGraphics(); whiteout(); go = false; tx1 = Integer.MAX_VALUE; } public void start() { if(th == null) { th = new Thread(this); th.start(); } } public void stop() { if(th != null) { th.stop(); th = null; } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(buf, 0, 0, this); } public boolean mouseEnter(Event ev, int x, int y) { go = true; tx1 = 4000; return true; } public boolean mouseExit(Event ev, int x, int y) { go = false; return true; } public void run() { for(;;) { try { Thread.sleep(100); } catch(Exception e) {} if(go || tx1 != Integer.MAX_VALUE) { tx1 = tx1 - 200; draw(); repaint(); } if(tx1 < -4000-size().width) { if(go) { tx1 = 4000; } else { tx1 = Integer.MAX_VALUE; } } } } public void draw() { whiteout(); drawWire(tx1, size().height/2, 0.0001); drawWire(tx1+100, size().height/2, 0.0001); drawWire(tx1+500, size().height/4, 0.0004); } public void drawWire(int _x0, int _y0, double _ratio) { int step = 20; int x1; double y, y1; x1 = -1 * step; y1 = _y0 - _ratio * (x1 - _x0) * (x1 - _x0); for(int x=0 ; x <= size().width ; x += step) { y = _y0 - 0.00001 * (x - _x0) * (x - _x0); bufg.drawLine(x1, (int)y1, x, (int)y); x1 = x; y1 = y; } } public void whiteout() { bufg.setColor(Color.white); bufg.fillRect(0,0, size().width, size().height); bufg.setColor(Color.black); bufg.drawRect(0,0, size().width-1, size().height-1); } }