//ConeTest.java //CMIS 241 driver for testing Cone class. //uses ArrayList to store Cones //ActionListeners are anonymous inner classes. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class ConeTest { private JFrame shapesFrame; private Container container; private JLabel numShapesLabel; private JTextField numShapesField; private JLabel maxShapesSizeLabel; private JTextField maxShapesSizeField; private JButton createShapesButton; private JTextArea outputTextArea; private JButton sortAreaButton; private JButton sortVolumeButton; private JLabel radiusLabel; private JTextField radiusField; private JLabel heightLabel; private JTextField heightField; private JButton addButton; private JLabel fromIndexLabel; private JTextField fromIndexField; private JLabel toIndexLabel; private JTextField toIndexField; private JButton copyCtorButton; private JButton equalsButton; private JButton areaCompareToButton; private JButton volumeCompareToButton; private JTextField answerField; private JButton copyConstructorButton; private ArrayList shapes; public static void main(String [] args) { ConeTest shapesFrame = new ConeTest(); } //constructor public ConeTest() { shapesFrame = new JFrame(); shapesFrame.setSize(900,700); //assumes everyone is running at least SVGA 1024x768 shapesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); container = shapesFrame.getContentPane(); container.setLayout( new FlowLayout() ); shapes = new ArrayList(); numShapesLabel = new JLabel( "Number of shapes" ); container.add(numShapesLabel); numShapesField = new JTextField("",4); container.add(numShapesField); numShapesField.addActionListener( //anonymous inner class: new ActionListener() { //executed when Enter is pressed in the numShapesField: public void actionPerformed( ActionEvent event ) { createShapes(); displayShapes(); } } ); maxShapesSizeLabel = new JLabel( "Max. size" ); container.add(maxShapesSizeLabel); maxShapesSizeField = new JTextField("10",4); container.add(maxShapesSizeField); createShapesButton = new JButton("Create new shapes"); container.add(createShapesButton); createShapesButton.addActionListener( new ActionListener() { //executed when createShapesButton pressed public void actionPerformed( ActionEvent event ) { createShapes(); displayShapes(); } } ); outputTextArea = new JTextArea(20,60); container.add( new JScrollPane(outputTextArea)); numShapesField.requestFocusInWindow(); // ?? sortAreaButton = new JButton("Sort by area"); container.add(sortAreaButton); sortAreaButton.addActionListener( new ActionListener() { //executed when sortAreaButton pressed public void actionPerformed( ActionEvent event ) { if ( shapes.size() > 0 ) { Collections.sort(shapes,new ConeAreaComparator()); displayShapes(); } } } ); sortVolumeButton = new JButton("Sort by volume"); container.add(sortVolumeButton); sortVolumeButton.addActionListener( new ActionListener() { //executed when sortVolumeButton pressed public void actionPerformed( ActionEvent event ) { if ( shapes.size() > 0 ) { Collections.sort(shapes,new ConeVolumeComparator()); displayShapes(); } } } ); radiusLabel = new JLabel( "Radius" ); container.add(radiusLabel); radiusField = new JTextField("",3); container.add(radiusField); heightLabel = new JLabel( "Height" ); container.add(heightLabel); heightField = new JTextField("",3); container.add(heightField); addButton = new JButton("Add"); container.add(addButton); addButton.addActionListener( new ActionListener() { //executed when addButton pressed public void actionPerformed( ActionEvent event ) { int radius, height; try { radius = Integer.parseInt( radiusField.getText() ); height = Integer.parseInt( heightField.getText() ); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "radius and height must be numbers"); return; } if (radius>0 && height>0) { //add a Cone of radius and height to ArrayList. //need temporary reference because will add to arraylist and append Cone cone = new Cone(radius,height); shapes.add(cone); outputTextArea.append( String.format("%-30s",cone.toString())+ "\tA="+cone.getArea()+"\tV="+cone.getVolume()+"\n"); } else JOptionPane.showMessageDialog(null,"radius and height must be > 0"); } } ); copyConstructorButton = new JButton("Copy constructor"); container.add(copyConstructorButton); copyConstructorButton.addActionListener( new ActionListener() { //executed when copyConstructorButton pressed public void actionPerformed( ActionEvent event ) { int fromIndex; try { fromIndex = Integer.parseInt( fromIndexField.getText() ); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "From index must be a number"); return; //quit this method call } if (fromIndex>=0 && fromIndex=0 && maxSize>0) { shapes.clear(); //remove previous shapes for (int i=1; i<=numShapes; i++) //add a Cone with random radius and height to ArrayList shapes.add(new Cone((int)(Math.random()*maxSize)+1, (int)(Math.random()*maxSize)+1)); displayShapes(); } else JOptionPane.showMessageDialog(null, "Number of shapes and Max size must be positive numbers"); } //pressing equalsButton, areaCompareToButton, volumeCompareToButton private void processButton (JButton whichButton) { int index1, index2; try { index1 = Integer.parseInt( fromIndexField.getText() ); index2 = Integer.parseInt( toIndexField.getText() ); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "From index and To index must be numbers"); return; } if (index1>=0 && index2>=0 && index1