简体   繁体   中英

creating undecorated stage in javafx 2.0

I am trying to create a custom stage in javafx 2.0. I want that my stage drops shadow on the screen as dropped by other windows... I tried with following code :

public class ChatWindow {
final private Stage stage = new Stage(StageStyle.UNDECORATED);
private Scene scene;
private Group rg;
private Text t = new Text();
private double initx = 0, inity = 0;

public ChatWindow() {

    rg = new Group();
    scene = new Scene(rg, 320, 240);
    //scene.setFill(null);
    scene.setFill(new Color(0, 0, 0, 0));
    stage.setScene(scene);
    stage.show();
    setupStage();
}

private void setupStage() {
    Rectangle r = new Rectangle(5, 5, stage.getWidth() - 10, stage.getHeight() - 10);
    r.setFill(Color.STEELBLUE);
    r.setEffect(new DropShadow());
    rg.setOnMousePressed(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent me) {
            initx = me.getScreenX() - stage.getX();// - me.getSceneX(); 
            inity = me.getScreenY() - stage.getY();
        }
    });
    rg.setOnMouseDragged(new EventHandler<MouseEvent>() {

        public void handle(MouseEvent me) {
            stage.setX(me.getScreenX() - initx);
            stage.setY(me.getScreenY() - inity);
        }
    });
    rg.getChildren().add(r);
    rg.getChildren().add(t);
}

public void setVisible() {
    stage.show();
}
}

I can see the shadow fall, but actually their is a white background on which its falling. So, its useless, as on colored screen the defect will be visible, will make it look ugly..

This is the screen shot on white screen : 在此输入图像描述

And this on colored screen: 在此输入图像描述

HOw to resolve this issue?? Please help.

You should set style StageStyle.TRANSPARENT , see next code:

public class ChatWindow extends Application {

    @Override
    public void start(final Stage stage) throws Exception {
        stage.initStyle(StageStyle.TRANSPARENT); // here it is

        Group rg = new Group();
        Scene scene = new Scene(rg, 320, 240, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();

        Rectangle r = new Rectangle(5, 5, stage.getWidth() - 10, stage.getHeight() - 10);
        r.setFill(Color.STEELBLUE);
        r.setEffect(new DropShadow());
        rg.getChildren().add(r);
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM