import java.awt.*; import java.applet.*; import java.util.*; import java.awt.event.*; import java.lang.String; public class box extends Applet implements Runnable { Graphics offScreenG; Image offScreenImg, backImg; boolean drawDottedLines = false, fold = false, keeprunning = false, start = false, showDimensions = false, drawoc = false, drawcf = false, drawfi = false, drawio = false; float shownHeight, shownWidth, shownLength, Volume, SurfaceArea; int X, Y, length, width, height, diff; int pointoX, pointoY, pointaX, pointaY, pointbX, pointbY, pointcX, pointcY, pointdX, pointdY, pointeX, pointeY, pointfX, pointfY, pointgX, pointgY, pointhX, pointhY, pointiX, pointiY, pointjX, pointjY, pointkX, pointkY, temppointhY, temppointeY, temppointdY, temppointgY; Font f = new Font("Times New Roman", Font.BOLD, 20); Button Reset; Button Fold; Thread runner; public void init() { backImg = getImage(getCodeBase(),"backgraph.gif"); offScreenImg = createImage(this.size().width, this.size().height); offScreenG = offScreenImg.getGraphics(); repaint(); Fold = new Button("Cut and Fold"); add(Fold); Reset = new Button("Reset"); add(Reset); } public void paint (Graphics g){ offScreenG.drawImage(backImg,0,0,this); show(offScreenG); g.setFont(f); g.drawImage(offScreenImg,0,0,this); if (showDimensions) g.setColor(Color.red); g.drawString("Length: " + roundValue(shownLength),60,40); if (showDimensions) g.setColor(Color.blue); g.drawString("Width: " + roundValue(shownWidth),60,68); if (showDimensions) g.setColor(Color.green); g.drawString("Height: " + roundValue(shownHeight), 60, 96); g.setColor(Color.black); if (showDimensions){ g.drawString("Volume: " + roundValue(Volume), 225, 40); g.drawString("Surface Area: " + roundValue(SurfaceArea), 225, 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){ down(x,y); return true; } public boolean mouseDrag(Event evt, int x, int y){ drag(x,y); return true; } public void down(int x, int y){ if (fold == false) if (x > 60 && x < 180) if (y > 140 && y < 260){ X = x; Y = y; drawDottedLines = true; up(x,y); } } public void drag(int x, int y){ if (fold == false) if (x > 60 && x < 180) if (y > 140 && y < 260){ X = x; Y = y; drawDottedLines = true; up(x,y); } } public void up(int x, int y){ System.out.println("X: " + x + "Y: " + y); diff = X - 60; height = diff; length = 300 - 2*diff; width = 240 - 2*diff; setShownDimensions(); repaint(); } public void setShownDimensions(){ shownHeight = (float)height/30; shownWidth = (float)width/30; shownLength = (float)length/30; Volume = roundValue(shownHeight)*roundValue(shownWidth)*roundValue(shownLength); SurfaceArea = 2*roundValue(shownHeight)*roundValue(shownWidth) + 2*roundValue(shownHeight)*roundValue(shownLength) + roundValue(shownWidth)*roundValue(shownLength); } public void Reset(){ backImg = getImage(getCodeBase(),"backgraph.gif"); drawDottedLines = false; fold = false; keeprunning = false; start = false; showDimensions = false; drawoc = false; drawcf = false; drawfi = false; drawio = false; shownHeight = 0; shownWidth = 0; shownLength = 0; Volume = 0; X = 0; Y = 0; length = 0; width = 0; height = 0; diff = 0; repaint(); } public void Fold(){ backImg = getImage(getCodeBase(),"background.gif"); drawDottedLines = false; fold = true; repaint(); } public void show(Graphics offScreenG){ if (fold == false) offScreenG.drawRect(60, 140, 300, 240); if (X >= 180) X = 179; if (drawDottedLines){ int j = 140; int k = 360; int l = 380; for(int i = 60; i <= (X - 5);){ //Quadrant 2 offScreenG.drawLine(i, 140 + diff, i+5, 140 + diff); offScreenG.drawLine(X, j, X, j+5); //Quadrant 1 offScreenG.drawLine(k, 140 + diff, k - 5, 140 + diff); offScreenG.drawLine(360 - diff, j, 360 - diff, j + 5); //Quadrant 3 offScreenG.drawLine(i, 380 - diff, i + 5, 380 - diff); offScreenG.drawLine(60 + diff, l, 60 + diff, l - 5); //Quadrant 4 offScreenG.drawLine(k , 380 - diff, k - 5, 380 - diff); offScreenG.drawLine(360 - diff, l, 360 - diff, l - 5); i = i+10; j = j+10; k = k-10; l = l-10; } } if (fold){ if (start == false){ pointoX = 187; pointoY = 160 + height; pointaX = pointoX + (int)(.85*Math.pow(.8*height*height, .5)); pointaY = pointoY - (int)(.85*.5*Math.pow(.8*height*height, .5)); pointbX = pointaX + (int)(.85*Math.pow(.8*length*length, .5)); pointbY = pointaY + (int)(.85*.5*Math.pow(.8*length*length, .5)); pointcX = pointbX - (int)(.85*Math.pow(.8*height*height, .5)); pointcY = pointbY + (int)(.85*.5*Math.pow(.8*height*height, .5)); pointdX = pointcX + (int)(.85*Math.pow(.8*height*height, .5)); pointdY = pointcY + (int)(.85*.5*Math.pow(.8*height*height, .5)); pointeX = pointdX - (int)(.85*Math.pow(.8*width*width, .5)); pointeY = pointdY + (int)(.85*.5*Math.pow(.8*width*width, .5)); pointfX = pointeX - (int)(.85*Math.pow(.8*height*height, .5)); pointfY = pointeY - (int)(.85*.5*Math.pow(.8*height*height, .5)); pointgX = pointfX - (int)(.85*Math.pow(.8*height*height, .5)); pointgY = pointfY + (int)(.85*.5*Math.pow(.8*height*height, .5)); pointhX = pointgX - (int)(.85*Math.pow(.8*length*length, .5)); pointhY = pointgY - (int)(.85*.5*Math.pow(.8*length*length, .5)); pointiX = pointhX + (int)(.85*Math.pow(.8*height*height, .5)); pointiY = pointhY - (int)(.85*.5*Math.pow(.8*height*height, .5)); pointjX = pointiX - (int)(.85*Math.pow(.8*height*height, .5)); pointjY = pointiY - (int)(.85*.5*Math.pow(.8*height*height, .5)); pointkX = pointjX + (int)(.85*Math.pow(.8*width*width, .5)); pointkY = pointjY - (int)(.85*.5*Math.pow(.8*width*width, .5)); } offScreenG.setColor(new Color(96,96,255)); int[] xs = {pointoX, pointaX, pointbX, pointcX}; int[] ys = {pointoY, pointaY, pointbY, pointcY}; int pts = xs.length; Polygon poly = new Polygon(xs, ys, pts); offScreenG.fillPolygon(poly); xs[0] = pointcX; ys[0] = pointcY; xs[1] = pointdX; ys[1] = pointdY; xs[2] = pointeX; ys[2] = pointeY; xs[3] = pointfX; ys[3] = pointfY; poly = new Polygon(xs, ys, pts); offScreenG.fillPolygon(poly); xs[0] = pointfX; ys[0] = pointfY; xs[1] = pointgX; ys[1] = pointgY; xs[2] = pointhX; ys[2] = pointhY; xs[3] = pointiX; ys[3] = pointiY; poly = new Polygon(xs, ys, pts); offScreenG.fillPolygon(poly); xs[0] = pointiX; ys[0] = pointiY; xs[1] = pointjX; ys[1] = pointjY; xs[2] = pointkX; ys[2] = pointkY; xs[3] = pointoX; ys[3] = pointoY; poly = new Polygon(xs, ys, pts); offScreenG.fillPolygon(poly); xs[0] = pointoX; ys[0] = pointoY; xs[1] = pointcX; ys[1] = pointcY; xs[2] = pointfX; ys[2] = pointfY; xs[3] = pointiX; ys[3] = pointiY; poly = new Polygon(xs, ys, pts); offScreenG.fillPolygon(poly); offScreenG.setColor(Color.black); offScreenG.drawLine(pointoX, pointoY, pointaX, pointaY); offScreenG.drawLine(pointaX, pointaY, pointbX, pointbY); offScreenG.drawLine(pointbX, pointbY, pointcX, pointcY); offScreenG.drawLine(pointcX, pointcY, pointdX, pointdY); offScreenG.drawLine(pointdX, pointdY, pointeX, pointeY); offScreenG.drawLine(pointeX, pointeY, pointfX, pointfY); if (showDimensions){ offScreenG.setColor(Color.green); offScreenG.drawLine(pointfX - 1, pointfY, pointgX - 1, pointgY); offScreenG.drawLine(pointfX + 1, pointfY, pointgX + 1, pointgY); } offScreenG.drawLine(pointfX, pointfY, pointgX, pointgY); offScreenG.setColor(Color.black); offScreenG.drawLine(pointgX, pointgY, pointhX, pointhY); offScreenG.drawLine(pointhX, pointhY, pointiX, pointiY); offScreenG.drawLine(pointiX, pointiY, pointjX, pointjY); offScreenG.drawLine(pointjX, pointjY, pointkX, pointkY); offScreenG.drawLine(pointkX, pointkY, pointoX, pointoY); if (start){ if (drawoc) offScreenG.drawLine(pointoX, pointoY, pointcX, pointcY); if (drawcf){ if (showDimensions){ offScreenG.setColor(Color.blue); offScreenG.drawLine(pointcX, pointcY - 1, pointfX, pointfY - 1); offScreenG.drawLine(pointcX, pointcY + 1, pointfX, pointfY + 1); } offScreenG.drawLine(pointcX, pointcY, pointfX, pointfY); offScreenG.setColor(Color.black); } if (drawfi){ if (showDimensions){ offScreenG.setColor(Color.red); offScreenG.drawLine(pointfX, pointfY - 1, pointiX, pointiY - 1); offScreenG.drawLine(pointfX, pointfY + 1, pointiX, pointiY + 1); } offScreenG.drawLine(pointfX, pointfY, pointiX, pointiY); offScreenG.setColor(Color.black); } if (drawio) offScreenG.drawLine(pointiX, pointiY, pointoX, pointoY); } if (start == false){ keeprunning = true; start = true; start(); } } } public void start() { runner = new Thread(this); runner.start(); } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void run() { while(keeprunning){ try {Thread.sleep(1000); } catch (InterruptedException e){} int b = pointbX; for(int a = pointaX; a >= pointoX;){ drawoc = true; pointaX = a; pointaY = pointoY - (int)(.85*Math.pow((height*height - (pointaX - pointoX)*(pointaX-pointoX)), .5)); pointbX = b; pointbY = pointcY - (int)(.85*Math.pow((height*height - (pointbX - pointcX)*(pointbX-pointcX)), .5)); a = a - 1; b = b - 1; try {Thread.sleep(10); } catch (InterruptedException e){} repaint(); } int ee = pointeX; for(int d = pointdX; d >= pointcX;){ drawcf = true; pointdX = d; //if (pointdY <= pointcY) pointdY = pointcY - (int)(.85*Math.pow((height*height - (pointdX - pointcX)*(pointdX-pointcX)), .5)); //else{ // temppointdY = pointcY + (int)(.85*Math.pow((height*height - (pointdX - pointcX)*(pointdX-pointcX)), .5)); // if (temppointdY < pointdY) // pointdY = temppointdY; //} pointeX = ee; //if (pointeY <= pointfY) pointeY = pointfY - (int)(.85*Math.pow((height*height - (pointeX - pointfX)*(pointeX-pointfX)), .5)); //else{ // temppointeY = pointfY + (int)(.85*Math.pow((height*height - (pointeX - pointfX)*(pointeX-pointfX)), .5)); // if (temppointeY < pointeY) // pointeY = temppointeY; //} if (pointdY <= pointcY) d = d - 1; else d = d + 1; if (pointeY <= pointfY) ee = ee - 1; else ee = ee + 1; try {Thread.sleep(10); } catch (InterruptedException e){} repaint(); } if (pointdX != pointbX || pointdY != pointbY){ pointdX = pointbX; pointdY = pointbY; repaint(); } int h = pointhX; for(int g = pointgX; g <= pointfX;){ drawfi = true; pointgX = g; //if (pointgY <= pointfY) pointgY = pointfY - (int)(.85*Math.pow((height*height - (pointfX - pointgX)*(pointfX-pointgX)), .5)); //else{ // temppointgY = pointfY + (int)(.85*Math.pow((height*height - (pointfX - pointgX)*(pointfX-pointgX)), .5)); // if (temppointgY < pointgY) // pointgY = temppointgY; //} pointhX = h; //if (pointhY <= pointiY) pointhY = pointiY - (int)(.85*Math.pow((height*height - (pointiX - pointhX)*(pointiX-pointhX)), .5)); //else{ // temppointhY = pointiY + (int)(.85*Math.pow((height*height - (pointiX - pointhX)*(pointiX-pointhX)), .5)); // if (temppointhY < pointhY) // pointhY = temppointhY; //} if (pointgY <= pointfY) g = g + 1; else g = g - 1; if (pointhY <= pointiY) h = h + 1; else h = h - 1; try {Thread.sleep(10); } catch (InterruptedException e){} repaint(); } if (pointgX != pointeX || pointgY != pointeY){ pointgX = pointeX; pointgY = pointeY; repaint(); } int k = pointkX; for(int j = pointjX; j <= pointiX;){ drawio = true; pointjX = j; pointjY = pointiY - (int)(.85*Math.pow((height*height - (pointiX - pointjX)*(pointiX-pointjX)), .5)); pointkX = k; pointkY = pointoY - (int)(.85*Math.pow((height*height - (pointoX - pointkX)*(pointoX-pointkX)), .5)); j = j + 1; k = k + 1; try {Thread.sleep(10); } catch (InterruptedException e){} repaint(); } if (pointjX != pointhX || pointjY != pointhY){ pointjX = pointhX; pointjY = pointhY; repaint(); } if (pointkX != pointaX || pointkY != pointaY){ pointkX = pointaX; pointkY = pointaY; repaint(); } showDimensions = true; System.out.println("showDimensions is true now"); repaint(); keeprunning = false; } } // End Run public boolean action(Event evt, Object arg) { if (arg.equals("Reset")){ Reset(); } if (arg.equals("Cut and Fold")) Fold(); return true; } public Insets insets() { return new Insets(100, 165, 0, 0); } }