Transparent Stage should not minimized when clicked inside in Javafx
Asked Answered
C

1

0

I am learning to create Screen Recording application in JavaFx. I want user to resize the rectangle to decide the screen capture area. I have made stage and scene Transparent by primaryStage.initStyle(StageStyle.TRANSPARENT); and scene.setFill(null); . I am able to resize the rectangular section but the problem is When I click inside the stage, It gets minimized as it is transparent. How to solve this issue ? I have seen this application screencast-o-matics and following the same. Please guide me on this.

Edit:: Code:

    import java.awt.Toolkit;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class ScreenCaptureDemo extends Application {

    Rectangle rectangle ;
    double x0,y0;

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

    @Override
    public void start(final Stage primaryStage) throws Exception {

        BorderPane borderPane = new BorderPane();

        GridPane gridPane = new GridPane();

        HBox box = new HBox();
        Button button1 = new Button("button2");
        Button button2 = new Button("Button3");
        Button button = new Button("button");

        box.getChildren().add(button);
        box.getChildren().add(button1);
        box.getChildren().add(button2);



        rectangle = new Rectangle(500.0, 500.0);

        rectangle.setStrokeWidth(2);
        rectangle.setArcHeight(15.0);
        rectangle.setArcWidth(15.0);
        rectangle.setFill(Color.TRANSPARENT);
        rectangle.setStroke(Color.RED);
        rectangle.setStrokeWidth(5);
        rectangle.getStrokeDashArray().addAll(3.0,13.0,3.0,7.0);

        gridPane.add(rectangle, 0, 0);
        gridPane.add(box, 0, 1);

        borderPane.setCenter(gridPane);

        Scene scene = new Scene(borderPane,Toolkit.getDefaultToolkit().getScreenSize().getWidth()-100,Toolkit.getDefaultToolkit().getScreenSize().getHeight()-100);
        scene.setOnMouseDragged(mouseHandler);
        scene.setOnMousePressed(mouseHandler);

        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        scene.setFill(null);
        rectangle.setMouseTransparent(true);
        rectangle.setPickOnBounds(true);
        primaryStage.show();

    }


    void setScaleRect(double sX, double sY){
        rectangle.setHeight(sY);
        rectangle.setWidth(sX);
    }

    EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent mouseEvent) {

            if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                double heightLowerLimit = rectangle.getHeight()-500;
                double heightUpperLimit = rectangle.getHeight()+500;
                double widthLowerLimit = rectangle.getWidth()-500;
                double widthUpperLimit = rectangle.getWidth()+500;
                if ((mouseEvent.getY() >heightLowerLimit && mouseEvent.getY() < heightUpperLimit) && 
                        (mouseEvent.getX() >widthLowerLimit && mouseEvent.getX() < widthUpperLimit)
                        ) {

                    double scaleX = mouseEvent.getX();
                    double scaleY = mouseEvent.getY();

                    setScaleRect(scaleX, scaleY);
                } else if ((mouseEvent.getY() >heightLowerLimit && mouseEvent.getY() < heightUpperLimit)
                        && (mouseEvent.getX() <widthLowerLimit && mouseEvent.getX() > widthUpperLimit)) {
                    double scaleY = mouseEvent.getY();
                    double scaleX=rectangle.getWidth();
                    setScaleRect(scaleX, scaleY);
                } else if (mouseEvent.getY() != rectangle.getHeight()
                        && mouseEvent.getX() == rectangle.getWidth()) {
                    double scaleX = mouseEvent.getX();
                    double scaleY=rectangle.getHeight();
                    setScaleRect(scaleX, scaleY);
                }

            }

        }

    };
}

Thank you in advance

Cellobiose answered 22/5, 2014 at 5:39 Comment(2)
Post reproducible sample code.Mingmingche
@UlukBiy: hie I want to follow same as screencast-o-matic.com.. I am not able to click the transparent window.As I click on it, It gets minimized..I have edited my question with code...Please check screen-o-matics. I want similar functionality as far as screen capture area is concerned.Thank youCellobiose
M
0

Fill the rectangle as

rectangle.setFill(Color.web("blue", 0.1));
// or more transparent
rectangle.setFill(Color.web("gray", 0.01));
Mingmingche answered 23/5, 2014 at 11:7 Comment(1)
yes it gives me transparent screen but in that case user won't be able to select the content on the screen. In case of screencast-o-matics , user can select the content on the screen which I think is a mandatory feature..Cellobiose

© 2022 - 2024 — McMap. All rights reserved.