Java Reference
In-Depth Information
A color instance can be constructed using the following constructor:
public
Color(
double
r,
double
g,
double
b,
double
opacity);
in which
r
,
g
, and
b
specify a color by its red, green, and blue components with values in the
range from
0.0
(darkest shade) to
1.0
(lightest shade). The
opacity
value defines the trans-
parency of a color within the range from
0.0
(completely transparent) to
1.0
(completely
opaque). This is known as the RGBA model, where RGBA stands for red, green, blue, and
alpha. The alpha value indicates the opacity. For example,
RBGA model
Color color =
new
Color(
0.25
,
0.14
,
0.333
,
0.51
);
The
Color
class is immutable. Once a
Color
object is created, its properties cannot be
changed. The
brighter()
method returns a new
Color
with a larger red, green, and blue
values and the
darker()
method returns a new
Color
with a smaller red, green, and blue
values. The
opacity
value is the same as in the original
Color
object.
You can also create a
Color
object using the static methods
color(r, g, b)
,
color(r,
g, b, opacity)
,
rgb(r, g, b)
, and
rgb(r, g, b, opacity)
.
Alternatively, you can use one of the many standard colors such as
BEIGE
,
BLACK
,
BLUE
,
BROWN
,
CYAN
,
DARKGRAY
,
GOLD
,
GRAY
,
GREEN
,
LIGHTGRAY
,
MAGENTA
,
NAVY
,
ORANGE
,
PINK
,
RED
,
SILVER
,
WHITE
, and
YELLOW
defined as constants in the
Color
class. The following
code, for instance, sets the fill color of a circle to red:
circle.setFill(Color.RED);
14.14
✓
✓
How do you create a color? What is wrong about creating a
Color
using
new
Color(1.2, 2.3, 3.5, 4)
? Which of two colors is darker,
new Color(0, 0,
0, 1)
or
new Color(1, 1, 1, 1)
? Does invoking
c.darker()
change the color
value in
c
?
Check
Point
14.15
How do you create a
Color
object with a random color?
14.16
How do you set a circle object
c
with blue fill color using the
setFill
method and
using the
setStyle
method?
Key
Point
You can set fonts for rendering the text. The
javafx.scene.text.Font
class is used to
create fonts, as shown in Figure 14.10.
A
Font
instance can be constructed using its constructors or using its static methods. A
Font
is defined by its name, weight, posture, and size. Times, Courier, and Arial are the
examples of the font names.You can obtain a list of available font family names by invoking
the static
getFamilies()
method.
List
is an interface that defines common methods for a
list.
ArrayList
is a concrete implmentation of
List
. The font postures are two constants:
FontPosture.ITALIC
and
FontPosture.REGULAR
. For example, the following state-
ments create two fonts.
Font font1 =
new
Font(
"
SansSerif
"
,
16
);
Font font2 = Font.font(
"
Times New Roman
"
, FontWeight.BOLD,
FontPosture.ITALIC,
12
);
Listing 14.8 gives a program that displays a label using the font (Times New Roman, bold,
italic, and size 20), as shown in Figure 14.11.
Search WWH ::
Custom Search