JavaFX可拖动形状转义窗格到整个场景

时间:2017-04-14 18:32:54

标签: javafx shapes scene pane

我的程序旨在允许用户在窗格上放置形状并自由移动它们。我成功地将初始形状放在窗格上以及在窗格周围移动形状。我遇到的问题是,形状可以被拖到他们应该限制的窗格之外。形状可以被拖动到场景中的任何地方,这是不允许的(下图)。

Shape Breaking out of Pane and into entire Scene

非常感谢您的协助。谢谢。

    my_pane_for_rectangles.addEventHandler(MouseEvent.MOUSE_PRESSED, 
            new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent event) {

            if(MouseHasImage == true){
                try {
                    Rectangle rectangle_for_img = new Rectangle();// Creates the rectangle that will hold our image

                    Image my_image = new Image(ClickedAddDevice.image_file.toURI().toURL().toExternalForm());// Select the appropriate image

                    rectangle_for_img.setFill(new ImagePattern(my_image));// Fill the rectangle with the image
                    rectangle_for_img.relocate(event.getX(), event.getY());// Set the X and Y coordinates for our device (based on panel X and Y mouse position)

                    rectangle_for_img.setHeight(my_image.getHeight());// Set the height of the rectangle to the height of the image
                    rectangle_for_img.setWidth(my_image.getWidth());// Set the width of the rectangle to the width of the image

                    rectangle_for_img.setId(placed_button_id);// Set the ID of the rectangle to the name of the device


                    rectangle_for_img.setOnMousePressed(new EventHandler<MouseEvent>(){
                        public void handle(MouseEvent mouse_pressed) {
                                orgSceneX = mouse_pressed.getSceneX();
                                orgSceneY = mouse_pressed.getSceneY();
                                orgTranslateX = rectangle_for_img.getTranslateX();
                                orgTranslateY = rectangle_for_img.getTranslateY();
                        }
                    });

                    rectangle_for_img.setOnMouseDragged(new EventHandler<MouseEvent>(){
                        public void handle(MouseEvent mouse_dragged) {
                            my_scene.setCursor(Cursor.CLOSED_HAND);
                            rectangle_for_img.toFront();
                            isbeingdragged = true;
                            double offsetX = mouse_dragged.getSceneX() - orgSceneX;
                            double offsetY = mouse_dragged.getSceneY() - orgSceneY;
                            double newTranslateX = orgTranslateX + offsetX;
                            double newTranslateY = orgTranslateY + offsetY;

                            rectangle_for_img.setTranslateX(newTranslateX);
                            rectangle_for_img.setTranslateY(newTranslateY);
                        }
                    });

                    rectangle_for_img.setOnMouseReleased(new EventHandler<MouseEvent>(){
                        public void handle(MouseEvent mouse_released) {
                            if(isbeingdragged == true){
                                //System.out.println("Released from drag");
                                my_scene.setCursor(Cursor.DEFAULT);
                                System.out.println("X: " + mouse_released.getX() + ", Y: " + mouse_released.getY());
                            }

                            isbeingdragged = false;
                        }
                    });

0 个答案:

没有答案