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

时间:2019-04-04 04:45:58

标签: javafx-8

我目前正在为逻辑电路编写类似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 .:这是我在这里的第一篇文章,所以希望我做对了;)

1 个答案:

答案 0 :(得分:0)

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

基本上,Pin自定义形状是在X轴的负数部分绘制的(错误的计算,这很糟糕!)。我最好的猜测是,Java会以某种方式注意到我超出了标准范围,然后将额外的空间添加到了范围内,因此,宽度增加了50%,与Pin的长度相匹配。在正面区域绘制它似乎已经解决了问题。

我不确定100%是否是正确的答案,但这是有道理的,并且可以正常工作。