Java Reference
In-Depth Information
public SketcherFrame(String title, Sketcher theApp) {
setTitle(title); // Set the window title
this.theApp = theApp; // Save app. object reference
setJMenuBar(menuBar); // Add the menu bar to the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // Default is exit the application
// Rest of the constructor as before...
}
Directory "Sketcher 1 drawing a 3D rectangle"
You can add a field to the SketcherFrame class that stores the reference to the application object:
private Sketcher theApp;
// The application object
There are new methods in the Sketcher class that return a reference to the application window, the mod-
el, and the view, so all of these are now accessible from anywhere in the Sketcher application code where
you have a reference to the application object available.
After creating the model and view objects in the createGUI() method in the Sketcher class, you register
the view as an observer for the model to enable the model to notify the view when any changes occur. You
then add the view to the content pane of the window object, which is the main application window. Because
you add the view in the center using the BorderLayout manager for the content pane, it occupies all the
remaining space in the pane.
You now know roughly the direction in which you are heading, so let's move on down the road.
COMPONENT COORDINATE SYSTEMS
In Chapter 17, you saw how your computer screen has a coordinate system that is used to define the position
and size of a window. You also saw how you can add components to a container with their position estab-
lished by a layout manager. The coordinate system that is used by a container to position components within
it is analogous to the screen coordinate system. The origin is at the top-left corner of the container, with the
positive x -axis running horizontally from left to right, and the positive y -axis running from top to bottom.
The positions of buttons in a JWindow or a JFrame object are specified as a pair of ( x,y ) coordinates, relative
to the origin at the top-left corner of the container object on the screen. In Figure 19-2 you can see the co-
ordinate system for the Sketcher application window.
FIGURE 19-2
 
 
Search WWH ::




Custom Search