Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
JavaFX | LineTo class
Next article icon

JavaFX | LinearGradient Class

Last Updated : 19 Sep, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

LinearGradient class is a part of JavaFX. LinearGradient class fills a shape with a linear color gradient pattern. A user may specify more than one LinearGradient pattern and the system will provide interpolation between the colors.

Constructors of the class:

  1. LinearGradient(double sX, double sY, double eX, double eY, boolean prop, CycleMethod c, List s): Creates a new Lineargradient object.
  2. LinearGradient(double sX, double sY, double eX, double eY, boolean prop, CycleMethod c, Stop… s): Creates a new LinearGradient object.

Commonly Used Methods:

Method Explanation
equals(Object o) Returns whether the LinearGradient objects are equal or not.
getCycleMethod() Returns the cycle method of the linear gradient object.
getEndX() Returns the x coordinate of end point of the linear gradient.
getEndY() Returns the y coordinate of end point of the linear gradient.
getStartX() Returns the x coordinate of start point of the linear gradient.
getStartY() Returns the y coordinate of start point of the linear gradient.
getStops() Returns the stops of linear gradient.
isOpaque() Returns whether the linear Gradient is opaque or not.
isProportional() Returns whether the linear Gradient is proportional or not.
valueOf(String v) Creates a linear gradient value from a string representation.

Below programs illustrate the use of LinearGradient Class:

  1. Java program to create a LinearGradient object and add stops to it 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. Now create a circle with specified x, y positions, and radius and add the linear gradient to it. Then 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 a LinearGradient 
    // object and add stops to it 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 Linear_Gradient_1 extends Application {
       
    // launch the application
    public void start(Stage stage)
    {
      
        try {
      
            // set title for the stage
            stage.setTitle("Linear Gradient");
      
            // create stops
            Stop[] stop = {new Stop(0, Color.RED), 
                           new Stop(0.5, Color.GREEN), 
                           new Stop(1, Color.BLUE)};
      
            // 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:

  2. Java program to create a LinearGradient object and add stops to it and set the CycleMethod to reflect and set proportional to false 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. Then create a LinearGradient object with specified stops. Set the CycleMethod to reflect and set proportional to false. Create a circle with specified x, y positions, and radius and add the linear gradient to it. Then 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 a LinearGradient object 
    // and add stops to it and set the CycleMethod to
    // reflect and set proportional to false 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 Linear_Gradient_2 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("Linear Gradient");
      
                // create stops
                Stop[] stop = {new Stop(0, Color.RED), new Stop(0.5, 
                             Color.GREEN), new Stop(1, Color.BLUE)};
      
                // create a Linear gradient object
                LinearGradient linear_gradient = new LinearGradient(0, 0, 
                                35, 0, false, CycleMethod.REFLECT, 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/LinearGradient.html



Next Article
JavaFX | LineTo class
author
andrew1234
Improve
Article Tags :
  • Java
  • JavaFX
Practice Tags :
  • Java

Similar Reads

  • JavaFX | LineTo class
    LineTo class is a part of JavaFX. LineTo class draws a line from the current position to specified x and y coordinate. LineTo class inherits PathElement class. Constructor of the class: LineTo(): Creates a new LineTo object. LineTo(double x, double y): Creates a new LineTo object with specified x, y
    4 min read
  • JavaFX | VLineTo Class
    VLineTo class is a part of JavaFX. VLineTo class creates a vertical line path from the current position to specified Y coordinate. VLineTo class inherits PathElement class. Constructor of the class: VLineTo(): Creates an empty object of VLineTo. VLineTo(double y): Creates an object of VLineTo with s
    4 min read
  • JavaFX | Slider Class
    A Slider is a Control in JavaFX which is used to display a continuous or discrete range of valid numeric choices and allows the user to interact with the control. A slider is rendered as a vertical or horizontal bar with a knob that the user can slide to indicate the desired value. A slider can also
    5 min read
  • JavaFX | Point2D Class
    Point2D class is a part of JavaFX. This class defines a 2-dimensional point in space. The Point2D class represents a 2D point by its x, y coordinates. It inherits java.lang.Object class.Constructor of the class: Point2D(double x, double y): Create a point2D object with specified x and y coordinates.
    4 min read
  • JavaFX | Rectangle2D Class
    Rectangle2D class is a part of JavaFX. Rectangle2D class creates a rectangle with the given coordinates of the upper left corner of the rectangle and the width and height or it is defined by a location (minX, minY) and dimension (width x height). Constructors of the class: Rectangle2D(double minX, d
    5 min read
  • JavaFX | MotionBlur Class
    MotionBlur is a part of JavaFX. MotionBlur class implements motion blur effect using a Gaussian convolution kernel, with a configurable radius and angle. MotionBlur class inherits Effect class. Constructors of the class: MotionBlur(): Creates a new object of MotionBlur.MotionBlur(double a, double r)
    4 min read
  • JavaFX | Stop Class
    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 Meth
    2 min read
  • Java Observable Class
    In Java, the Observable class is used to create objects that can be observed by other parts of the program. When an object of such a subclass undergoes a change, observing classes are notified. The update() method is called when an observer is notified of a change. Note: The Observable class and Obs
    8 min read
  • java.time.YearMonth Class in Java
    Java YearMonth class provides the output of the format "year-month". This class is an immutable class which means that it defines objects which, once created, never change their value. Any field which will be derived from a year and month, like quarter-of-year, are often obtained. This class does no
    4 min read
  • java.time.Period Class in Java
    The Period Class in Java class obtains a quantity or amount of time in terms of years, months and days. The time obtained is a date-based amount of time in the ISO-8601 calendar system, such as '4 years, 5 months, and 6 days. The units which are supported for a period are YEARS, MONTHS, and days. Al
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences