如何为这些形状设置最大宽度和高度尺寸?

时间:2018-09-07 17:01:33

标签: javafx lambda

我已经看到code的构建,我的注意力是修复这些形状的大小,当我移动它们时,如何阻止该形状不移出窗口?

当x介于100到300之间时,我试图将大小调整为不超过最大值,这是可行的,但是当我尝试减小形状尺寸时,它不再移动了

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);
            }
        });
    }

0 个答案:

没有答案