Class LinearGradient

java.lang.Object
javafx.scene.paint.LinearGradient
All Implemented Interfaces:
Paint

public final class LinearGradient extends Object implements Paint

The LinearGradient class fills a shape with a linear color gradient pattern. The user may specify two or more gradient colors, and this Paint will provide an interpolation between each color.

The application provides an array of Stops specifying how to distribute the colors along the gradient. The Stop#offset variable must be the range 0.0 to 1.0 and act like keyframes along the gradient. The offsets mark where the gradient should be exactly a particular color.

If the proportional variable is set to true then the start and end points of the gradient should be specified relative to the unit square (0.0->1.0) and will be stretched across the shape. If the proportional variable is set to false, then the start and end points should be specified in the local coordinate system of the shape and the gradient will not be stretched at all.

The two filled rectangles in the example below will render the same. The one on the left uses proportional coordinates to specify the end points of the gradient. The one on the right uses absolute coordinates. Both of them fill the specified rectangle with a horizontal gradient that varies from black to red

 // object bounding box relative (proportional = true)
 Stop[] stops = new Stop[] { new Stop(0, Color.BLACK), new Stop(1, Color.RED)};
 LinearGradient lg1 = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
 Rectangle r1 = new Rectangle(0, 0, 100, 100);
 r1.setFill(lg1);

 // user space relative (proportional: = false)
 LinearGradient lg2 = new LinearGradient(125, 0, 225, 0, false, CycleMethod.NO_CYCLE, stops);
 Rectangle r2 = new Rectangle(125, 0, 100, 100);
 r2.setFill(lg2);
 
Since:
JavaFX 2.0
  • Constructor Summary

    Constructors
    Constructor
    Description
    LinearGradient(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, List<Stop> stops)
    Creates a new instance of LinearGradient.
    LinearGradient(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, Stop... stops)
    Creates a new instance of LinearGradient.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Indicates whether some other object is "equal to" this one.
    double
     
    double
     
    Defines which of the following cycle method is applied to the LinearGradient: CycleMethod.NO_CYCLE, CycleMethod.REFLECT, or CycleMethod.REPEAT.
    final double
    Defines the X coordinate of the gradient axis end point.
    final double
    Defines the Y coordinate of the gradient axis end point.
    double
     
    final double
    Defines the X coordinate of the gradient axis start point.
    final double
    Defines the Y coordinate of the gradient axis start point.
    final List<Stop>
    A sequence of 2 or more Stop values specifying how to distribute the colors along the gradient.
    int
    Returns a hash code for this LinearGradient object.
    final boolean
     
    final boolean
    Indicates whether start and end locations are proportional or absolute.
    Returns a string representation of this LinearGradient object.
    valueOf(String value)
    Creates a linear gradient value from a string representation.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • LinearGradient

      public LinearGradient(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, Stop... stops)
      Creates a new instance of LinearGradient.
      Parameters:
      startX - the X coordinate of the gradient axis start point
      startY - the Y coordinate of the gradient axis start point
      endX - the X coordinate of the gradient axis end point
      endY - the Y coordinate of the gradient axis end point
      proportional - whether the coordinates are proportional to the shape which this gradient fills
      cycleMethod - cycle method applied to the gradient
      stops - the gradient's color specification
    • LinearGradient

      public LinearGradient(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, List<Stop> stops)
      Creates a new instance of LinearGradient.
      Parameters:
      startX - the X coordinate of the gradient axis start point
      startY - the Y coordinate of the gradient axis start point
      endX - the X coordinate of the gradient axis end point
      endY - the Y coordinate of the gradient axis end point
      proportional - whether the coordinates are proportional to the shape which this gradient fills
      cycleMethod - cycle method applied to the gradient
      stops - the gradient's color specification
  • Method Details

    • getStartX

      public final double getStartX()
      Defines the X coordinate of the gradient axis start point. If proportional is true (the default), this value specifies a point on a unit square that will be scaled to match the size of the the shape that the gradient fills.
    • getStartY

      public final double getStartY()
      Defines the Y coordinate of the gradient axis start point. If proportional is true (the default), this value specifies a point on a unit square that will be scaled to match the size of the the shape that the gradient fills.
    • getEndX

      public final double getEndX()
      Defines the X coordinate of the gradient axis end point. If proportional is true (the default), this value specifies a point on a unit square that will be scaled to match the size of the the shape that the gradient fills.
    • getEndY

      public final double getEndY()
      Defines the Y coordinate of the gradient axis end point. If proportional is true (the default), this value specifies a point on a unit square that will be scaled to match the size of the the shape that the gradient fills.
    • isProportional

      public final boolean isProportional()
      Indicates whether start and end locations are proportional or absolute. If this flag is true, the two end points are defined in a coordinate space where coordinates in the range [0..1] are scaled to map onto the bounds of the shape that the gradient fills. If this flag is false, then the coordinates are specified in the local coordinate system of the node.
    • getCycleMethod

      public final CycleMethod getCycleMethod()
      Defines which of the following cycle method is applied to the LinearGradient: CycleMethod.NO_CYCLE, CycleMethod.REFLECT, or CycleMethod.REPEAT.
    • getStops

      public final List<Stop> getStops()
      A sequence of 2 or more Stop values specifying how to distribute the colors along the gradient. These values must be in the range 0.0 to 1.0. They act like key frames along the gradient: they mark where the gradient should be exactly a particular color.

      Each stop in the sequence must have an offset that is greater than the previous stop in the sequence.

      The list is unmodifiable and will throw UnsupportedOperationException on each modification attempt.

    • isOpaque

      public final boolean isOpaque()
      Specified by:
      isOpaque in interface Paint
    • getAngle

      public double getAngle()
    • getAngleDegree

      public double getAngleDegree()
    • getLength

      public double getLength()
    • equals

      public boolean equals(Object obj)
      Indicates whether some other object is "equal to" this one.
      Overrides:
      equals in class Object
      Parameters:
      obj - the reference object with which to compare.
      Returns:
      true if this object is equal to the obj argument; false otherwise.
    • hashCode

      public int hashCode()
      Returns a hash code for this LinearGradient object.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code for this LinearGradient object.
    • toString

      public String toString()
      Returns a string representation of this LinearGradient object.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this LinearGradient object.
    • valueOf

      public static LinearGradient valueOf(String value)
      Creates a linear gradient value from a string representation.

      The format of the string representation is based on JavaFX CSS specification for linear gradient which is

       linear-gradient( [ [from <point> to <point>| [ to <side-or-corner>], ]? [ [ repeat | reflect ], ]? <color-stop>[, <color-stop>]+)
       
      where
       <side-or-corner> = [left | right] || [top | bottom]
       <point> = [ [ <length> <length> ] | [ <percentage> | <percentage> ] ]
       <color-stop> = [ <color> [ <percentage> | <length>]? ]
       

      Currently length can be only specified in px, the specification of unit can be omited. Format of color representation is the one used in Color.web(String color). The linear-gradient keyword can be omited. For additional information about the format of string representation, see the CSS Reference Guide.

      Examples:
      
       LinearGradient g
            = LinearGradient.valueOf("linear-gradient(from 0% 0% to 100% 100%, red  0% , blue 30%,  black 100%)");
       LinearGradient g
            = LinearGradient.valueOf("from 0% 0% to 100% 100%, red  0% , blue 30%,  black 100%");
       LinearGradient g
            = LinearGradient.valueOf("linear-gradient(from 0px 0px to 200px 0px, #00ff00 0%, 0xff0000 50%, 0x1122ff40 100%)");
       LinearGradient g
            = LinearGradient.valueOf("from 0px 0px to 200px 0px, #00ff00 0%, 0xff0000 50%, 0x1122ff40 100%");
       
      Parameters:
      value - the string to convert
      Returns:
      a LinearGradient object holding the value represented by the string argument.
      Throws:
      NullPointerException - if the value is null
      IllegalArgumentException - if the value cannot be parsed
      Since:
      JavaFX 2.1
      See Also: