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

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 に答える