Java Reference
In-Depth Information
private Color elementColor = DEFAULT _ ELEMENT _ COLOR; // Current element color
private int elementType = DEFAULT _ ELEMENT _ TYPE; // Current element type
We can now use these to ensure that the menu items are checked appropriately when the application
starts. We also want the constants from the Constants interface available, so make the following
changes to the SketchFrame class definition:
import javax.swing.*;
import java.awt.Event;
import java.awt.Color;
public class SketchFrame extends JFrame implements Constants {
// Constructor
public SketchFrame(String title) {
setTitle(title); // Set the window title
setJMenuBar(menuBar); // Add the menu bar to the window
setDefaultCloseOperation(EXIT _ ON _ CLOSE);
// Code to create the File menu...
// Construct the Element pull down menu
elementMenu.add(lineItem = new JRadioButtonMenuItem(
"Line", elementType==LINE));
elementMenu.add(rectangleItem = new JRadioButtonMenuItem(
"Rectangle", elementType==RECTANGLE));
elementMenu.add(circleItem = new JRadioButtonMenuItem(
"Circle", elementType==CIRCLE));
elementMenu.add(curveItem = new JRadioButtonMenuItem(
"Curve", elementType==CURVE));
ButtonGroup types = new ButtonGroup();
// ...plus the rest of the code for the element types as before...
elementMenu.addSeparator();
elementMenu.add(colorMenu); // Add the sub-menu
colorMenu.add(redItem = new JCheckBoxMenuItem(
"Red", elementColor.equals(Color.RED)));
colorMenu.add(yellowItem = new JCheckBoxMenuItem(
"Yellow", elementColor.equals(Color.YELLOW)));
colorMenu.add(greenItem = new JCheckBoxMenuItem(
"Green", elementColor.equals(Color.GREEN)));
colorMenu.add(blueItem = new JCheckBoxMenuItem(
"Blue", elementColor.equals(Color.BLUE)));
// Add element color accelerators
// ... plus the rest of the constructor as before...
}
// ...plus the rest of the class and include the two new data members...
private Color elementColor = DEFAULT _ ELEMENT _ COLOR; // Current element color
private int elementType = DEFAULT _ ELEMENT _ TYPE; // Current element type
}
Search WWH ::




Custom Search