import java.awt.*; import java.applet.*; import java.util.*; import java.awt.event.*; import java.lang.String; public class RectPerim extends Applet { Graphics offScreenG; Image offScreenImg, backImg; boolean redraw = true; int X, Y, traceCount = 0, setLineOfConstraint = -1; int constraintX, constraintY; int xTracePoints[] = new int[1000], yTracePoints[] = new int[1000]; int width, length, heldPerimWidth, heldPerimLength, heldAreaWidth, heldAreaLength; int pastLength, pastWidth; float area, perimeter, slope, tempLength, tempWidth, shownLength, shownWidth; Font f = new Font("Times New Roman", Font.BOLD, 20); Checkbox Area; Checkbox Perimeter; Checkbox Constraint; Checkbox Trace; public void init() { backImg = getImage(getCodeBase(),"back.jpg"); offScreenImg = createImage(this.size().width, this.size().height); offScreenG = offScreenImg.getGraphics(); repaint(); Area = new Checkbox("Set Area"); add(Area); Perimeter = new Checkbox("Set Perimeter"); add(Perimeter); Constraint = new Checkbox("Line of Constraint"); add(Constraint); Trace = new Checkbox("Trace"); add(Trace); } public void paint (Graphics g){ if (redraw){ offScreenG.drawImage(backImg,0,0,this); show(offScreenG); g.setFont(f); g.drawImage(offScreenImg,0,0,this); if ((width > 0 && width < 40) || (length > 0 && length < 40)) g.setColor(Color.red); else g.setColor(Color.black); g.drawString("Area: " + roundValue(area),30,40); g.drawString("Perimeter: " + roundValue(perimeter),30,68); g.drawString("Width: " + roundValue(shownWidth), 230, 40); g.drawString("Length: " + roundValue(shownLength), 230, 68); } } public float roundValue (float x){ float y = x*1000; y = (int)y; y = (float)y/1000; return y; } public void update (Graphics g) { paint(g); } public boolean mouseDown(Event evt, int x, int y){ System.out.println("X: " + x + "Y: " + y); if (Constraint.getState()){ if (setLineOfConstraint == -1){ setLineOfConstraint = 0; constraintX = x; constraintY = y; } repaint(); } down(x,y); repaint(); return true; } public boolean mouseDrag(Event evt, int x, int y){ drag(x,y); return true; } public void down(int x, int y){ X = x; Y = y; setLengthAndWidth(); setAreaAndPerimeter(); if (Trace.getState()){ if (traceCount < 1000){ xTracePoints[traceCount] = width + 20; yTracePoints[traceCount] = 380 - length; traceCount++; } } } public void drag(int x, int y){ X = x; Y = y; setLengthAndWidth(); setAreaAndPerimeter(); if (Trace.getState()){ if (traceCount < 1000){ xTracePoints[traceCount] = width + 20; yTracePoints[traceCount] = 380 - length; traceCount++; } } repaint(); } public void setLengthAndWidth(){ redraw = true; if (X>=20 && X<=380) width = X-20; else if (X<20) width = 0; else width = 360; if (Y>=100 && Y<=380) length = 380-Y; else if (Y>380) length = 0; else{ Y=100; length = 280; } if(Area.getState()){ float temp = (float)heldAreaWidth/width; float tempLength = (float)heldAreaLength * temp; length = (int)tempLength; if (length > 280){ length = 280; width = heldAreaWidth*heldAreaLength/length; } if (Constraint.getState()) redraw = false; } if(Perimeter.getState()){ int temp = heldPerimWidth - width; length = heldPerimLength + temp; if (length > 280){ length = 280; width = (heldPerimLength + heldPerimWidth) - length; } if (length < 0){ length = 0; } if (Area.getState()){ if (length != heldAreaWidth || width != heldAreaLength) if (length != heldAreaLength || width != heldAreaWidth){ redraw = false; length = pastLength; width = pastWidth; } } if (Constraint.getState()) redraw = false; } if (Constraint.getState()){ slope = (380 - (float)constraintY)/((float)constraintX - 20); tempLength = slope * width; length = (int)tempLength; if (length > 280){ length = 280; tempWidth = length/slope; width = (int)tempWidth; } } if(length == 0 || width == 0){ length = 0; width = 0; if (Perimeter.getState() || Area.getState()){ redraw = false; length = pastLength; width = pastWidth; } } shownWidth = (float)width/40; shownLength = (float)length/40; pastLength = length; pastWidth = width; } public void setAreaAndPerimeter(){ perimeter = (2*(shownWidth + shownLength)); area = shownWidth*shownLength; if (Area.getState()){ area = (float)heldAreaLength*(float)heldAreaWidth/1600; shownLength = area/shownWidth; perimeter = 2*(shownWidth + shownLength); } } public void show(Graphics offScreenG){ if (setLineOfConstraint == 0){ if (constraintX < 60 || constraintY > 340) offScreenG.setColor(Color.red); else offScreenG.setColor(Color.black); offScreenG.drawLine(20, 380, constraintX, constraintY); setLineOfConstraint = -2; } else{ if (width < 40 || length < 40) offScreenG.setColor(Color.red); else offScreenG.setColor(Color.black); if (width != 0 && length != 0){ offScreenG.drawRect(20,380 - length,width,length); offScreenG.drawRect(21,381 - length,width-2,length-2); offScreenG.drawRect(22,382 - length,width-4,length-4); if (Constraint.getState()) offScreenG.drawLine(20, 380, constraintX, constraintY); } } if(Trace.getState()){ for(int i = 0; i < traceCount; i++) offScreenG.drawLine(xTracePoints[i],yTracePoints[i],xTracePoints[i],yTracePoints[i]); } } public boolean action(Event evt, Object arg) { if(evt.target == Perimeter){ heldPerimWidth = width; heldPerimLength = length; } if(evt.target == Area){ heldAreaWidth = width; heldAreaLength = length; } if(evt.target == Trace){ if (Trace.getState()) for(int i = 0; i < xTracePoints.length; i++){ traceCount = 0; } } if(evt.target == Constraint) setLineOfConstraint = -1; return true; } public Insets insets() { return new Insets(410, 0, 0, 0); } }