//PolygonsTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PolygonsTest extends JApplet implements ActionListener, ItemListener { //ItemListener for JComboBox events Polygon mypoly; //polygon of vertices/points JButton drawButton; JComboBox colorsComboBox; //drop-down select box of items boolean drawButtonClicked, mouseClicked; int x, y; //mouse clicks //initailize elements of an array of String String [] colors = {"black", "white", "red", "green", "blue", "cyan", "magenta", "yellow", "gray", "pink", "orange", "darkGray"}; String drawingColorString; public void init() { mypoly = new Polygon(); //empty polygon object 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 ); addMouseListener( new MouseAdapter() { public void mouseClicked( MouseEvent event) { x = event.getX(); y = event.getY(); showStatus("("+x+","+y+")"); //append this point to the polygon object mypoly.addPoint( x, y); mouseClicked = true; repaint(); //display this point } } ); } public void actionPerformed( ActionEvent event ) { if ( event.getSource() == drawButton ) { drawButtonClicked = true; repaint(); } } // event handler for JComboBox events public void itemStateChanged( ItemEvent event ) { if ( event.getSource() == colorsComboBox ) { //an item was selected if ( event.getStateChange() == ItemEvent.SELECTED ) { //index of selected item int i = colorsComboBox.getSelectedIndex(); drawingColorString = colors[i]; } } } public void paint ( Graphics g ) { //super.paint( g ); if (drawButtonClicked) { /* g.setColor(new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256))); */ g.setColor( makeColor( drawingColorString ) ); //display the polygon. g.fillPolygon( mypoly ); drawButtonClicked = false; mypoly = new Polygon(); //create new empty polygon } else if (mouseClicked) { g.setColor(Color.BLACK); g.drawLine(x,y,x,y); mouseClicked = false; } } //is there an easier way to go from a string to a color??? public Color makeColor( String s ) { Color colorObject; if (s.equals("black")) colorObject = Color.BLACK; else if (s.equals("white")) colorObject = Color.white; else if (s.equals("red")) colorObject = Color.RED; else if (s.equals("blue")) colorObject = Color.blue; else if (s.equals("green")) colorObject = Color.green; else if (s.equals("yellow")) colorObject = Color.yellow; else if (s.equals("magenta")) colorObject = Color.magenta; else if (s.equals("cyan")) colorObject = Color.cyan; else if (s.equals("pink")) colorObject = Color.pink; else if (s.equals("orange")) colorObject = Color.orange; else if (s.equals("darkGray")) colorObject = Color.darkGray; else colorObject = Color.black; return colorObject; } }