How to implement selection of Nodes in JavaFX

后端 未结 3 1463
离开以前
离开以前 2020-12-01 22:50

I am very new JavaFX, started learning it yesterday. Spent the whole day reading through the documentation, but learned nothing...

Here is what I want to do, make a

3条回答
  •  臣服心动
    2020-12-01 23:04

    This is how I did it... Simple and Easy

    Main Class

    public class Lab05 extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            double width = 400;
            double height = 400;
    
            int num_of_circles = 5;
            int radius_of_circles = 20;
    
            BorderPane root = new BorderPane();
    
            for (int i = 0; i < num_of_circles; i++) {
                MyCircle temp = createCircle(radius_of_circles, (50 * i) + 1, 100);
                temp.setFrame(width, height);
                root.getChildren().add(temp.getCircle());
                temp.getCircle().addEventFilter(MouseEvent.ANY, temp);
            }
    
            Scene scene = new Scene(root, width, height);
    
            primaryStage.setTitle("Lab 05");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
        public static MyCircle createCircle(int radius, int x, int y){
            MyCircle circle = new MyCircle();
            circle.setCircle(radius, x, y);
            return circle;
        }
    }
    

    MyCircle Class

    public class MyCircle implements EventHandler{
        private static double frameX = 0;
        private static double frameY = 0;
        private final Circle circle = new Circle();
        private static final List CIRCLES = new ArrayList<>();
    
        public void setCircle(int radius, int x, int y){
            circle.setRadius(radius);
            position(x,y);
            circle.setStrokeWidth(3);
            circle.setStroke(Color.valueOf("white"));
            CIRCLES.add(circle);
        }
    
        public void setFrame(double x, double y){
            frameX = x;
            frameY = y;
        }
    
        public Circle getCircle(){
            return circle;
        }
    
        public void position(double x, double y){
            if ( x < frameX && x > 0)
                circle.setCenterX(x);
            if ( y < frameY && y > 0)
                circle.setCenterY(y);
        }
    
        public void selected(){
            CIRCLES.stream().forEach((c) -> {
                c.setStroke(Color.valueOf("white"));
            });
            circle.setStroke(Color.valueOf("orange"));
        }
    
        public void unselected() {
            circle.setStroke(Color.valueOf("white"));
        }
    
        @Override
        public void handle(MouseEvent event) {
            if (event.getEventType() == MouseEvent.MOUSE_PRESSED){
                selected();
            }
            else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED){
                position(event.getX(), event.getY());
            }
        }
    }
    

提交回复
热议问题