Wednesday 4 November 2015

Filled Under:

Circle Shape Stage In JavaFx

In this code I show you how can we use javafx graphics to create stage of custom shapes.In this program I have create an stage of circle shape you can create stage of any shape by making change in this line. 

Circle dragger = new Circle(100, 100, 100,Color.CADETBLUE);

Enjoy reading.

CODE:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class mybej extends Application {
Double intx,inty;
@Override
public void start(final Stage stage) {

stage.initStyle(StageStyle.TRANSPARENT);
Text text = new Text("Transparent STAGE");
Circle dragger = new Circle(100, 100, 100,Color.CADETBLUE);
text.setFont(new Font(40));
Group g = new Group();
Button min = new Button("-");
min.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
stage.setIconified(true);
}
});
Button close = new Button("X");
close.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
stage.close();
}
});

dragger.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
intx = (me.getScreenX() - stage.getX());
inty = me.getScreenY() - stage.getY();
}
});

//when screen is dragged, translate it accordingly
dragger.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
stage.setX(me.getScreenX() - intx);
stage.setY(me.getScreenY() - inty);
}
});
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setPadding(new Insets(25,0,0,60));
hBox.setAlignment(Pos.TOP_CENTER);
hBox.getChildren().addAll(min,close);

g.getChildren().addAll(dragger,hBox);
final Scene scene = new Scene(g,300, 250);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.show();

}

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

OUTPUT:

Round shape stage in javafx

1 comments:

  1. Oh, damn! Thank you, dude), it's really helpful!
    I guess I first who post a comment, 'cause most of developers do not come to thought that window able to have different shape form.

    I had been looking for this topic and had found just a few articles, so it confirms my supposition.

    Thank you.

    ReplyDelete