JavaFX在创建边界矩形时删除多个圆

时间:2016-04-05 15:44:31

标签: java javafx

我正在创建一个javafx应用程序,在用户点击主鼠标时围绕圆圈创建一个有界矩形。用户还可以使用辅助鼠标按钮移除圆圈,并且边界矩形应相应地做出反应并使用剩余的圆圈作为其上限和下限。我的程序的问题是,当我尝试连续删除多个圆圈时它不起作用(它只删除并调整最后一个圆圈的大小)

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Collections;

public class BoundRectangle extends Application {
    double minX, maxX, minY, maxY;
    Pane pane = new Pane();
    Rectangle rectangle;
    ArrayList<Circle> allCircles = new ArrayList<>();

    @Override
    public void start(Stage primaryStage) {

        VBox mainBox = new VBox();
        Pane mainPane = new Pane(mainBox);
        mainPane.setPadding(new Insets(10, 10, 10, 10));

        pane.getChildren().addAll(mainPane);
        mainBox.setLayoutX(10);
        mainBox.setLayoutY(10);

        pane.setOnMouseClicked(e -> {
             if (e.getButton() == MouseButton.PRIMARY)   {
                   Circle circle = new Circle(e.getX(), e.getY(), 10);
                   allCircles.add(circle);

                   pane.getChildren().add(drawRectangle());
                   pane.getChildren().add(circle);
                   System.out.println("maxX " + maxX);
                   System.out.println("maxY " + maxY);
                   System.out.println("minX " + minX);
                   System.out.println("minY " + minY);

                circle.setOnMouseClicked(evt -> {
                    if (evt.getButton() == MouseButton.SECONDARY) {
                            pane.getChildren().remove(circle);
                            allCircles.remove(circle);
                        pane.getChildren().add(drawRectangle());

                    }

                });

                circle.setStroke(Color.BURLYWOOD);
                circle.setStrokeWidth(3);
                circle.setFill(Color.TRANSPARENT);

            }
        });

        Scene scene = new Scene(pane, 600, 600);

        primaryStage.setScene(scene);
        primaryStage.setTitle("click circles, make rectangle");
        primaryStage.show();
    }

    public Rectangle drawRectangle() {
        refresh();
        getMinMax();

        if (pane.getChildren().size() == 1)
        {
            Rectangle rect0 = new Rectangle(0, 0, 0, 0);
            return rect0;
         }

        Rectangle boundingRect = new Rectangle();
        boundingRect.setX(minX - 10 - 2);
        boundingRect.setY(minY - 10 - 2);
        boundingRect.setWidth(maxX - minX + 2.0 * 10 + 2);
        boundingRect.setHeight(maxY - minY + 2.0 * 10 + 2);
        boundingRect.setStroke(Color.BLACK);
        boundingRect.setStrokeWidth(2);
        boundingRect.setFill(Color.TRANSPARENT);

        return boundingRect;
    }

    public void getMinMax() {

        maxY = allCircles.get(0).getCenterY();
        minY = allCircles.get(0).getCenterY();
        maxX = allCircles.get(0).getCenterX();
        minX = allCircles.get(0).getCenterX();

        for (Circle c : allCircles) {
            if (c.getCenterX() < minX)
                minX = c.getCenterX();
            if (c.getCenterX() > maxX)
                maxX = c.getCenterX();
            if (c.getCenterY() < minY)
                minY = c.getCenterY();
            if (c.getCenterY() > maxY)
                maxY = c.getCenterY();
         }
    }

    private void refresh() {
        ObservableList<Node> list = pane.getChildren();

        for (Node c : list) {
            if (c instanceof Rectangle) {
                pane.getChildren().remove(c);
                break;
            }
        }
    }

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

1 个答案:

答案 0 :(得分:0)

您添加的最后一个圆圈显示在矩形的顶部;但矩形显示在所有其他圆圈的顶部。因此,只有添加的最后一个圆圈才能获得鼠标点击(其他圆圈的点击定位到矩形)。

最快的解决方法是使矩形鼠标透明:

boundingRect.setMouseTransparent(true);

更好的解决方案可能只是创建一个矩形并更新其xywidthheight属性。这样你就可以确保矩形被添加一次并保持在堆栈的底部。