Java Reference
In-Depth Information
}
Save this as SketcherConstants.java in the Constants directory you have created. Each element type
ID in the class is an integer constant with a unique value, and you can obviously extend the variety of ele-
ment types if necessary. Of course, you could also have defined the element IDs as enumeration constants,
but because you will be adding other types of constants to Sketcher later, you would have more than one
class involved in the definition of constants for Sketcher.
You have defined the DEFAULT_ELEMENT_TYPE constant to specify the initial element type to apply when
the Sketcher application starts. You could do the same thing for the Color submenu and supply a constant
that specifies the default initial element color:
// Defines application wide constants
package Constants;
import java.awt.Color;
public class SketcherConstants {
// Element type definitions
public final static int LINE = 101;
public final static int RECTANGLE = 102;
public final static int CIRCLE = 103;
public final static int CURVE = 104;
// Initial conditions
public final static int DEFAULT_ELEMENT_TYPE = LINE;
public final static Color DEFAULT_ELEMENT_COLOR = Color.BLUE;
}
Directory "Sketcher 4 with element type listeners"
You have defined the DEFAULT_ELEMENT_COLOR field as type Color , so you have added an import state-
ment for the Color class name. When you want to change the default startup color or element type, you just
need to change the values of the constants in the SketcherConstants class. This automatically takes care
of setting things up — as long as you implement the program code appropriately.
You can add fields to the SketcherFrame class to store the current element type and color because these
are application-wide values and are not specific to a view:
private Color elementColor = DEFAULT_ELEMENT_COLOR;
// Current element color
private int elementType = DEFAULT_ELEMENT_TYPE;
// Current element type
You can now use these to ensure that the menu items are checked appropriately when the application
starts. Of course, for the class to compile, you also want the names of the constants from the SketcherCon-
stants class imported into the SketcherFrame class, so make the following changes to the SketcherFrame
class definition:
Search WWH ::




Custom Search