/* ComboDrawApp.java Seminar 7 - Graphical User Interface (GUI) POST in Sem 7 folder DQs #2 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. Be sure to use the elements specified in the DQ in order to get some experience with basic GUI elements! */ /** * * @author Darren Frendo */ import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ItemListener; import java.awt.event.ActionEvent; import java.awt.event.ItemEvent; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.JTextComponent; import javax.swing.JComboBox; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.SwingConstants; // common constants used with Swing import java.awt.Color; import java.awt.Graphics; import javax.swing.*; public class ComboDrawForm extends JFrame { private JComboBox UserSelectionComboBox; private JLabel label; private JLabel label1; private String names[] = { "Blue Rectangle", "Red Square", "Black Circle"}; private String shape[] = {names[0],names[1], names[2]}; private JButton exitJButton; private BorderLayout borderLayout; public ComboDrawForm() { super ("This Application Aims to draw a Shape 20 Times from a ComboBox"); borderLayout = new BorderLayout(); setLayout (borderLayout); //creating buttons and textfields UserSelectionComboBox = new JComboBox(names); UserSelectionComboBox.setMaximumRowCount(3); exitJButton = new JButton("Exit"); //setting handler to perform tasks based on Button pressed. ButtonHandler handler = new ButtonHandler(); UserSelectionComboBox.addItemListener( new ItemListener() //anonymous inner class { public void itemStateChanged( ItemEvent event) { if( event.getStateChange() == ItemEvent.DESELECTED) { int choice = UserSelectionComboBox.getSelectedIndex(); if (choice == 0) { DrawShapeRect newDrawShapeRect = new DrawShapeRect(); newDrawShapeRect.setBackground( Color.CYAN ); add(newDrawShapeRect, BorderLayout.CENTER); newDrawShapeRect.setVisible(false); newDrawShapeRect.setVisible(true); } else if (choice == 1) { DrawShapeSquare newDrawShapeSquare = new DrawShapeSquare(); add(newDrawShapeSquare, BorderLayout.CENTER); newDrawShapeSquare.setVisible(false); newDrawShapeSquare.setVisible(true); } else if (choice == 2) { DrawShapeCircle newDrawShapeCircle = new DrawShapeCircle(); add(newDrawShapeCircle, BorderLayout.CENTER); newDrawShapeCircle.setVisible(false); newDrawShapeCircle.setVisible(true); } } } // end method itemstatechange } //end inner class ); // end call exitJButton.addActionListener(handler); label = new JLabel(" "); label1 = new JLabel(" "); add(label,BorderLayout.NORTH); DrawShapeRect newDrawShapeRect = new DrawShapeRect(); add(newDrawShapeRect, BorderLayout.CENTER); //add exitJButton add(exitJButton,BorderLayout.EAST); add( UserSelectionComboBox, BorderLayout.SOUTH); } private class ButtonHandler implements ActionListener { //Actions to be performed based on Button Pressed. public void actionPerformed (ActionEvent event) { //Exit Button is Clicked if (event.getActionCommand() == "Exit") { //Closes window dispose (); } } //end of action performed. } } //end of Class