Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating undecorated stage in javafx 2.0

Tags:

java

javafx-2

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 : enter image description here

And this on colored screen: enter image description here

HOw to resolve this issue?? Please help.

like image 781
batman Avatar asked Dec 29 '11 15:12

batman


People also ask

How do you set a scene in a stage?

You can add a Scene object to the stage using the method setScene() of the class named Stage.

What method is invoked to display a stage in JavaFX?

Showing a Stage The difference between the JavaFX Stage methods show() and showAndWait() is, that show() makes the Stage visible and the exits the show() method immediately, whereas the showAndWait() shows the Stage object and then blocks (stays inside the showAndWait() method) until the Stage is closed.


1 Answers

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();
    }
}
like image 84
Sergey Grinev Avatar answered Oct 11 '22 22:10

Sergey Grinev