//******************************************************************** // ButtonPanel.java Author: Lewis and Loftus / Peter DePasquale // // Refinement #2 // // Represents the primary panel of controls for PaintBox. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonPanel extends JPanel { public static final int LINE = 0; public static final int OVAL = 1; public static final int RECT = 2; public static final int POLY = 3; public static final int COLOR = -1; public static final int SELECT = -2; private JRadioButton selectButton, lineButton, ovalButton; private JRadioButton rectButton, polyButton, strokeButton; private JPanel strokeIndicator; private int currentButton; private Color currentStrokeColor; //----------------------------------------------------------------- // Sets up the panel of buttons. //----------------------------------------------------------------- public ButtonPanel () { setLayout (new BoxLayout (this, BoxLayout.Y_AXIS)); currentButton = SELECT; ButtonListener listener = new ButtonListener(); ButtonGroup shapeGroup = new ButtonGroup(); selectButton = new JRadioButton (new ImageIcon("select.gif")); selectButton.setSelectedIcon (new ImageIcon("selectOn.gif")); selectButton.setToolTipText ("Select tool"); selectButton.addActionListener (listener); selectButton.setAlignmentX (Component.CENTER_ALIGNMENT); selectButton.doClick(); lineButton = new JRadioButton (new ImageIcon ("line.gif")); lineButton.setSelectedIcon (new ImageIcon("lineOn.gif")); lineButton.setToolTipText ("Line tool"); lineButton.addActionListener (listener); lineButton.setAlignmentX (Component.CENTER_ALIGNMENT); ovalButton = new JRadioButton (new ImageIcon ("oval.gif")); ovalButton.setSelectedIcon (new ImageIcon("ovalOn.gif")); ovalButton.setToolTipText ("Oval tool"); ovalButton.addActionListener (listener); ovalButton.setAlignmentX (Component.CENTER_ALIGNMENT); rectButton = new JRadioButton (new ImageIcon ("rect.gif")); rectButton.setSelectedIcon (new ImageIcon("rectOn.gif")); rectButton.setToolTipText ("Rectangle tool"); rectButton.addActionListener (listener); rectButton.setAlignmentX (Component.CENTER_ALIGNMENT); polyButton = new JRadioButton (new ImageIcon ("poly.gif")); polyButton.setSelectedIcon (new ImageIcon("polyOn.gif")); polyButton.setToolTipText ("Polyline tool"); polyButton.addActionListener (listener); polyButton.setAlignmentX (Component.CENTER_ALIGNMENT); strokeButton = new JRadioButton (new ImageIcon("color.gif")); strokeButton.setSelectedIcon (new ImageIcon("colorOn.gif")); strokeButton.setToolTipText ("Color tool"); strokeButton.addActionListener (listener); strokeButton.setAlignmentX (Component.CENTER_ALIGNMENT); shapeGroup.add (selectButton); shapeGroup.add (lineButton); shapeGroup.add (ovalButton); shapeGroup.add (rectButton); shapeGroup.add (polyButton); shapeGroup.add (strokeButton); currentStrokeColor = Color.black; strokeIndicator = new JPanel(); strokeIndicator.setMaximumSize (new Dimension(20, 10)); strokeIndicator.setAlignmentX (Component.CENTER_ALIGNMENT); strokeIndicator.setBackground (currentStrokeColor); add (selectButton); add (Box.createRigidArea (new Dimension(1,10))); add (lineButton); add (ovalButton); add (rectButton); add (polyButton); add (Box.createRigidArea (new Dimension(1,10))); add (strokeButton); add (strokeIndicator); } //----------------------------------------------------------------- // Returns the currently toggled button. //----------------------------------------------------------------- public int getButton () { return currentButton; } //----------------------------------------------------------------- // Returns the current stroke color. //----------------------------------------------------------------- public Color getStrokeColor () { return currentStrokeColor; } //***************************************************************** // An inner class to handle button events. //***************************************************************** private class ButtonListener implements ActionListener { //-------------------------------------------------------------- // Responds to action events caused by buttons. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { Object source = event.getSource(); if (source == selectButton) currentButton = SELECT; else if (source == lineButton) currentButton = LINE; else if (source == ovalButton) currentButton = OVAL; else if (source == rectButton) currentButton = RECT; else if (source == polyButton) currentButton = POLY; else if (source == strokeButton) { currentButton = COLOR; currentStrokeColor = JColorChooser.showDialog ( (Component)event.getSource(), "Set the stroke color:", Color.black); strokeIndicator.setBackground (currentStrokeColor); } } } }