JavaFX、添加或删除点

 


package fx;

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

import java.util.HashSet;
import java.util.Set;

public class Exercise15_15 extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();

        pane.setOnMouseClicked(event -> {
            Circle circle;

            if (event.getButton() == MouseButton.PRIMARY) {
                circle = new Circle(event.getX(), event.getY(), 5);
                circle.setStyle("-fx-fill: white; -fx-stroke: black;");
                pane.getChildren().add(circle);

                try {
                    circle.setOnMouseClicked(event1 -> {
                        if (event1.getButton() == MouseButton.SECONDARY)
                        for (Node node : pane.getChildren()) {
                            Circle temp = (Circle)node;
                            if (isInCircle(temp, event1.getX(), event1.getY())) {
                                pane.getChildren().remove(temp);
                                break;
                            }
                        }
                    });
                } catch (Exception ex) {}
            }
        });

        Scene scene = new Scene(pane, 350, 200);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Exercise15_15");
        primaryStage.show();
    }

    private boolean isInCircle(Circle circle, double x, double y) {
        return Math.sqrt(Math.pow(x - circle.getCenterX(), 2) + Math.pow(y - circle.getCenterY(), 2)) <= circle.getRadius();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值