import javax.swing.*; import java.awt.*; import java.awt.event.*; /**** * * Class ToolBar is a simple example of a the style of tool bar that you need * for program 6. It uses a JToolBar as the outermost container. The * invividual tools are JToggleButtons, which have an on/off state. The * buttons are put in a BottonGroup, which provides the one-on-at-a-time * functionality that we want. That is, when one of the toggle buttons in the * group is turned on, the others are all turned off. *

* Vis a vis program 6, the important thing that this example illustrates is * how to have a tool bar that allows the user to click one of its tools, and * have the be able to program determine which is the currently selected tool. *

*/ public class DrawingToolBar extends JPanel { /** The tool bar, with a vertical alignment. */ JToolBar bar = new JToolBar(JToolBar.VERTICAL); /** The button group, to provide one-on-at-at-time behavior */ ButtonGroup group = new ButtonGroup(); /** First toggle button */ JToggleButton selectButton = new JToggleButton( createImageIcon("images/select.jpg")); /** Second toggle button */ JToggleButton lineButton = new JToggleButton( createImageIcon("images/line.jpg")); /** Third toggle button */ JToggleButton rectangleButton = new JToggleButton( createImageIcon("images/rectangle.jpg")); /** Fourth toggle button */ JToggleButton ellipseButton = new JToggleButton( createImageIcon("images/ellipse.jpg")); /** Fifth toggle button */ JToggleButton polygonButton = new JToggleButton( createImageIcon("images/polygon.jpg")); /** Sizth toggle button */ JToggleButton textButton = new JToggleButton( createImageIcon("images/text.jpg")); /** Clickable button that reports which tool button is currently selected */ JButton tButton = new JButton("Selected Tool"); DrawingCanvas canvas; /** * Construct the toolbar and lay out its components. */ public DrawingToolBar(DrawingCanvas c) { canvas = c; /* * Build the buttons with text labels. In program 6, the labels need * to be image icons of the different shapes to be drawn. */ selectButton.setActionCommand("Select"); selectButton.setAlignmentX(Box.CENTER_ALIGNMENT); lineButton.setActionCommand("Line"); lineButton.setAlignmentX(Box.CENTER_ALIGNMENT); rectangleButton.setActionCommand("Rectangle"); rectangleButton.setAlignmentX(Box.CENTER_ALIGNMENT); ellipseButton.setActionCommand("Ellipse"); ellipseButton.setAlignmentX(Box.CENTER_ALIGNMENT); polygonButton.setActionCommand("Polygon"); polygonButton.setAlignmentX(Box.CENTER_ALIGNMENT); textButton.setActionCommand("Text"); textButton.setAlignmentX(Box.CENTER_ALIGNMENT); /* * Add the buttons to the toolbar. */ bar.add(selectButton); bar.add(lineButton); bar.add(rectangleButton); bar.add(ellipseButton); bar.add(polygonButton); bar.add(textButton); /* * Add the action listeners to the buttons. */ selectButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.setListener("Select"); } } ); lineButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.setListener("Line"); } } ); rectangleButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.setListener("Rectangle"); } } ); ellipseButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.setListener("Ellipse"); } } ); polygonButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.setListener("Polygon"); } } ); textButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.setListener("Text"); } } ); /* * Add buttons in the group. */ group.add(selectButton); group.add(lineButton); group.add(rectangleButton); group.add(ellipseButton); group.add(polygonButton); group.add(textButton); /* * Do some window dressing. */ bar.setBackground(Color.WHITE); bar.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); add(bar); } /** * Force the Text tool to be selected. This fixes a curious problem that * has the Select Tool becoming current after the text entering JTextField * is removed from the canvas. */ public void setTextTool() { requestFocus(); repaint(); // p("selected tool = " + group.getSelection().getActionCommand()); // group.setSelected(textButton.getModel(), true); // transferFocus(); // group.clearSelection(); } /** * Return an ImageIcon, or null if the path is invalid. This code was take * directly from the Sun tutorial on JButtons. */ private ImageIcon createImageIcon(String path) { java.net.URL imgURL = Drawing.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } void p(String s) {System.out.println(s);} }