CentralControl_VBox4
  • Linear Gradient
Code :

package javafxtemplate1;
//Central Controller Class
import java.sql.SQLException;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.io.IOException;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;

public class JavaFXTemplate1 extends Application {


@Override
public void start(Stage primaryStage) throws IOException, SQLException {
CentralController controller = new CentralController();
Scene scene = new Scene(controller.getRoot(), 400, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFx VBox");
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

//----
class CentralController {
private final VBox root;
private final Label label1, label2;
//RadialGradient gradient1;
int r1 , g1 , b1;
private final Circle ball;
public CentralController() {

root = new VBox();
label1 = new Label("Central control!");
label2 = new Label("Central control!");
label1.setLayoutY(100);label1.setLayoutX(100);
Button btn = new Button("Change Color");
ball = new Circle(100, 100, 100);

root.getChildren().addAll(label1,label2,ball,btn);
root.setSpacing(8);
btn.setOnAction(new DemoEventHandler1());
}
public VBox getRoot() {
return root;
}
private class DemoEventHandler1 implements EventHandler<ActionEvent>{
@Override
public void handle(ActionEvent event) {
//Platform.exit();
Color customColor = mycolor();
String str1 = mycolor().toString();
//BackgroundFill fillGreen =
//new BackgroundFill(Color.GREEN, new CornerRadii(4), new Insets(0));
//
BackgroundFill fillrgb =
new BackgroundFill(customColor, new CornerRadii(4), new Insets(0));
//
Background bg = new Background(fillrgb);
root.setBackground(bg);
label2.setText(str1 + " red : " + r1 +" green " + g1+ " blue :" + b1);
label2.setBackground(bg);
LinearGradient gradient1 = mygrad();
ball.setFill(gradient1);

}
private Color mycolor(){
int red = (int) (Math.random()*256);
int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
Color customColor = Color.rgb(red,green,blue);
r1 = red; g1 = green;b1 = blue;
return customColor;
}
private LinearGradient mygrad(){
int red = (int) (Math.random()*256);
int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
//Color customColor = Color.rgb(red,green,blue);
LinearGradient linearGrad = new LinearGradient(0,
0, 0, 1,// central core
true,
CycleMethod.NO_CYCLE,
//new Stop(0, Color.RED),
new Stop(0,Color.rgb(red, green, blue) ),
new Stop(3, Color.RED)
);
return linearGrad;
}
}
}

 
 
 
 
Displays: