I'm trying to have a few CheckBoxes visualize ingredients on a pizza in this javafx app. The Pizza is a ImageView. But I don't know how I will go about adding ingredients. Lets talk about salami for a second!
My first idea was to do this on my setOnAction of my CheckBox salami: (gc being my graphics context)
Image salami1 = new Image("salami.png");
gc.setFill(new ImagePattern(salami1);
gc.fillOval(250, 200, 60, 60);
(I tried just adding another ImageView on top instead, but even though It was a .png which transparent background the background would still show. So I tried this instead. Since I will only go for cheese, salami this would be fine too. This is very basic and supposed to be just for practice on my side.)
However, How do I make the salami disappear again once I uncheck the box? I'm aware of gc.clearRect() but that's it. I'm clueless as on how to do this upon unchecking the box. Thanks in advance
You can do this pretty simply by binding the
ImageView'svisibleproperty to theselectedproperty of the appropriateCheckBox.Here's a quick sample:
The important part is the binding line. This is the basic logic that this line performs:
imgSalamiobject's visibility to match whetherchkSalamiis selected.This means you do not need to mess around with adding any loops or
ChangeListeners to theCheckBox; just bind each image to the matching checkbox.