// Written by Nicholas D. Exner, Exner Enterprises C.2000, May 2000 // Class DrawPoly uses code references to other classes such as // ManagePanel.class and Box.class that are the intellectual property of // Nicholas Exner. These classes have been included here with the // consent of the author. // //Adapted from the code for the applet "ToothPick.java" //3/3/01 import java.applet.*; import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.image.*; public class MLMToothPick extends Applet { Graphics h; Image offscreenImg; Image sign; Pick p; Point initpt1; Point initpt2; boolean pickInit; int currentX, currentY; public void init() { Pick.loadImages((Applet)this,(ImageObserver)this); offscreenImg = createImage(this.size().width,this.size().height); h = offscreenImg.getGraphics(); sign = getImage(getCodeBase(),"sign.gif"); add(new Button("Start Over")); initpt1 = new Point(52,51); p = new Pick(-100,-100); pickInit=false; initpt2 = new Point(initpt1.x + p.h+1, initpt1.y + p.w+1); setBackground(new Color(200,196,196)); repaint(); } public void paint(Graphics g) { h.setColor(new Color(200,196,196)); h.fillRect(0,0,750,500); h.drawImage(sign,0,0,this); h.setColor(new Color(0,0,0)); h.setColor(new Color(112,88,63)); h.setFont(new Font("TimesRoman",Font.BOLD,18)); h.drawString(""+p.numpicks+"",70,77); p.show(h); g.drawImage(offscreenImg,0,0,this); } public boolean mouseDown(Event evt, int x, int y) { currentX = x; currentY = y; p.mouseDown(x,y); if(p.getLast().select == false) { if(x > 158 && x < 239 && y > 44 && y < 110) { if(pickInit) { p.add(new Pick(x-(p.h/2),y-(p.w/2))); } else { p.setX(x-(p.h/2)); p.setY(y-(p.w/2)); pickInit = true; p.numpicks++; } } } p.mouseDown(x,y); repaint(); return true; } public boolean mouseUp(Event evt, int x, int y) { currentX = x; currentY = y; p.mouseUp(); repaint(); return true; } public boolean mouseDrag(Event evt, int x, int y) { currentX = x; currentY = y; p.mouseDrag(x,y); repaint(); return true; } public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { p.reset(initpt1.x,initpt1.y); pickInit = false; p.setX(-2*p.h); p.setY(-2*p.w); } repaint(); return true; } public boolean keyDown(Event evt, int key) { if (key == 'r' || key == 'R') if (p.getLast() != null) p.getLast().rotate(currentX,currentY); repaint(); return true; } public void update(Graphics g) { paint(g); } } // END CLASS ToothPick //////////////////////////////////////////////////////////// /////////////////// *****CLASS Pick***** /////////////////// //////////////////////////////////////////////////////////// class Pick { int x, y, w, h, pointOnImageX, pointOnImageY; Pick next; Image objImg, origImg; boolean select; static Image pickv, pickh; static ImageObserver iobp; static Pick lastPick = null; static int numpicks = 0; public Pick(int x, int y) { this.x = x; this.y = y; pointOnImageX = x; pointOnImageY = y; this.next = null; w=8; h=68; lastPick = this; select = false; System.out.print("X "+x+" Y1: "+y); objImg = pickh; origImg = objImg; } public Pick(int x, int y, Pick next) { this(x,y); add(next); } public void add(Pick next) { System.out.println("added"); if (this.next == null) { this.next = next; numpicks++; } else this.next.add(next); } public static void loadImages(Applet parent,ImageObserver iob) { iobp = iob; MediaTracker tracker = new MediaTracker(parent); pickv = parent.getImage(parent.getCodeBase(),"pickv.gif"); pickh = parent.getImage(parent.getCodeBase(),"pickh.gif"); tracker.addImage(pickv,0); tracker.addImage(pickh,1); tracker.checkAll(true); } public void show (Graphics g) { if (objImg != null) g.drawImage(objImg,x,y,iobp); if (next != null) next.show(g); } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public int getX() { return x; } public int getY() { return y; } public Pick getLast() { return lastPick; } public void rotate(int curX, int curY) { if (objImg == pickv) { objImg = pickh; setX(curX-h/2); setY(curY-w/2); // setX(curX + pointOnImageX); // setY(curY); } else { objImg = pickv; setX(curX-w/2); setY(curY-h/2); // setX(curX + pointOnImageY); // setY(curY + pointOnImageX); } pointOnImageX = x-curX; pointOnImageY = y-curY; } public void mouseDown(int x1, int y1) { select = false; if (objImg == pickv) if (x1 >= x && x1 <= x+w) if (y1 >= y && y1 <=y+h) select(x1,y1); if (objImg == pickh) if (x1 >= x && x1 <= x+h) if (y1 >= y && y1 <=y+w) select(x1,y1); if (!select && next != null) next.mouseDown(x1,y1); } public void mouseDrag(int x, int y) { if (select) { this.x=(x+pointOnImageX); this.y=(y+pointOnImageY); } else if (next != null) next.mouseDrag(x,y); } public void select(int x1,int y1) { pointOnImageX = x-x1; pointOnImageY = y-y1; select = true; lastPick = this; } public void mouseUp() { select = false; if (next != null) next.mouseUp(); } public void reset(int x,int y) { objImg = origImg; // if (next != null) // next.reset(); next=null; numpicks=0; this.x = x; this.y = y; } } // END CLASS Pick