簡體   English   中英

在JavaFX中獲取Shape對象的寬度和高度

[英]Getting width and height of a Shape object in JavaFX

我有一個方法,它接受Shape任何子類(可以是CircleRectanglePolygonLine等),並返回Shape對象。

public Shape returnShapeObject() {
   return circle1;
}

問題在於,一旦獲得了圓的形狀對象表示,它就不再具有.getRadius()方法。 它也沒有.getWidth().getHeight()

然后,如何在2d JavaFX中獲取Shape對象的半徑/寬度/高度?

您可以將方法Node#getLayoutBounds()用作:

List<Shape> shapes = new ArrayList<>();
shapes.add(new Circle(30));
shapes.add(new Rectangle(200, 200));
shapes.add(new Text("some arbitrary long text for testing"));
for (Shape shape : shapes) {
    System.out.println("bounds = " + shape.getLayoutBounds());
    System.out.println("width  = " + shape.getLayoutBounds().getWidth());
    System.out.println("height = " + shape.getLayoutBounds().getHeight());
}

請注意,請仔細閱讀該方法的javadoc。

如果您想要混凝土形狀的方法和屬性,則應按照他的評論中提到的@AKS進行澆鑄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM