Frame Lab


The Frame Lab animated illustration lets you practice setting colors, drawing shapes, setting fonts, and drawing text. To run the program, type java visual.FrameLab. The initial display is shown in Figure 14.17.

click to expand
Figure 14.17: Initial Frame Lab display

The animated illustration simulates a subclass of Frame, with a paint() method that you can configure. You can enter any class name you like in the first text field. (Notice that the lines that declare and call the constructor change as you change the name.) If you want your constructor to set the background color, make sure the check box in the setBackground line of the constructor is checked, and select a color from the pull-down menu. The constructor sets the frame's size to 450 by 450, but you can enter any number you want.

The body of the paint() method consists of ten lines, each controlled by its own pull-down menu that lets you select a method to call on the graphics object. You can set the color or the font, or you can call any of the drawing methods that were presented in this chapter. You can also select a comment (/********/), which indicates that you don't want the line to do anything. No matter what you select, the line will present you with controls for entering the arguments of the method you've chosen.

When you're ready to simulate execution of the code you've set up, click either Run or Run Lightspeed to view either an animation or an instant result. If you want to run again, click Reset, adjust the controls, and again click either Run or Run Lightspeed. Figure 14.18 shows one possible Frame Lab configuration.

click to expand
Figure 14.18: Frame Lab with custom configuration

The resulting frame is shown in Figure 14.19.

click to expand
Figure 14.19: The result of Figure 14.18

Try configuring Frame Lab to paint the following:

  • A line of text, in any font you like, with the baseline visible.

  • A line of text centered in a filled oval.

  • Three concentric circles.

Now draw anything you like. If you create any interesting results that you would like to share, please email them to us at groundupjava@sgsware.com. In the next edition of this book, Frame Lab will include a gallery of the best pictures submitted, along with the artists' names.

Take a look at the main() method in Frame Lab. So far in this chapter, all your main() methods have been two lines long, like this:

public static void main(String[] args) {   FontAndBaseline fabl = new FontAndBaseline();   fabl.setVisible(true); }

In Frame Lab, vertical space is a valuable commodity. That's why the open curly brackets appear at the ends of lines, rather than on their own lines. Frame Lab's main() is only one line long:

(new FancyFrame()).setVisible(true);

This is just a shorter equivalent of the following:

FancyFrame ff = new FancyFrame(); ff.setVisible(true);

In the single-line version, there is no reference to the instance of FancyFrame that gets constructed. If you want to make another call on the instance after setVisible(), you're out of luck. The instance is anonymous. "Anonymous" means "without a name," and a reference is like an object's name. The single-line version of the constructor is considered better style, because there is no need for the reference except in the setVisible() call. For the rest of this book, the Frame subclass instances will be anonymous.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net