Sunday, February 5, 2012

JavaFX 2.0 Exercise: fill with LinearGradient

JavaFX 2.0: fill with LinearGradient
package javafx_exdraw;

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

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_exDraw extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

LinearGradient linearGradient_NO_CYCLE
= LinearGradientBuilder.create()
.startX(50)
.startY(50)
.endX(200)
.endY(200)
.proportional(false)
.cycleMethod(CycleMethod.NO_CYCLE)
.stops(
new Stop(0.1f, Color.rgb(255, 0, 0, 0.9)),
new Stop(1.0f, Color.rgb(0, 0, 255, 0.9)))
.build();

LinearGradient linearGradient_REFLECT
= LinearGradientBuilder.create()
.startX(275)
.startY(50)
.endX(250)
.endY(100)
.proportional(false)
.cycleMethod(CycleMethod.REFLECT)
.stops(
new Stop(0.1f, Color.rgb(255, 0, 255, 0.9)),
new Stop(1.0f, Color.rgb(0, 255, 0, 1.0)))
.build();

Rectangle rect1 = RectangleBuilder.create()
.x(50)
.y(50)
.width(100)
.height(100)
.build();
rect1.setFill(linearGradient_NO_CYCLE);

Rectangle rect2 = RectangleBuilder.create()
.x(100)
.y(100)
.width(100)
.height(100)
.fill(linearGradient_NO_CYCLE)
.build();

Rectangle roundRect = RectangleBuilder.create()
.x(250)
.y(50)
.width(100)
.height(200)
.arcWidth(30)
.arcHeight(30)
.fill(linearGradient_REFLECT)
.build();

root.getChildren().add(rect1);
root.getChildren().add(rect2);
root.getChildren().add(roundRect);

primaryStage.setScene(scene);
primaryStage.show();
}
}

No comments:

Post a Comment