问题
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