// // 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. import java.applet.*; import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.image.*; public class DrawPoly extends Applet { NewPolygon polygon; Image offscreenImg; Image back; Graphics h; static ManagePanel mp; boolean clickOnce; static int circleNum; public void init() { back = getImage(getCodeBase(),"polyangle2.jpg"); offscreenImg = createImage(this.size().width,this.size().height); h = offscreenImg.getGraphics(); reset(); } public void reset() { circleNum = 0; polygon = new NewPolygon(); mp = new ManagePanel(); clickOnce = false; } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ h.drawImage(back,0,0,this); polygon.show(h); if (polygon.allPoints && !clickOnce) { for (int i=0;i 15 && x < 265) if (y > 15 && y < 265) polygon.addPoint(x,y); if (x > 280 && x < 280+100) if (y > 175+60 && y < 175+90) reset(); // System.out.println(""+x+" "+y); mp.down(x,y); repaint(); return true; } public boolean mouseUp(Event evt, int x, int y) { mp.up(x,y); repaint(); return true; } public boolean mouseMove(Event evt, int x, int y) { // System.out.println("mouse at x:"+x+" y:"+y); return true; } public boolean mouseDrag(Event evt, int x, int y) { mp.drag(x,y); repaint(); return true; } public Image makeImage(int x, int y) { Image temp = createImage(31,31); Graphics gr = temp.getGraphics(); gr.drawImage(offscreenImg,-x,-y,this); gr.setColor(Color.blue); gr.drawRect(0,0,30,30); return temp; } } // End class ////////////////////////// NewPolygon //////////////////////////////// class NewPolygon { Polygon p; boolean allPoints; static double sumValue; public NewPolygon() { p = new Polygon(); allPoints = false; sumValue=0; } public int getMax(){ return p.npoints; } public int getX(int n) { return p.xpoints[n]; } public int getY(int n) { return p.ypoints[n]; } public void addPoint(int x, int y){ if (p.npoints > 0) if (x < p.xpoints[0]+10 && x > p.xpoints[0]-10) if (y < p.ypoints[0]+10 && y > p.ypoints[0]-10) allPoints = true; if (!allPoints) p.addPoint(x,y); } public void show(Graphics g) { if (allPoints) { g.setColor(Color.green); g.fillPolygon(p); g.setColor(Color.black); g.drawPolygon(p); /////// g.setColor(Color.red); for (int i=0;i= 3) for (int i =0 ; i < p.npoints;i++){ g.drawString(""+Math.round(angleMeasure(i)),p.xpoints[i]- 10,p.ypoints[i]-17); } /////// } else { g.setColor(Color.black); for (int i =1 ; i < p.npoints;i++){ g.drawLine(p.xpoints[i-1],p.ypoints[i- 1],p.xpoints[i],p.ypoints[i]); } for (int i =0 ; i < p.npoints;i++){ g.drawOval(p.xpoints[i],p.ypoints[i],2,2); } } // End else showArc(g); } // End show public void showArc(Graphics g){ int tempSum = (int)Math.round(sumValue); int circleNum = tempSum / 360; int endDegrees = tempSum % 360; DrawPoly.circleNum = circleNum; circleNum = circleNum % 4; switch(circleNum) { case 0: g.setColor(Color.green); break; case 1: g.setColor(Color.yellow); break; case 2: g.setColor(Color.red); break; case 3: g.setColor(new Color(50,50,255)); break; case 4: g.setColor(Color.white); break; case 5: g.setColor(new Color(255,0,255)); break; default: g.setColor(new Color(150,150,255)); } g.fillOval(292,35,80,80); switch(circleNum+1) { case 0: g.setColor(Color.green); break; case 1: g.setColor(Color.yellow); break; case 2: g.setColor(Color.red); break; case 3: g.setColor(new Color(50,50,255)); break; case 4: g.setColor(Color.white); break; case 5: g.setColor(new Color(255,0,255)); break; default: g.setColor(new Color(150,150,255)); } g.fillArc(292,35,80,80,90,-endDegrees); g.setColor(Color.black); g.drawArc(292,35,80,80,90,-endDegrees); g.drawString(""+Math.round(sumValue),326,76); } public double angleMeasure(int n) { int left=n-1, right=n+1; if (left == -1) left = p.npoints-1; if (n == p.npoints-1) right = 0; double distA, distB=1, distC; distA = Math.sqrt(Math.pow((p.xpoints[left]-p.xpoints[n]),2)+ Math.pow((p.ypoints[left]-p.ypoints[n]),2)); distB = Math.sqrt(Math.pow((p.xpoints[right]-p.xpoints[n]),2)+ Math.pow((p.ypoints[right]-p.ypoints[n]),2)); distC = Math.sqrt(Math.pow((p.xpoints[right]-p.xpoints[left]),2)+ Math.pow((p.ypoints[right]-p.ypoints[left]),2)); double theta = Math.acos((distA*distA + distB*distB - distC*distC)/(2*distA*distB)); theta = 180*theta/Math.PI; if(checkInside(left,n,right)) theta = ((360-theta)%360+360)%360; return theta; } // End angleMeasure public boolean checkInside(int left, int n, int right) { double midX = Math.abs(p.xpoints[left]+p.xpoints[right])/2; double midY = Math.abs(p.ypoints[left]+p.ypoints[right])/2; /* double distA=Math.sqrt((p.xpoints[left]-p.xpoints[n])*(p.xpoints[left]- p.xpoints[n]) +(p.ypoints[left]-p.ypoints[n])*(p.ypoints[left]-p.ypoints[n])); double distB=Math.sqrt((p.xpoints[right]-p.xpoints[n])*(p.xpoints[right]-p.xpoints[n]) +(p.ypoints[right]-p.ypoints[n])*(p.ypoints[right]-p.ypoints[n])); double slope =(midY-p.ypoints[n])/(midX - p.xpoints[n]); double slope2=0.0; if (distA > distB) slope2=(p.ypoints[left]-p.ypoints[n])/(p.xpoints[left] - p.xpoints[n]); else slope2=(p.ypoints[right]-p.ypoints[n])/(p.xpoints[right] - p.xpoints[n]); */ // if (Math.abs(slope - slope2) > .1) { for (int i=0;i<10;i++) { midX = Math.abs(p.xpoints[n]+midX)/2; midY = Math.abs(p.ypoints[n]+midY)/2; // System.out.println("Running..."); } // } // boolean bob = false; // if (midX == ) // double slope // if (Math.round(midY) == Math.round(slope*midX+b)) // return true; // Then outside // System.out.println ("x:" + midX); // System.out.println ("y:" + midY); if (p.contains(midX,midY)) return false; else return true; } } // End NewPolygon ////////////////////////////// ManagePanel ////////////////////////////////////////// class ManagePanel { Vector objVect; public ManagePanel() { objVect = new Vector(); } public void addObj(Object x) { objVect.addElement(x); } public void show(Graphics g) { for (int i=0;i a[j].getX()) { temp = a[i]; a[i]=a[j]; a[j]=temp; } if (a[i].getY() < a[j].getY()) { temp = a[i]; a[i]=a[j]; a[j]=temp; } } } int max = objVect.size(); objVect = new Vector(); for (int i=0;i 294 && x < 368) if (y > 35 && y < 112) if (c.contact(x,y)){ NewPolygon.sumValue += c.angleValue; objVect.removeElementAt(i); } //// } } public void down(int x, int y) { for (int i=0;i= x && x1 <= x+31) { if (y1 >= y && y1 <=y+31) { return true; } } select = false; return false; } public void deselect() { select = false; } public void select(int pointOnImageX1,int pointOnImageY1) { pointOnImageX = x-pointOnImageX1; pointOnImageY = y-pointOnImageY1; select = true; } public void objectDrag(int x, int y) { if (select) { this.x=(x+pointOnImageX); this.y=(y+pointOnImageY); } } } // End class Box ///////////////////////////////////