Java Reference
In-Depth Information
To identify the type of element, we can define constants that will act as IDs for the four types of element
we have provided for in the menu so far. This will help us with the operation of the listeners for the
menu item as well as provide a way to identify a particular type of element. Since we will accumulate
quite a number of application wide constants, it will be convenient to define them in an interface that
can be implemented by any class that refers to any of the constants. Here's the initial definition
including constants to define line, rectangle, circle and curve elements:
// Defines application wide constants
public interface Constants {
// Element type definitions
int LINE = 101;
int RECTANGLE = 102;
int CIRCLE = 103;
int CURVE = 104;
// Initial conditions
int DEFAULT _ ELEMENT _ TYPE = LINE;
}
Save this in the same directory as the rest of the Sketcher program as Constants.java . Each element
type ID is an integer constant with a unique value and we can obviously extend the variety of element
types if necessary. We have defined a constant, DEFAULT _ ELEMENT _ TYPE , representing the initial
element type to apply when the application starts. We should do the same thing for the Color submenu
and supply a constant that specifies the initial element color:
// Defines application wide constants
import java.awt.Color;
public interface Constants {
// Element type definitions
int LINE = 101;
int RECTANGLE = 102;
int CIRCLE = 103;
int CURVE = 104;
// Initial conditions
int DEFAULT _ ELEMENT _ TYPE = LINE;
Color DEFAULT _ ELEMENT _ COLOR = Color.BLUE;
}
We have defined the DEFAULT _ ELEMENT _ COLOR as type Color , so we have added an import
statement for java.awt to get the definition for the Color class. When we want to change the default
start-up color or element type, we just need to change the values of the constants in the Constants
interface. This will automatically take care of setting things up - as long as we implement the program
code appropriately.
We can add fields to the SketchFrame class to store the current element type and color, since these
are application-wide values, and are not specific to a view:
Search WWH ::




Custom Search