![](/img/trans.png)
[英]Problems getting the width and height of a pane from a class object - Javafx
[英]Getting width and height of a Shape object in JavaFX
我有一個方法,它接受Shape
任何子類(可以是Circle
, Rectangle
, Polygon
, Line
等),並返回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.