Open In App

JavaFX | Stop Class

Last Updated : 19 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Stop class is a part of JavaFX. Stop class contains an offset and an color. Defines one element of the ramp of colors to use on a gradient. Stop class inherits Object class

Constructor of the class:

  • Stop(double o, Color c): Create a new Stop object with specified offset and color.

Commonly Used Methods:

Method Explanation
equals(Object o) Returns whether two Stop objects are equal or not.
getColor() Returns the color gradient at this offset.
getOffset() Returns the offset of the Stop.
hashCode() Returns the hashcode of the Stop object.

Java program to create stops add it to linear gradient and apply it to the circle: In this program we will create an array of Stop objects with their offset values ranging from 0 to 1. Create a LinearGradient object with specified stops. Then create a circle with specified x, y positions, and radius and add the linear gradient to it. Create a VBox and set the alignment of it. Add the circle to the vbox and add the vbox to the scene and add the scene to the stage and call the show() function to display the results.




// Java program to create stops add it to
// linear gradient and apply it to the circle
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
  
public class STOP_1 extends Application {
  
    // launch the application
    public void start(Stage stage)
    {
  
        try {
  
            // set title for the stage
            stage.setTitle("Stops");
  
            // create stops
            Stop[] stop = {new Stop(0, Color.RED), 
                           new Stop(0.33, Color.GREEN), 
                           new Stop(0.66, Color.BLUE), 
                           new Stop(1, Color.YELLOW)};
  
            // create a Linear gradient object
            LinearGradient linear_gradient = new LinearGradient(0, 0,
                             1, 0, true, CycleMethod.NO_CYCLE, stop);
  
            // create a circle
            Circle circle = new Circle(100, 100, 70);
  
            // set fill
            circle.setFill(linear_gradient);
  
            // create VBox
            VBox vbox = new VBox(circle);
  
            // ste Alignment
            vbox.setAlignment(Pos.CENTER);
  
            // create a scene
            Scene scene = new Scene(vbox, 400, 300);
  
            // set the scene
            stage.setScene(scene);
  
            stage.show();
        }
  
        catch (Exception e) {
  
            System.out.println(e.getMessage());
        }
    }
  
    // Main Method
    public static void main(String args[])
    {
  
        // launch the application
        launch(args);
    }
}


Output:

Note: The above programs might not run in an online IDE please use an offline compiler.

Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/paint/Stop.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads