Tuesday 28 July 2015

Filled Under:

Tutorial On Linear Gradient In JavaFx

Linear Gradient can be used to fill any shape with a define gradient pattern the user can create gradient pattern using two or more colour as you can see in this program gradient of two different styles is filled in rectangles.The general constructor of linear gradient is
LinearGradient(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, Stop)

PARAMETERS
DESCRIPTION
startX
This parameter is used to define the starting X-Coordinate of gradient axis.
startY 
This parameter is used to define the starting  Y-Coordinate of gradient axis.
endX
This parameter is used to define the ending X-Coordinate of gradient axis.
endY
This parameter is used to define the ending Y-Coordinate of gradient axis.
proportional
This parameter is used to define whether the coordinates are proportional to the shape or not.
cycleMethod
Used to define the cycle method of gradient such gradient can be repeated   and reflective.
Examples: CycleMethod.NO_CYCLE, CycleMethod.REFLECT, or CycleMethod.REPEAT4.
stops
Defines the  two or more Stop values specifying how to distribute the colors along the gradient. These values must be in the range 0 to 1 in float .They are basically colors which user want to be used in gradient .
.

LINEAR GRADIENT IN JAVAFX:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.Stop;

public class fxmain extends Application
 {
public void start(Stage primaryStage)
{
primaryStage.setTitle("Linear Gradient");
Group root = new Group();
Rectangle rect = new Rectangle();
//Changing The Position Of Rectangle
rect.setX(10);
rect.setY(10);
//Changing Size Of Rectangle
rect.setWidth(200);
rect.setHeight(200);
LinearGradient linearGrad = new LinearGradient(40, 40, 55, 55, false, CycleMethod.REPEAT, new Stop(0f, Color.RED),new Stop(1f, Color.BLACK));
//Filling gradient in Rectangle
rect.setFill(linearGrad);
root.getChildren().add(rect);
Rectangle rect1 = new Rectangle();
rect1.setX(300);
rect1.setY(10);
rect1.setWidth(200);
rect1.setHeight(200);
LinearGradient grad = new LinearGradient(0, 0, 30, 0, false,
CycleMethod.REFLECT,
new Stop(0f, Color.RED),
new Stop(1f, Color.BLUE));
rect1.setFill(grad);
root.getChildren().add(rect1);
Rectangle rect2 = new Rectangle();
rect2.setX(560);
rect2.setY(10);
rect2.setWidth(200);
rect2.setHeight(200);
Scene scene = new Scene(root, 700, 220,Color.WHITE);
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {

Application.launch(args);
}
}

OUTPUT:





HOW TO ADD GRADIENT IN A SCENE:


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.stage.Stage;

public class javafx extends Application 
{
@Override


public void start(Stage primaryStage)
{

StackPane root = new StackPane(); 
primaryStage.setTitle("Gradient In Scene");
LinearGradient grad1 = new LinearGradient(0, 0, 2, 0, true, 
CycleMethod.REPEAT, 
new Stop(0f, Color.GREEN),
new Stop(.5f, Color.STEELBLUE),new Stop(.3f,Color.AQUAMARINE));
Scene scene = new Scene(root, 300, 250,grad1);
primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String[] args) {

launch(args);

}

}







0 comments:

Post a Comment