Issue
I am working with circle objects in a gridpane and I have to check to color of the circle object by clicking it. For example if it's blue, do some stuff, if it's red, do other stuffs.
Solution
get shape color with mouse event
you can get it with getFill()
and change it with setFill()
method and provide a concrete Paint object as argument.
this is a single class javafx app you can try .
mouse event will print getFill as string
App.java
public class App extends Application {
@Override
public void start(Stage stage) {
Paint defaultColor = Color.AQUA;
Paint clickColor = Color.YELLOWGREEN;
Circle circle = new Circle(50, clickColor);
circle.setOnMouseClicked(e -> {
if (circle.getFill().equals(defaultColor)) {
circle.setFill(clickColor);
}else{
circle.setFill(defaultColor);}
System.out.println(circle.getFill().toString());
});
Scene scene = new Scene(new StackPane(circle), 200, 200);
stage.setScene(scene);
stage.setTitle("changing fill color");
stage.show();
}
}
Answered By - Giovanni Contreras
Answer Checked By - Willingham (JavaFixing Volunteer)