Cora Coleman Cora Coleman - 1 month ago 4
Java Question

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

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');
}

Answer

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.");
}
Comments