在Javafx中单击内部时,不应最小化透明舞台

时间:2014-05-22 05:39:56

标签: javafx javafx-2 transparent screen-recording

我正在学习在JavaFx中创建屏幕录制应用程序。我希望用户调整矩形的大小以决定屏幕捕获区域。我通过primaryStage.initStyle(StageStyle.TRANSPARENT);scene.setFill(null);使舞台和场景变得透明。 我可以调整矩形部分的大小,但问题是当我在舞台内部点击时,它会变得最小化,因为它是透明的。如何解决这个问题? 我已经看过这个应用程序screencast-o-matics并遵循相同的原则。请指导我。

编辑:: 代码:

    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);
                }

            }

        }

    };
}

提前谢谢

1 个答案:

答案 0 :(得分:0)

将矩形填充为

rectangle.setFill(Color.web("blue", 0.1));
// or more transparent
rectangle.setFill(Color.web("gray", 0.01));