【问题标题】:JavaFX removing more than one circle while creating bounding rectangleJavaFX 在创建边界矩形时删除了多个圆圈
【发布时间】:2016-07-25 16:12:20
【问题描述】:

我正在制作一个 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);
    }
}

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    您添加的最后一个圆圈出现在矩形的顶部;但是矩形出现在所有其他圆圈的顶部。所以只有最后一个添加的圆圈会得到鼠标点击(点击其他圆圈会定位到矩形)。

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

    boundingRect.setMouseTransparent(true);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2015-05-18
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      相关资源
      最近更新 更多