如何使用JavaFX在图像上的元素上绘制一个矩形?

Nav*_*waj 2 javafx

我有一个显示在我的JavaFX应用程序上的图像.给定坐标,我必须使用矩形在图像上显示该部分.例如,如果图像上有文本字段并且我给出了文本字段的坐标,则图像上的文本字段上应显示一个矩形(就像突出显示它一样).绘制矩形很容易,但我很难将其定位在图像上.请帮忙.

Ulu*_*Biy 5

您应该使用不自动布局其子项的父组件.为此您可以使用Pane:

@Override
public void start( final Stage primaryStage )
{
    ImageView imageView = new ImageView( ... );
    // Optional: locating the image at iX-iY
    // imageView.setX( iX );
    // imageView.setY( iY );

    Rectangle r = new Rectangle( rX, rY, width, height );

    // Add rectangle at the last, so it shows up on the top of other children
    Pane pane = new Pane( imageView, r );

    final Scene scene = new Scene( pane, 400, 300 );
    primaryStage.setScene( scene );
    primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)

  • @ChristopherSmith.无论其子x和y属性如何,StackPane将默认垂直和水平对齐中心.然而,Pane(所有窗格的超类)不会这样做. (2认同)