无法弄清楚Region的界限是如何工作的

问题描述 投票:0回答:1

我正在为逻辑电路编写类似CAD的程序(这是我的第一个“图形密集型”程序)。当我在原理图上放置一个组件时,让我们说一个AND门(它的根类为Region类),我希望能够与它进行交互(选择它,更改其属性等)。到现在为止还挺好。我可以点击它,一切顺利。但是,如果我在其外部单击,鼠标单击事件仍将组件显示为源(!)。

进一步挖掘,我在鼠标点击处理程序中添加了一些痕迹,发现getBoundsInLocal()getBoundsInParent()返回的边界比它应该大50%左右。 getLayoutBounds()getWidth()getHeight()确实返回了正确的值。

布置组件的窗格是一个简单的Pane对象,但它使用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()值来自哪里?它们也与父母的比例值不匹配。与getWidth()一样:我得到24.825000 ......而不是16。

看看这个,我理解为什么在组件外部点击就像我点击它一样。它的界限比它应该的大约50%。

我用谷歌搜索该死的东西,现在搜索一些文档差不多2天了,我仍然感到困惑。我想我明白getBoundsInXXX()方法可以做自己的计算,但可能会被这么多吗?我不是这样的。我最好的猜测是它是createShape()方法中的内容,但我无法弄清楚它是什么。

任何人都知道发生了什么?

非常感谢您的帮助。

P.S。:这是我在这里的第一篇文章,所以希望我做对了;)

javafx-8
1个回答
0
投票

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

基本上,Pin自定义形状是在X轴的负面部分绘制的(错误的计算,我的坏!)。我最好的猜测是,不知何故,Java注意到我超出了标准边界,然后添加了用于边界的额外空间,因此,增加了50%的宽度,这与Pin的长度相匹配。在正面区域绘制似乎已经解决了问题。

我不是百分之百确定这是否是正确答案,但它是有道理的,它现在正在发挥作用。

© www.soinside.com 2019 - 2024. All rights reserved.