Can you return the color of a rectangle object in java?

喜欢而已 提交于 2019-12-12 03:48:23

问题


For example, I create a rectangle object and set its fill to black. Would I be able to check the fill color of the rectangle to determine if its white or black? Something like:

Rectangle r = new Rectangle(10.0, 10.0, 10.0, 10.0);

if (r.getFill == 'BLACK') {
    r.setFill(Color.'PURPLE');
}

回答1:


The getFill() method returns a Paint object. Therefore, you should use equals for comparison:

Rectangle r = new Rectangle(1.0, 1.0, Color.WHITE);

if (r.getFill().equals(Color.WHITE)) {
    System.out.println("That's a white rectangle.");
}



回答2:


Assuming you mean the Rectangle that is a subclass of javafx.scene.shape.Shape, then yes, there is a method named getFill that returns a Paint (Documentation)



来源:https://stackoverflow.com/questions/40196753/can-you-return-the-color-of-a-rectangle-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!