// scriptTest.java 「で、ソレ何の役に立つの?」と言われたスクリプトレコーダ // Dr.DOTIMPACT 1998/7/3 // 7/8 ラインを太くする(Sctatch.javaのルーチンをパクる) import java.applet.Applet; import java.awt.*; import java.util.Vector; public class scriptTest extends Applet implements Runnable { // スレッド Thread th; // ダブルバッファ Image buf; Graphics bufg; // 部品 Scrollbar stroke; TextField strk_disp; Button record, start, reset; // 変数 Vector script; Point[] scriptAry; Point last, there; int count, script_count; boolean rec, seq; public void init() { // スレッド初期化 th = null; // ダブルバッファ確保 buf = createImage(size().width, size().height - 30); bufg = buf.getGraphics(); bufg.setColor(Color.white); bufg.fillRect(0, 0, size().width, size().height - 30); bufg.setColor(Color.black); // 部品設定 setLayout(null); Label label = new Label("stroke", Label.CENTER); label.reshape(5, size().height - 25, 50, 20); add(label); stroke = new Scrollbar(Scrollbar.HORIZONTAL, 3, 1, 1, 10); stroke.reshape(55, size().height - 25, 100, 20); add(stroke); strk_disp = new TextField(2); strk_disp.reshape(160, size().height - 25, 20, 20); add(strk_disp); record = new Button("record"); record.reshape(205, size().height - 25, 50, 20); add(record); start = new Button("start"); start.reshape(260, size().height - 25, 50, 20); add(start); reset = new Button("reset"); reset.reshape(315, size().height - 25, 50, 20); add(reset); show(); // 初期値 script = new Vector(100, 50); last = new Point(0, 0); there = new Point(0, 0); count = 0; script_count = 0; rec = seq = false; } public void start() { if(th == null) { th = new Thread(this); th.start(); } } public void stop() { if(th != null) { th.stop(); th = null; } } public void run() { while(true) { try { Thread.sleep(50); } catch(Exception e) {} if(seq && script_count < script.size()) { if(script_count == 0) { //last = scriptAry[0]; last = script[0]; script_count = 1; } //there = scriptAry[script_count]; there = script[script_count]; //draw(); blushline(last.x, last.y, there.x, there.y); last = there; script_count++; if(script_count == script.size()) { bufg.drawString("script loaded.", 10, 20); seq = false; } } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(buf, 0, 0, this); } public boolean mouseDown(Event ev, int x, int y) { if(rec) { script.addElement(new Point(x, y)); } count++; last.x = x; last.y = y; return true; } public boolean mouseDrag(Event ev, int x, int y) { if(rec) { script.addElement(new Point(x, y)); } count++; there.x = x; there.y = y; //draw(); blushline(last.x, last.y, there.x, there.y); last.x = x; last.y = y; return true; } public boolean mouseUp(Event ev, int x, int y) { System.out.println(count); if(rec) { script.addElement(new Point(x, y)); } rec = false; // 暫定的 count = 0; there.x = x; there.y = y; //draw(); blushline(last.x, last.y, there.x, there.y); return true; } public boolean action(Event ev, Object what) { if(ev.target == record) { seq = false; count = 0; flash(); bufg.drawString("rec.", 10, 20); repaint(); script.removeAllElements(); // レコーダリセット rec = true; } if(ev.target == start) { if(script.size() > 0) { seq = false; rec = false; flash(); bufg.drawString("script load", 10, 20); repaint(); scriptAry = new Point[script.size()]; script.copyInto(scriptAry); script_count = 0; seq = true; } } if(ev.target == reset) { rec = false; seq = false; flash(); } return true; } // epoxy氏のScratch.javaからパクりました。 void blushline(int x1, int y1, int x2, int y2) { int x=0,y=0; int sign=0,dx,dy; if(x1==x2 && y1==y2) return; dx=Math.abs(x2-x1); dy=Math.abs(y2-y1); bufg.fillRect(x1, y1, 4, 4); if(dx > dy) { int yy=dy/2; if(x1 > x2) { int tmp; tmp=x1; x1=x2; x2=tmp; tmp=y1; y1=y2; y2=tmp; } sign=sgn(y2-y1); while(x <= dx) { x=x+1; yy+=dy; if(yy >= dx) { y+=sign; yy-=dx; } bufg.fillRect(x1+x, y1+y, 4, 4); } } else { int xx=dx/2; if(y1 > y2) { int tmp; tmp=x1; x1=x2; x2=tmp; tmp=y1; y1=y2; y2=tmp; } sign=sgn(x2-x1); while(y <= dy) { y=y+1; xx+=dx; if(xx >= dy) { x+=sign; xx-=dy; } bufg.fillRect(x1+x, y1+y, 4, 4); } } repaint(); } void draw() { bufg.drawLine(last.x, last.y, there.x, there.y); repaint(); } void flash() { bufg.setColor(Color.white); bufg.fillRect(0, 0, size().width, size().height - 30); bufg.setColor(Color.black); repaint(); } int sgn(int val) { int ans; if(val==0) ans=0; else if(val<0) ans=-1; else ans=1; return(ans); } }