Java Reference
In-Depth Information
Example 10•16: Scribble.java (continued)
toolbar.add(select);
toolbar.add(quit);
contentPane.add(toolbar, BorderLayout.NORTH);
// Create another toolbar for use as a color palette and add to
// the left side of the window.
JToolBar palette = new JToolBar();
palette.add(black);
palette.add(red);
palette.add(blue);
palette.setOrientation(SwingConstants.VERTICAL);
contentPane.add(palette, BorderLayout.WEST);
}
/** This inner class defines the "clear" action that clears the scribble */
class ClearAction extends AbstractAction {
public ClearAction() {
super("Clear"); // Specify the name of the action
}
public void actionPerformed(ActionEvent e) { scribblePane.clear(); }
}
/** This inner class defines the "quit" action to quit the program */
class QuitAction extends AbstractAction {
public QuitAction() { super("Quit"); }
public void actionPerformed(ActionEvent e) {
// Use JOptionPane to confirm that the user really wants to quit
int response =
JOptionPane.showConfirmDialog(Scribble.this, "Really Quit?");
if (response == JOptionPane.YES_OPTION) System.exit(0);
}
}
/**
* This inner class defines an Action that sets the current drawing color
* of the ScribblePane2 component. Note that actions of this type have
* icons rather than labels
**/
class ColorAction extends AbstractAction {
Color color;
public ColorAction(Color color) {
this.color = color;
putValue(Action.SMALL_ICON, new ColorIcon(color)); // specify icon
}
public void actionPerformed(ActionEvent e) {
scribblePane.setColor(color); // Set current drawing color
}
}
/**
* This inner class implements Icon to draw a solid 16x16 block of the
* specified color. Most icons are instances of ImageIcon, but since
* we're only using solid colors here, it is easier to implement this
* custom Icon type
**/
static class ColorIcon implements Icon {
Color color;
public ColorIcon(Color color) { this.color = color; }