Java Reference
In-Depth Information
boolean contains(int x, int y) : Returns true if the current Rectangle object encloses the
point ( x , y ) and returns false otherwise.
These methods are useful when you are dealing with the contents of the client area of the application
window. You work with points and rectangles relating to the items drawn in the window. You meet other
classes that define rectangles when you start drawing in a window in Chapter 19.
Visual Characteristics of a Component
Two things determine the visual appearance of a Swing component: the representation of the component
created by the code in the component class that is executed when the component is displayed, and whatever
you draw on the component. You draw on a Component object by implementing its paint() method. You
used this method in Chapter 1 to output the text for an applet. The paint() method is called automatically
when the component needs to be drawn.
The need to redraw a component can arise quite often for a variety of reasons — for example, your
program may request that the area that the component occupies should be redrawn, or the user may resize
the window containing the component. Your implementation of this method must include code to generate
whatever you want drawn within the Component object. Note that the component itself — the JButton or
JFrame or whatever — is drawn for you. You only need to override the paint() method for anything addi-
tional that you want to draw on it. You override the paint() method in Chapter 19 to draw in a window, so
I'm leaving further discussion of it until then.
You can alter some aspects of the appearance of the basic component by calling methods for the object.
The following methods have an effect on the appearance of a Component object:
void setBackground(Color aColor) : Sets the background color to aColor . The background
color is the color used for the basic component. You must use a different color from the back-
ground when you draw if you want what you have drawn to be visible.
Color getBackground() : Returns the component's current background color. This is useful when
you want to verify that the color you plan to use for drawing on a component is different from the
background.
void setForeground(Color bColor) : Sets the foreground color to bColor . The foreground color
is the color used by default for anything you draw on the component.
Color getForeground() : Returns the component's current foreground color.
void setCursor(Cursor aCursor) : Sets the cursor for the component to aCursor . This determ-
ines the appearance of the cursor within the area occupied by the component.
void setFont(Font aFont) : Sets the font for the Component object. This is the default font when
you draw text on the component.
Font getFont() : Returns the current font for the component.
To be able to make use of these properly, you need to understand what Color objects are, and you need
to know how to create Cursor and Font objects.
Defining Colors
A screen color is represented by a java.awt.Color object. You define a color value as a combination of
the three primary colors: red, green, and blue. They are usually expressed in that sequence, and are often
referred to as RGB values . There are other ways of specifying colors in Java, but I discuss only RGB. You
Search WWH ::




Custom Search