// 先生.class // Dr. dotimpact 98/8/27 // 98.8.31 考えるの面倒になったのでとりあえずRunnableで実装 import java.applet.Applet; import java.awt.*; public class sensei extends Applet implements Runnable{ // スレッド Thread th; // ダブルバッファ Image buf; Graphics bufg; // 部品 //Scrollbar slider; Wiper slider; // 変数 Point last, there; int sliderpos; public void init() { th = null; // ダブルバッファ確保 buf = createImage(size().width, size().height - 30); bufg = buf.getGraphics(); bufg.setColor(Color.lightGray); bufg.fillRect(0, 0, size().width, size().height - 30); bufg.setColor(Color.black); // 部品設定 setLayout(null); slider = new Wiper(buf, size().width, size().height - 30); slider.reshape(5, size().height - 25, size().width - 10, 20); add(slider); // スライダをAdjustmentListenerに登録 //slider.addAdjustmentListener(this); // 初期設定 last = new Point(0, 0); there = new Point(0, 0); sliderpos = 0; } 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() { for(;;) { try { Thread.sleep(100); } catch(Exception e){} repaint(); } } 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) { last.x = x; last.y = y; return true; } public boolean mouseDrag(Event ev, int x, int y) { 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) { there.x = x; there.y = y; //draw(); blushline(last.x, last.y, there.x, there.y); return true; } /* public void adjustmentValueChanged(AdjustmentEvent e) { int cur = slider.getValue(); bufg.setColor(Color.white); if(cur < sliderpos) { bufg.fillRect(cur, 0, sliderpos - cur, size().height - 25); } else { bufg.fillRect(sliderpos, 0, cur - sliderpos, size().height - 25); } sliderpos = cur; bufg.setColor(Color.black); repaint(); } */ 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(); } int sgn(int val) { int ans; if(val==0) ans=0; else if(val<0) ans=-1; else ans=1; return(ans); } } class Wiper extends Scrollbar { Image img; Dimension dim; int last; public Wiper(Image img, int width, int height) { super(Scrollbar.HORIZONTAL, 0, 1, 0, width); dim = new Dimension(width, height); this.img = img; last = 0; } public boolean handleEvent(Event ev) { Integer value = (Integer)ev.arg; switch (ev.id) { case Event.SCROLL_LINE_UP: case Event.SCROLL_LINE_DOWN: case Event.SCROLL_PAGE_UP : case Event.SCROLL_PAGE_DOWN : last = value.intValue(); break; case Event.SCROLL_ABSOLUTE: //System.out.println("!"); int cur = value.intValue(); Graphics g = img.getGraphics(); g.setColor(Color.lightGray); if(cur < last) { g.fillRect(cur, 0, last - cur, dim.height); } else { g.fillRect(last, 0, cur - last, dim.height); } last = cur; g.setColor(Color.black); //repaint(); break; } return true; } }