import javax.swing.*; import java.awt.*; import java.util.*; import java.text.*; public class Avg2DDistance extends JApplet { String type; int length, radius; double [][] avgDistance; double [] avgDistanceRadius; //for circle double minAvgDist, maxAvgDist; double minAvgDistCirc, maxAvgDistCirc; public void init () { double sumDist; DecimalFormat d1, d2; d1 = new DecimalFormat ("0.0"); d2 = new DecimalFormat ("0.00"); int showData=JOptionPane.NO_OPTION; type = JOptionPane.showInputDialog("[s]quare, [b]ox, [d]isk, [c]ircle?","s"); String input; if (type.equals("s") || type.equals("b")) { input = JOptionPane.showInputDialog("Length?","100"); length = Integer.parseInt(input); if (length%2 == 1) //odd length--; //step down to even if (length <= 20) //don't even ask if more than 20 showData = JOptionPane.showConfirmDialog(null,"Show the average distances?", "show the data?", JOptionPane.YES_NO_OPTION); } else { input = JOptionPane.showInputDialog("Radius?","100"); radius = Integer.parseInt(input); } if (type.equals("s")) { avgDistance = new double[length][length]; minAvgDist = 1000000; maxAvgDist = 0; for (int i=0; i maxAvgDist) maxAvgDist = avgDistance[i][j]; } } else if (type.equals("b")) { avgDistance = new double[length][length]; minAvgDist = 1000000; maxAvgDist = 0; for (int i=0; i maxAvgDist) maxAvgDist = avgDistance[i][j]; } } } else if (type.equals("d")) { //circle. take advantage of circular symmetry. avgDistanceRadius = new double[radius]; //jsut the points on the radius line minAvgDistCirc = 1000000; maxAvgDistCirc = 0; double numCirclePoints=4*radius*radius*.785; //circle is approx PI/4 area of bounding square int d; for (int i=0; i maxAvgDistCirc) maxAvgDistCirc = avgDistanceRadius[i]; } } else if (type.equals("c")) { double avgDistRoundCircle = 2*Math.PI*(radius) / 4; //one-fourth of circumference double avgDistLine = Math.sqrt(2)*(radius); //to point 1/4 of C away JOptionPane.showMessageDialog(null,"Avg distance on circle: " + d2.format(avgDistRoundCircle) + "\nAvg distance straight line: " + d2.format(avgDistLine)); //"\nCenter's avg distance: " + d2.format(avgDistanceRadius[radius-1])); //System.exit(1); } if (showData == JOptionPane.YES_OPTION) { JTextArea textarea = new JTextArea(length+2,6*length); textarea.setLineWrap( true ); textarea.setFont( new Font("Courier",Font.BOLD,14) ); JScrollPane scrollpane = new JScrollPane(textarea); for (int i=0; i