import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class Shape3DTest extends JApplet { private HashSet shapes; private JTextField numShapesField; private int numShapes; public void init() { Container container = getContentPane(); container.setLayout( new FlowLayout() ); shapes = new HashSet(); JLabel numShapesLabel = new JLabel( "Number of shapes" ); container.add( numShapesLabel ); numShapesField = new JTextField( "", 4 ); container.add( numShapesField ); numShapesField.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { try { numShapes = Integer.parseInt( numShapesField.getText() ); } catch (NumberFormatException e) { numShapes = 0; } if ( numShapes > 0 ) { for (int i=1; i<=numShapes; i++) if ( Math.random() < .5 ) shapes.add( new Cone() ); else shapes.add( new Sphere() ); repaint(); } } } ); } public void start() { numShapesField.requestFocusInWindow(); } public void paint( Graphics g ) { super.paint( g ); Iterator iter = shapes.iterator(); while ( iter.hasNext() ) ((Shape3D)iter.next()).draw( g ); //overridden draw of Shape3D heirarchy shapes.clear(); //throw them all away } }