/* * MakePanel_S7DQ2.java * * Created on April 21, 2007, 10:50 AM * By Joseph Shemeta for MSCSHR-JV-070308-01 * SEM7DQ2 * *2. Write a program that allows the user to select a shape from a JComboBox * and that then draws that shape 20 times with random locations and dimensions in method paint(). * The first item in the JComboBox should be the default shape that is displayed the first time paint() is called. * * MY BOOK NOTES: * Page 398 Random lines * Page 613 Shape info * Page 544 JComboBox info * Page 263 smiley face * Page 214 "shapesPanel = new Shapes(0)" idea * */ import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JComboBox; import javax.swing.JFrame; import java.awt.Color; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class MakePanel_S7DQ2 extends JFrame{ private JPanel allPanel; // panel to hold all panels private JPanel comboPanel; // panel to hold the combo box private JPanel shapesPanel; //panel to hold the shapes private JComboBox shapesComboBox;//make a combo box private String shapes[]= {"Lines", "Rectangle", "Rounded Rectangle", "3D Rectangle", "Oval"};//The shapes this makes /** * Creates a new instance of MakePanel_S7DQ2 */ public MakePanel_S7DQ2() { super("Enjoy.... (Hit the RED \"X\" to exit)"); setLayout(new BorderLayout()); ChangeHandler grabIT = new ChangeHandler(); //set up the ActionListener shapesPanel = new JPanel(); //panel to hold the shapes allPanel= new JPanel(); // set up a panel to hold all panels allPanel.setLayout( new BorderLayout() ); //set up layout comboPanel= new JPanel(); // set up a panel to hold the combo panel shapesComboBox = new JComboBox(shapes); shapesComboBox.setMaximumRowCount(5); //Show the five items shapesComboBox.addActionListener(grabIT);//Register the combo box comboPanel.add(shapesComboBox);//Add it to the panel shapesPanel = new Shapes(0);//Initial shape will be a Line allPanel.add(shapesPanel, BorderLayout.CENTER); //Add the shapes panel to the main panel allPanel.add(comboPanel, BorderLayout.SOUTH); //Add the combo panel to the main panel add(allPanel); //Add the main panel to the root panel }//END public MakePanel_S7DQ2 public class ChangeHandler implements ActionListener{ public void actionPerformed( ActionEvent event ){ for(int count = 0; count < shapesComboBox.getMaximumRowCount(); count++){ if ( count == shapesComboBox.getSelectedIndex() ) { //The count number comes from the user's choice switch(count){ case 0: remover(allPanel, shapesPanel); //Remove the 'old' shape panel shapesPanel = new Shapes(count);//Make a new shape panel per the 'count' adder(allPanel, shapesPanel); //Add it to the main panel break; case 1: remover(allPanel, shapesPanel); shapesPanel = new Shapes(count); adder(allPanel, shapesPanel); break; case 2: remover(allPanel, shapesPanel); shapesPanel = new Shapes(count); adder(allPanel, shapesPanel); break; case 3: remover(allPanel, shapesPanel); shapesPanel = new Shapes(count); adder(allPanel, shapesPanel); break; case 4: remover(allPanel, shapesPanel); shapesPanel = new Shapes(count); adder(allPanel, shapesPanel); break; }//END switch(count) }//END if ( count == shapesComboBox.getSelectedIndex() ) }//END for(int count = }//END public void actionPerformed public void remover(JPanel hostPanel, JPanel childPanel){ hostPanel.remove(childPanel); //Remove the child panel from the parent }//END test public void adder(JPanel hostPanel, JPanel childPanel){ hostPanel.add(childPanel); //add the child panel from the parent hostPanel.validate(); //update the system repaint(); //repaint the screen so we can see the new panel }//END test }//END public class ChangeHandler }//END public class MakePanel_S7DQ2