// wire.class 電線アプレット // Dr.DOTIMPACT 1998/7/9 import java.applet.Applet; import java.awt.*; public class train extends Applet implements Runnable { // スレッド Thread th; // ダブルバッファ Image buf; Graphics bufg; // 画面には同時に2車両しか見えないと仮定 int tx1, tx2, ty; boolean go; public void init() { th = null; // ダブルバッファ準備 buf = createImage(size().width, size().height); bufg = buf.getGraphics(); blackout(); go = false; tx1 = Integer.MAX_VALUE; tx2 = Integer.MAX_VALUE; ty = 100; } 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 = -445; tx2 = -960; 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 || tx2 != Integer.MAX_VALUE) { tx1 = tx1 + 140; tx2 = tx2 + 140; draw(); repaint(); } if(tx1 > size().width) { if(go) { tx1 = tx2 - 445 - 60; } else { tx1 = Integer.MAX_VALUE; } } if(tx2 > size().width) { if(go) { tx2 = tx1 - 445 - 60; } else { tx2 = Integer.MAX_VALUE; } } } } public void draw() { blackout(); if(tx1 > -445 && tx1 < size().width) { train(tx1, ty); } if(tx2 > -445 && tx2 < size().width) { train(tx2, ty); } } // 車両 30*2 + 95*2 + 45*3 + 10*6 = 445 int train(int x1, int y1) { int x; x = window1(x1, 100); x = doorwindow(x+10, 100); x = window2(x+10, 100); x = doorwindow(x+10, 100); x = window2(x+10, 100); x = doorwindow(x+10, 100); x = window1(x+10, 100); return x1 + 445; } // 開閉式小窓 width = 30 height = 37 int window1(int x, int y) { if(x >= -30 || x <= size().width) { bufg.fillRect(x, y , 30, 15); bufg.fillRect(x, y+15+2, 30, 15); } return x + 30; } // 扉の窓 width = 45 height = 40 int doorwindow(int x, int y) { if(x >= -45 || x <= size().width) { bufg.fillRoundRect(x , y, 20, 40, 6, 6); bufg.fillRoundRect(x+20+5, y, 20, 40, 6, 6); } return x + 45; } // 窓 width = 95 height = 33 int window2(int x, int y) { if(x >= -95 || x <= size().width) { bufg.fillRoundRect(x , y, 45, 33, 10, 10); bufg.fillRoundRect(x+45+5, y, 45, 33, 10, 10); } return x + 95; } void blackout() { bufg.setColor(Color.black); bufg.fillRect(0,0, size().width, size().height); bufg.setColor(Color.white); } }