1

My program is meant to allow users to put shapes on a pane and move them around freely. I am successful in both placing the initial shape on the pane as well as moving the shape around the pane. The issue I am having is that shapes are able to be dragged outside of the pane they should be limited to. Shapes are able to be dragged anywhere on the scene which shouldn't be allowed (picture of this below).

Shape Breaking out of Pane and into entire Scene

Your assistance is greatly appreciated. Thank you.

    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;
                        }
                    });
ajc5869
  • 11
  • 2
  • You have to get the bounds of the plane and use those to keep the shapes within them. – SedJ601 Apr 14 '17 at 18:45
  • Possible duplicate of [this](http://stackoverflow.com/questions/33588418/detecting-window-boundary-in-javafx). – SedJ601 Apr 14 '17 at 18:47
  • you can not ask two question Sir, make it one, please, about containing nodes you can check for max regions, that is your parent Node boundsInParent and make dragging stop or pause, it – Elltz Apr 14 '17 at 21:24

0 Answers0