无法弄清楚区域的边界是如何工作的

Cannot figure out how the bounds of a Region work

我目前正在为逻辑电路编写 CAD-like 程序(这是我的第一个 "graphics intensive" 程序)。当我在原理图上放置一个组件时,假设是一个与门(在其根部是区域 class),我希望能够与其交互(select 它,更改其属性等).到目前为止,一切都很好。我可以点击它,一切顺利。但是,如果我在其外部单击,鼠标单击事件仍会显示组件的来源 (!)。

进一步挖掘,我在鼠标点击处理程序中放置了一些痕迹,发现 getBoundsInLocal()getBoundsInParent() return 边界比应有的大 50% 左右. getLayoutBounds()getWidth()getHeight() 做 return 正确的值。

放置组件的窗格是一个简单的 Pane object,但它使用 setScaleX()setScaleY() 来实现缩放功能。我确实尝试禁用它们,但没有成功。

public abstract class SchematicComponent
        extends Region {
    private Shape graphicShape = null;

    public Shape getGraphicShape() {
        if( isShapeDirty() ) {
            if( graphicShape != null ) {
                getChildren().remove( graphicShape );
            }

            graphicShape = createShape();

            markShapeDirty( false );
            if( graphicShape != null ) {
                getChildren().add( graphicShape );
            }
        }

        return graphicShape;
    }

    abstract protected Shape createShape();
}

abstract public class CircuitComponent
        extends SchematicComponent {
}

abstract public class LogicGate
        extends CircuitComponent {

    @Override
    protected void layoutChildren() {
        super.layoutChildren();

        Pin outPin;
        final double inputLength = getInputPinsMaxLength();

        // Layout the component around its center.
        // NOTE: I did try to set the center offset to 0 with no luck.
        Point2D centerOffset = getCenterPointOffset().multiply( -1 );

        Shape gateShape = getGraphicShape();
        if( gateShape != null ) {
            gateShape.setLayoutX( centerOffset.getX() + inputLength );
            gateShape.setLayoutY( centerOffset.getY() );
        }

        /* Layout the output pins. */
        outPin = getOutputPin();
        if( outPin != null ) {
            outPin.layout();

            outPin.setLayoutX( centerOffset.getX() + getWidth() );
            outPin.setLayoutY( centerOffset.getY() + getHeight() / 2 );
        }

        /* Compute the first input pin location and the gap between each
           pins */
        double pinGap = 2;
        double y;
        if( getInputPins().size() == 2 ) {
            y = centerOffset.getY() + getHeight() / 2 - 2;
            pinGap = 4;
        }
        else {
            y = centerOffset.getY() + ( getHeight() / 2 ) - getInputPins().size() + 1;
        }

        /* Layout the input pins */
        for( Pin inPin : getInputPins() ) {
            inPin.layout();

            inPin.layoutXProperty().set( centerOffset.getX() );
            inPin.layoutYProperty().set( y );

            y += pinGap;
        }
    }

}

// The actual object placed on the schematic
public class AndGate
        extends LogicGate {

    @Override
    protected double computePrefWidth( double height ) {
        // NOTE: computeMin/MaxWidth methods call this one

        double width = getSymbolWidth() + getInputPinsMaxLength();
        double length = 0;

        width += length;

        if( getOutputPin().getLength() > 0 ) {
            width += getOutputPin().getLength();
        }

        return width; // Always 16
    }

    @Override
    protected double computePrefHeight( double width ) {
        // NOTE: computeMin/MaxHeight methods call this one
        return getSymbolHeight() + getExtraHeight();  // Always 10
    }

    @Override
    protected Shape createShape() {
        Path shape;
        final double extraHeight = getExtraHeight();
        final double inputLength = getInputPinsMaxLength();
        final double outputLength = getOutputPin().getLength();

        /* Width and Height of the symbol itself (i,e, excluding the
           input/output pins */
        final double width = getWidth() - inputLength - outputLength;
        final double height = getHeight() - extraHeight;

        /* Starting point */
        double startX = 0;
        double startY = extraHeight / 2;

        ArrayList<PathElement> elements = new ArrayList<>();
        elements.add( new MoveTo( startX, startY ) );
        elements.add( new HLineTo( startX + ( width / 2 ) ) );
        elements.add( new ArcTo( ( width / 2 ), // X radius
                                 height / 2, // Y radius
                                 180, // Angle 180°
                                 startX + ( width / 2 ), // X position
                                 startY + height, // Y position
                                 false, // large arc
                                 true ) ); // sweep
        elements.add( new HLineTo( startX ) );

        if( extraHeight > 0 ) {
            /* The height of the input pins is larger than the height of
               the shape so we need to add extra bar on top and bottom of 
               the shape.
             */
            elements.add( new MoveTo( startX, 0 ) );
            elements.add( new VLineTo( extraHeight + height ) );

        }
        else {
            elements.add( new VLineTo( startY ) );
        }

        shape = new Path( elements );
        shape.setStroke( getPenColor() );
        shape.setStrokeWidth( getPenSize() );
        shape.setStrokeLineJoin( StrokeLineJoin.ROUND );
        shape.setStrokeLineCap( StrokeLineCap.ROUND );
        shape.setFillRule( FillRule.NON_ZERO );
        shape.setFill( getFillColor() );

        return shape;
    }
} // End: LogiGate

// SchematicView is the ScrollPane container that handles the events
public class SchematicView
        extends ScrollPane {

    /* Mouse handler inner class */
    private class MouseEventHandler
            implements EventHandler<MouseEvent> {

    @Override
    public void handle( MouseEvent event ) {
        if( event.getEventType() == MouseEvent.MOUSE_CLICKED ) {
            processMouseClicked( event );
        }
        else { /* ... more stuff ... */ }
     }

    private void processMouseClicked( MouseEvent event ) {
        Object node = event.getSource();
        SchematicSheet sheet = getSheet();

        Bounds local = ( (Node) node ).getLayoutBounds();
        Bounds local1 = ( (Node) node ).getBoundsInLocal();
        Bounds parent = ( (Node) node ).getBoundsInParent();

        // At this point, here is what I get:
        // node.getHeight() = 10    -> Good
        // local.getHeight() = 10   -> Good
        // local1.getHeight() = 15.6499996...   -> Not expected!
        // parent.getHeight() = 15.6500015...   -> Not expected!

        /*... More stuff ... */
    }
}

所以在这一点上,我 运行 了解正在发生的事情的线索。这些 getBoundsInXXX() 值从何而来?它们也不符合 parent 的比例值。 getWidth() 也是如此:我得到 24.825000... 而不是 16.

看到这里,我明白了为什么在组件外部单击就像单击它一样。它的边界比它应该的大 50%。

我用谷歌搜索了这该死的东西并搜索了将近 2 天的一些文档,但我仍然感到困惑。我想我明白 getBoundsInXXX() 方法会进行自己的计算,但它会偏离那么多吗?我不这么认为。我最好的猜测是它是 createShape() 方法内部的东西,但我无法弄清楚它是什么。

有人知道发生了什么事吗?

非常感谢您的帮助。

P.S.: 这是我第一次 post 来这里,希望我做对了 ;)

我想我终于找到问题了:)

基本上,Pin 自定义形状绘制在 X 轴的负数部分(计算错误,我的错!)。我最好的猜测是,不知何故,Java 注意到我在标准边界之外绘制,然后将额外的 space 添加到边界,因此,宽度增加 50%,这与 Pin。将其绘制在正区域似乎已解决问题。

我不是 100% 确定这是否是正确的答案,但它是有道理的,并且现在正在按预期工作。