//PolygonsTest3.java add random movements import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PolygonsTest3 extends JApplet implements ActionListener, ItemListener { //ItemListener for JComboBox, JRadioButton events Polygon [] polys; //array of Polygon int numPolygons; Color [] polygonColors; //array of Color JButton drawButton; JComboBox colorsComboBox; //drop-down select box of items JRadioButton fillRadioButton, drawRadioButton; ButtonGroup fillOrDrawButtonGroup; String fillOrDraw; boolean drawButtonClicked, mouseClicked; int [] X, Y; //store mouse click coords for current polygon int numPoints; //initialize elements of an array of String: String [] colors = {"black", "white", "red", "green", "blue", "cyan", "magenta", "yellow", "gray", "pink", "orange", "darkGray"}; String drawingColorString; int animationDelay=100; // millisecond delay Timer animationTimer; // Timer drives animation JSlider undulationSlider; public void init() { polys = new Polygon[100]; //create the array polys[0] = new Polygon(); //create first element of array numPolygons = 0; polygonColors = new Color[100]; X = new int[100]; Y = new int[100]; numPoints = 0; drawButtonClicked = false; mouseClicked = false; drawingColorString = colors[0]; //black Container container = getContentPane(); container.setLayout( new FlowLayout() ); drawButton = new JButton( "Draw it now" ); container.add( drawButton ); drawButton.addActionListener( this ); //item names are in array of String. //combobox itself is like array: indexed colorsComboBox = new JComboBox(colors); colorsComboBox.setMaximumRowCount(4); //4 items shown at most container.add( colorsComboBox ); //register that this applet will handle combobox events colorsComboBox.addItemListener( this ); //create a radio button. selected fillRadioButton = new JRadioButton( "FillPolygon", true ); fillOrDraw = "fill"; container.add( fillRadioButton ); fillRadioButton.addItemListener( this ); drawRadioButton = new JRadioButton( "DrawPolygon" ); container.add( drawRadioButton ); drawRadioButton.addItemListener( this ); //define the radio buttons that make the group, only one //will be selected at a time. button group NOT added to container. fillOrDrawButtonGroup = new ButtonGroup(); //radio buttons added to button group fillOrDrawButtonGroup.add(fillRadioButton); fillOrDrawButtonGroup.add(drawRadioButton); undulationSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 20, 1 ); undulationSlider.setMajorTickSpacing( 5 ); undulationSlider.setPaintTicks( true ); undulationSlider.setPaintLabels( true ); container.add (undulationSlider ); addMouseListener( new MouseAdapter() { public void mouseClicked( MouseEvent event) { int x = event.getX(); int y = event.getY(); showStatus("("+x+","+y+")"); //append this point to the polygon object polys[numPolygons].addPoint( x, y); X[numPoints] = x; Y[numPoints] = y; numPoints++; mouseClicked = true; repaint(); //display this point } } ); startAnimation(); // begin animation } public void actionPerformed( ActionEvent event ) { if ( event.getSource() == drawButton ) { drawButtonClicked = true; polygonColors[numPolygons] = makeColor( drawingColorString ); numPoints = 0; //zero out points repaint(); } else if ( event.getSource() == animationTimer ) { for (int i=0; i