1

我使用此代码在拖动鼠标时绘制一个矩形。问题是我只能从左到右绘制。

这是我的代码:

public class functionalTest extends Application {

    BorderPane pane;
    Rectangle rect;
    Group group;
    SimpleDoubleProperty rectinitX = new SimpleDoubleProperty();
    SimpleDoubleProperty rectinitY = new SimpleDoubleProperty();
    SimpleDoubleProperty rectX = new SimpleDoubleProperty();
    SimpleDoubleProperty rectY = new SimpleDoubleProperty();


    @Override
    public void start(Stage stage) {


        pane = new BorderPane();
        Scene scene = new Scene(pane, 800, 600);
        stage.setScene(scene);

        group = new Group();
        Circle circle = new Circle(200, 200, 25);
        circle.setFill(Color.HOTPINK);
        group.getChildren().add(circle);

        scene.setOnMouseDragged(mouseHandler);
        scene.setOnMousePressed(mouseHandler);
        scene.setOnMouseReleased(mouseHandler);

        rect = getNewRectangle();
        rect.widthProperty().bind(rectX.subtract(rectinitX));
        rect.heightProperty().bind(rectY.subtract(rectinitY));
        pane.getChildren().add(rect);
        pane.getChildren().add(group);
        stage.show();

        ArrayList<Node> containedNodesArray = new ArrayList<Node>();
        containedNodesArray = Main.dragBoxSelection(group, rect);

        if (containedNodesArray.size() > 0) {
            System.out.println("Success");
        }
    }

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

        @Override
        public void handle(MouseEvent mouseEvent) {

            if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
                rect.setX(mouseEvent.getX());
                rect.setY(mouseEvent.getY());
                rectinitX.set(mouseEvent.getX());
                rectinitY.set(mouseEvent.getY());
            } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                rectX.set(mouseEvent.getX());
                rectY.set(mouseEvent.getY());
                System.out.println("RECTX"+rectX.getValue());
                System.out.println("RECTY"+rectY.getValue());
                ArrayList<Node> containedNodesArray = new ArrayList<Node>();
                containedNodesArray = Main.dragBoxSelection(group, rect);

                if (containedNodesArray.size() > 0) {
                    System.out.println("Success");
                }
            } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) {
                // Clone the rectangle
                Rectangle r = getNewRectangle();
                r.setX(rect.getX());
                r.setY(rect.getY());
                r.setWidth(rect.getWidth());
                r.setHeight(rect.getHeight());
                pane.getChildren().add(r);

                // Hide the rectangle
                rectX.set(0);
                rectY.set(0);
            }
        }


    };

    private Rectangle getNewRectangle() {
        Rectangle r = new Rectangle();
        r.setFill(Color.web("blue", 0.1));
        r.setStroke(Color.BLUE);

        return r;
    }

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

请任何人都可以帮我从右上到左,从左到右,从右到左画其他方向。

4

5 回答 5

2

不知道它是否对任何人有用,但对我来说没有运气。但是,这在 JavaFX 8 中对我有用:

if( event.getEventType() == MouseDragEvent.MOUSE_DRRAGGED ) {
    double deltaX = event.getX() - initialPt.getX();
    double deltaY = event.getY() - initialPt.getY();

    if( deltaX < 0 ) {
        rect.setX( event.getX() );
        rect.setWidth( -deltaX );
    }
    else {
        rect.setX( initialPt.getX() );
        rect.setWidth( event.getX() - initialPt.getX() );
    }

    if( deltaY < 0 ) {
        rect.setY( event.getY() );
        rect.setHeight( -deltaY );
    }
    else {
        rect.setY( initialPt.getY() );
        rect.setHeight( event.getY() - initialPt.getY() );
    }
}

无需调用setTranslateX()setTranslateY()。不知道这是否与您使用的 FX 版本有关。

于 2019-04-24T14:22:42.900 回答
1

这是你需要做的:

拖动鼠标时:获取dx = mouseEvent.getX() - initial X; if dx < 0then 你需要setTranslateX(dx)and setWidth(-dx) else setTranslateX(0)andsetWidth(dx)

对 Y 坐标执行相同的操作,但是这次使用高度而不是宽度;

这是所有的了。

于 2013-07-07T16:52:10.460 回答
0

您缺少高度参数。

            if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
            rect.setX(event.getX());
            rect.setY(event.getY());
            rectinitX.set(event.getX());
            rectinitY.set(event.getY());
        } else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            Double dx = event.getX() - rectinitX.getValue();
            Double dy = event.getY() - rectinitY.getValue();
            if (dx < 0) {
                rectX.set(event.getX());
                rect.setTranslateX(dx);
                rect.widthProperty().bind(rectinitX.subtract(rectX));
            } else {
                rectX.set(event.getX());
                rect.setTranslateX(0);
                rect.widthProperty().bind(rectX.subtract(rectinitX));
            }
            if(dy < 0) {
                rectY.set(event.getY());
                rect.setTranslateY(dy);
                rect.heightProperty().bind(rectinitX.subtract(rectX));
            } else {
                rectY.set(event.getY());
                rect.setTranslateY(0);
                rect.heightProperty().bind(rectY.subtract(rectinitY));
            }

        } else if (event.getEventType() == MouseEvent.MOUSE_RELEASED) {
            // Clone the rectangle
            Rectangle r = getNewRectangle();
            r.setX(rect.getX());
            r.setY(rect.getY());
            r.setWidth(rect.getWidth());
            r.setHeight(rect.getHeight());
            pane.getChildren().add(r);

            // Hide the rectangle
            rectX.set(0);
            rectY.set(0);
  }}}};

这是用于在不同方向上绘制的确切代码,但不幸的是从左下到右上不起作用,我无法弄清楚。在默认情况下也可以完美地工作,也就是从左到右,但是在从右到左绘制时会出现代码错误。如果有人能弄清楚,我会很高兴的

于 2014-11-15T10:40:38.547 回答
0

我将代码替换为这样: EventHandler mouseHandler = new EventHandler() {

    @Override
    public void handle(MouseEvent mouseEvent) {

        if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
            rectinitX.set(mouseEvent.getX());
            rectinitY.set(mouseEvent.getY());
        } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            Double width = mouseEvent.getX() - rectinitX.getValue();
            if (width < 0) {
                rectX.set(mouseEvent.getX());
                rect.setTranslateX(width);
                rect.widthProperty().bind(rectinitX.subtract(rectX));
                System.out.println(rect.getWidth());
            } else {
                rectX.set(mouseEvent.getX());
                rect.setTranslateX(0);
                rect.widthProperty().bind(rectX.subtract(rectinitX));
            }

        } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) {
            // Clone the rectangle
            Rectangle r = getNewRectangle();
            r.setX(rect.getX());
            r.setY(rect.getY());
            r.setWidth(rect.getWidth());
            r.setHeight(rect.getHeight());
            pane.getChildren().add(r);


        }
    }
};

一开始我只想尝试你的宽度值解决方案,但它似乎不起作用。你对我的实施有什么意见吗?

于 2013-07-07T19:08:45.090 回答
0

它应该读

 if(dy < 0)
 {
   rectY.set(mouseEvent.getY());
   rect.setTranslateY(dy);
   rect.heightProperty().bind(rectinitY.subtract(rectY));
 }
于 2017-08-08T18:20:33.237 回答