Java Reference
In-Depth Information
and italic will be set. This prompts Java to make the text both bold and italic. Of course,
your program should never rely on knowing the value for these constants because they can
change in future Java implementations. The third parameter of the constructor of the
Font
class is the height of the font in pixels. This is also sometimes referred to as the
point size
of the font.
The expression
g2.setColor(Color.RED)
changes the color of the brush to red. Any
drawings that will be done after the command will be in red until the color of the brush
is changed again. The
Color
class defines the following constants:
BLACK
,
BLUE
,
CYAN
,
DARK GRAY
,
GRAY
,
GREEN
,
LIGHT GRAY
,
MAGENTA
,
ORANGE
,
PINK
,
RED
,
WHITE
,and
YELLOW
.
Java also allows us to use a custom color. Note that the color of each pixel on the screen
has three components: red, green, and blue. Any imaginable color can be created as a com-
bination of these three colors. Java allows the specification of true colors, which means that
a value between 0 and 255 can be assigned for the strength of each of the three primitive
colors. This results in 2
8
∗
3
=16
,
777
,
216 colors. Since the human eye is popularly believed
to be capable of discriminating among as many as ten million colors, this seems like a good
match. Here is an example of how the color of the brush can be changed to a new color.
g2 . setColor (
new
Color(20,30,40)) ;
The value 20 defines the strength of red, the value 30 defines the strength of green, and
the value 40 defines the strength of blue. Since red, green, and blue are the primary colors,
this approach is commonly referred to as the RGB model. Note that if the current display
settings of the monitor do not support true colors, then the closest available color will be
selected.
Next, consider the last two lines of the
showMessage
method.
Rectangle2D textBox = myFont. getStringBounds(s ,
g2 . getFontRenderContext () ) ;
g2. drawString(s , (
int
) (getWidth() / 2
−
textBox . getWidth() / 2) ,
(
int
) (getHeight () / 2
−
textBox . getHeight ())) ;
The first line returns a rectangle (i.e., an object of type
Rectangle2D
) that corresponds
to the virtual rectangle that will surround the string
s
when displayed. This line will give
us information about the size of the string box. The
getStringBounds
method will take
into account the current font and point size that are associated with the
myFont
object.
The expression
textBox.getWidth()
will return the width of the rectangle in pixels, while
textBox.getHeight()
will return the height of the rectangle in pixels. Conversely, the
getWidth
and
getHeight
methods of the
JPanel
class will return the width and height of
the current drawing panel, respectively. Note that the size of the panel is smaller than the
size of the window. For example, the window has border and title bar, which are not part of
the panel. Once we know the size of the current panel and the size of the virtual rectangle
that surrounds the string, we can display the string in the center of the panel. The integer
conversion in the last line is needed because the coordinates of the
textBox
rectangle are
doubles. Note that the
drawString
method takes as input the string and the coordinates
of where the bottom left corner of the text should be positioned.
Let us next examine the constructor of the
BreakoutPanel
class. It simply creates a
grid of bricks with random colors. The
getRandomColor
method chooses a random number
between 0 and 255 for the intensity of red, blue, and green, respectively. If the chosen color
happens to coincide with the background color or the panel, then the red color is chosen. This
prevents the creation of “invisible” bricks that have the same color as the panel background
color. The
getBackgound
method in the
JPanel
class returns the background color of the
panel. Note that the color of the bricks should be set outside the
paintComponent
method.
If the random color is chosen inside the
paintComponent
method, then the color of the