Java Reference
In-Depth Information
or from another object. The
Canvas
class should not need any modification. It is probably best
to treat it as a library class: open the editor and switch to the documentation view. This displays
the class's interface with the
javadoc
documentation.
The
Pen
class provides a pen object that can be used to produce drawings on the canvas by
moving the pen across the screen. The pen itself is invisible, but it will draw a line when moved
on the canvas.
The
DrawDemo
class provides a few small examples of how to use a pen object to produce a
drawing on screen.
The best starting point for understanding and experimenting with this project is the
DrawDemo
class.
Exercise 5.50
Create a
DrawDemo
object and experiment with its various methods. Read
the
DrawDemo
source code and describe (in writing) how each method works.
Exercise 5.51
Create a
Pen
object interactively using its default constructor (the construc-
tor without parameters). Experiment with its methods. While you do this, make sure to have
a window open showing you the documentation of the
Pen
class (either the editor window in
Documentation
view or a web-browser window showing the project documentation). Refer to
the documentation to be certain what each method does.
Exercise 5.52
Interactively create an instance of class
Canvas
and try some of its meth-
ods. Again, refer to the class's documentation while you do this.
Some of the methods in the classes
Pen
and
Canvas
refer to parameters of type
Color
. This
type is defined in class
Color
in the
java.awt
package (thus, its fully qualified name is
java.
awt.Color
). The
Color
class defines some color constants, which we can refer to as follows:
Color.RED
Using these constants requires the
Color
class to be imported in the using class.
Exercise 5.53
Find some uses of the color constants in the code of class
DrawDemo
.
Exercise 5.54
Write down four more color constants that are available in the
Color
class.
Refer to the class's documentation to find out what they are.
When calling methods interactively that expect parameters of the
Color
class, we have to refer
to the class slightly differently. Because the interactive dialog has no import statement (and thus
the
Color
class is not automatically known), we have to write the fully qualified class name to
refer to the class (Figure 5.5). This enables the Java system to find the class without using an
import statement.
Search WWH ::
Custom Search