Java Reference
In-Depth Information
private TypeAction lineAction, rectangleAction, circleAction,
curveAction, textAction;
You will need an icon in the Images directory that is a subdirectory to the Sketcher directory if you
want to create a toolbar button for text. I just created an icon with 'T' on it, , but you can create
something fancier if you have a mind to. The file containing the icon should have the name Text.gif ,
because that's what the TypeAction object will assume.
To add the menu item, you need to add a statement to the SketchFrame constructor following the
statements that create the other items in the element menu:
// Construct the Element pull-down menu
// Code to add the other Element menu items as before...
addMenuItem(elementMenu, textAction = new TypeAction("Text", TEXT, "Draw text"));
This assumes we have defined the value of TEXT in the Constants interface, so add that and a default
font for use with text elements now:
public interface Constants {
// Element type definitions
int LINE = 101;
int RECTANGLE = 102;
int CIRCLE = 103;
int CURVE = 104;
int TEXT = 105;
// Initial conditions
int DEFAULT _ ELEMENT _ TYPE = LINE;
Color DEFAULT _ ELEMENT _ COLOR = Color.BLUE;
Font DEFAULT _ FONT = new Font("Times New Roman",Font.PLAIN, 12);
}
If you don't have the Times New Roman font on your machine, choose a font that you do have. We will
need a data member in the application window object to hold a reference to the current font. It will start
out as the default font, but we will be adding the means to alter this later. Add the following data
member after the others in SketchFrame :
private Color elementColor = DEFAULT _ ELEMENT _ COLOR; // Current element color
private int elementType = DEFAULT _ ELEMENT _ TYPE; // Current element type
private Font font = DEFAULT _ FONT; // Current font
Of course, we also need a method to retrieve it from the application window object:
public Font getCurrentFont() {
return font;
}
Now the view will be able to get the current font when necessary, via the application object.
Search WWH ::




Custom Search