0

I've seen this code that builds, my attention is to fix the resizing of those shape, and when I move them how can I block the shape to not move out of the window ?

I tried to fix resizing to not exceed when x is between 100 and 300, it works but when I try to decrease the shape size it does not move any more

static void enableDrag(Circle node, boolean canDragX, boolean canDragY, DragHandler dragHandler) {
        final Delta dragDelta = new Delta();
        node.setOnMousePressed(mouseEvent -> {
            // record a delta distance for the drag and drop operation.
            dragDelta.x = node.getCenterX() - mouseEvent.getX();
            dragDelta.y = node.getCenterY() - mouseEvent.getY();
            node.getScene().setCursor(Cursor.MOVE);
        });
        node.setOnMouseReleased(mouseEvent -> {
            node.getScene().setCursor(Cursor.HAND);
        });
        node.setOnMouseDragged(mouseEvent -> {
            double oldX = node.getCenterX();
            double oldY = node.getCenterY();

            System.out.println(node.getCenterX());
            if(node.getCenterX()<=300 && node.getCenterX()>100){
                System.out.println();

                double newX = mouseEvent.getX() + dragDelta.x;
                if (canDragX && newX > 0 && newX < node.getScene().getWidth()) {
                    node.setCenterX(newX);
                }

                double newY = mouseEvent.getY() + dragDelta.y;
                if (canDragY && newY > 0 && newY < node.getScene().getHeight()) {
                    node.setCenterY(newY);
                }

                newX = node.getCenterX();
                newY = node.getCenterY();

                if (dragHandler != null && (newX != oldX || newY != oldY)) {
                    dragHandler.handle(oldX, oldY, newX, newY);
                }
            }
        });
        node.setOnMouseEntered(mouseEvent -> {
            if (!mouseEvent.isPrimaryButtonDown()) {
                node.getScene().setCursor(Cursor.HAND);
            }
        });
        node.setOnMouseExited(mouseEvent -> {
            if (!mouseEvent.isPrimaryButtonDown()) {
                node.getScene().setCursor(Cursor.DEFAULT);
            }
        });
    }
ninja007
  • 53
  • 5
  • what do you meen when you move theme ?! – Ahmed Emad Sep 07 '18 at 19:39
  • 1
    Don't resize if the new dimensions would exceed the max. Same for moving: don't move if the new coordinates would go out of the render area. @AhmedEmad I think "theme" is supposed to be "them". – Slaw Sep 07 '18 at 21:58
  • yes as said by @Slaw Don't resize if the new dimensions would exceed the max as well as for moving – ninja007 Sep 10 '18 at 08:58

0 Answers0