In this tutorial, you will learn how to create a Circular Stage in JavaFX. In this tutorial, I have explained the implementation with the help of an example project. I have described each step and use of that.
private double initX;
private double initY;
stage.setScene(scene);
stage.centerOnScreen();
stage.show();
dragger.setFill(new RadialGradient(50, 135, 0.5, 0.5, 1, true,
CycleMethod.NO_CYCLE,
new Stop[] {
rootGroup.setOnMousePressed((MouseEvent me) -> {
initY = me.getScreenY() – stage.getY();
rootGroup.setOnMouseDragged((MouseEvent me) -> {
stage.setY(me.getScreenY() – initY);
close.setOnAction((ActionEvent event) -> {
stage.close();
min.setOnAction((ActionEvent event) -> {
stage.setIconified(true);
text.setFill(Color.GREEN);
text.setEffect(new Lighting());
text.setBoundsType(TextBoundsType.VISUAL);
text.setFont(Font.font(Font.getDefault().getFamily(), 40));
vBox.setPadding(new Insets(60, 0, 0, 70));
vBox.setAlignment(Pos.TOP_RIGHT);
vBox.getChildren().addAll(text, min, close);
rootGroup.getChildren().addAll(dragger, vBox);
A method is used to create a circular stage in this video tutorial. I am sharing the code below that will help you to understand.
// variables for initial position of the stage at the beginning of drag and drop
private double initX;
private double initY;
public void createContent() {
//create stage which has set stage style transparent
final Stage stage = new Stage(StageStyle.TRANSPARENT);
//create root node of scene, i.e. group
Group rootGroup = new Group();
//
create scene with set width, height and color
create scene with set width, height and color
Scene
scene = new Scene(rootGroup, 800, 800, Color.TRANSPARENT);
scene = new Scene(rootGroup, 800, 800, Color.TRANSPARENT);
// set
scene to stage
scene to stage
stage.setScene(scene);
//
center stage on screen
center stage on screen
stage.centerOnScreen();
// show
the stage
the stage
stage.show();
//
create dragger with the desired size
create dragger with the desired size
Circle
dragger = new Circle(200,200,200);
dragger = new Circle(200,200,200);
// fill
the dragger with radial background
the dragger with radial background
dragger.setFill(new RadialGradient(50, 135, 0.5, 0.5, 1, true,
CycleMethod.NO_CYCLE,
new Stop[] {
new
Stop(0, Color.RED),
Stop(0, Color.RED),
new
Stop(1, Color.WHITE)
Stop(1, Color.WHITE)
}));
// when the mouse button is pressed, save the initial position of the screen
rootGroup.setOnMousePressed((MouseEvent me) -> {
initX = me.getScreenX() – stage.getX();
initY = me.getScreenY() – stage.getY();
});
// when the screen is dragged, translate it accordingly
rootGroup.setOnMouseDragged((MouseEvent me) -> {
stage.setX(me.getScreenX() – initX);
stage.setY(me.getScreenY() – initY);
});
//
create a button for the closing application
create a button for the closing application
Button
close = new Button(“Close me”);
close = new Button(“Close me”);
close.setOnAction((ActionEvent event) -> {
stage.close();
});
//
create a button for minimizing stage
create a button for minimizing stage
Button
min = new Button(“Minimize me”);
min = new Button(“Minimize me”);
min.setOnAction((ActionEvent event) -> {
stage.setIconified(true);
});
//
CREATE SIMPLE TEXT NODE
CREATE SIMPLE TEXT NODE
Text
text = new Text(“Cool IT Help”); //20, 110,
text = new Text(“Cool IT Help”); //20, 110,
text.setFill(Color.GREEN);
text.setEffect(new Lighting());
text.setBoundsType(TextBoundsType.VISUAL);
text.setFont(Font.font(Font.getDefault().getFamily(), 40));
// USE
A LAYOUT VBOX FOR EASIER POSITIONING OF VISUAL NODES ON SCENE
A LAYOUT VBOX FOR EASIER POSITIONING OF VISUAL NODES ON SCENE
VBox
vBox = new VBox();
vBox = new VBox();
vBox.setSpacing(10);
vBox.setPadding(new Insets(60, 0, 0, 70));
vBox.setAlignment(Pos.TOP_RIGHT);
vBox.getChildren().addAll(text, min, close);
// add
all nodes to the main root group
all nodes to the main root group
rootGroup.getChildren().addAll(dragger, vBox);
}
For complete understanding please watch this video tutorial.