Java Reference
In-Depth Information
Colors and Gradients
The following snippet from Listing 1-3 contains an example of defining a color gradient pattern, as well as
defining colors.
Stop[] stops = new Stop[]{new Stop(0, Color.web("0xAEBBCC")), new Stop(1, Color.web("0x6D84A3"))};
LinearGradient linearGradient = new LinearGradient(0, 0, 0, 1, true, CycleMethod.NO_CYCLE, stops);
Rectangle rectangle = new Rectangle(0, 0, 320, 45);
rectangle.setFill(linearGradient);
If the JavaFX API docs are handy, first take a look at the
javafx.scene.shape.Rectangle
class and notice that
it inherits a property named
fill
that is of type
javafx.scene.paint.Paint
. Looking at the JavaFX API docs for the
Paint
class, you'll see that the
Color
,
ImagePattern
,
LinearGradient
, and
RadialGradient
classes are subclasses of
Paint
. This means that the fill of any shape can be assigned a color, pattern, or gradient.
To create a
LinearGradient
, as shown in the code, you need to define at least two stops, which define the
location and color at that location. In this example, the offset value of the first stop is 0.0, and the offset value of the
second stop is 1.0. These are the values at both extremes of the unit square, the result being that the gradient will span
the entire node (in this case a
Rectangle
). The direction of the
LinearGradient
is controlled by its
startX
,
startY
,
endX
, and
endY
values, which we pass via the constructor. In this case, the direction is only vertical because the
startY
value is 0.0 and the
endY
value is 1.0, whereas the
startX
and
endX
values are both 0.0.
Note that in the Hello Earthrise example in Listing 1-1, the constant named
Color.WHITE
was used to represent
the color white. In the previous snippet, the web function of the
Color
class is used to define a color from a
hexadecimal value.
The Model Class for the Audio Configuration Example
Take a look at the source code for the
AudioConfigModel
class in Listing 1-4.
Listing 1-4.
The Source Code for
AudioConfigModel.java
package projavafx.audioconfig.model;
import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.SingleSelectionModel;
/**
* The model class that the AudioConfigMain class uses
*/
public class AudioConfigModel {
/**
* The minimum audio volume in decibels
*/
public double minDecibels = 0.0;
Search WWH ::
Custom Search