Hi guys
I'm currently doing chapter on Javafx for my course, and the assignment is asking us to make a javafx program that has radio buttons which change the colour of a square, a slider, and if the user clicks anywhere other than the radio buttons and slider, it should play a warning sound.
I did it all and its working except for the part that says the warning audio should not sound if the user clicks on the radio buttons or the slider. Its not working no matter what I do. It plays the audio wherever I click on the window, even when clicking on the radio buttons and slider. I don't know what I'm doing wrong and would appreciate the help! Here is my code:
package application; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.RadioButton; import javafx.scene.control.Slider; import javafx.scene.control.ToggleGroup; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.media.*; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.scene.text.TextAlignment; import javafx.stage.Stage; public class ChangingShape extends Application { private RadioButton redButton, greenButton, orangeButton; private Rectangle square = new Rectangle(100,100,Color.RED); private AudioClip audio = new AudioClip ("file:audio.mp3"); public static void main (String[] args) { launch(args); } public void start(Stage primaryStage) { // Create the root BorderPane BorderPane root = new BorderPane(); root.setPadding(new Insets(10)); root.setStyle("-fx-background-color: lightyellow;"); // Add instructions to the top root.setTop(instructions()); // Add radio buttons to the left root.setLeft(radiobuttons()); // Add the square to the center root.setCenter(setShape()); // Combine the slider and warning into a VBox VBox sliderAndWarning = new VBox(10); // Set some space between the slider and the warning sliderAndWarning.setAlignment(Pos.CENTER); sliderAndWarning.getChildren().addAll(setSlider(), warning()); // Add the combined VBox to the bottom root.setBottom(sliderAndWarning); // Add a mouse click event filter to play a warning sound when the user clicks // anywhere outside the controls (radio buttons and slider). root.addEventFilter(MouseEvent.MOUSE_CLICKED, this::processWarningAudio); Scene scene = new Scene (root, 400, 400); primaryStage.setTitle("Changing Square"); primaryStage.setScene(scene); primaryStage.show(); } // Setting up the instructions at the top of the window private VBox instructions() { Text instruction = new Text ("Change the square color using the radio buttons."); Text instruction_two = new Text ("Change the scale of the square between 0-100% using the slider."); instruction.setFont(Font.font("Calibri", FontWeight.BOLD, 14)); instruction_two.setFont(Font.font("Calibri", FontWeight.BOLD, 14)); //setting up the text into a text box and aligning it to the top of the window VBox textBox = new VBox(); textBox.setAlignment(Pos.CENTER); textBox.setPadding(new Insets(25,0,0,0)); textBox.getChildren().addAll(instruction, instruction_two); return textBox; } //Setting the warning to appear at the bottom of the window private VBox warning() { Text warning = new Text ("Select the radio buttons or the slider only. \nYou'll " + "hear a warning sound if the mouse is clicked elsewhere!"); warning.setFont(Font.font("Calibri", 14)); warning.setFill(Color.RED); warning.setTextAlignment(TextAlignment.CENTER); VBox warningBox = new VBox(); warningBox.setAlignment(Pos.CENTER); warningBox.setPadding(new Insets(10)); warningBox.getChildren().add(warning); return warningBox; } //This method checks to see which button is clicked and changes the colour accordingly private void processRadioButton(ActionEvent event) { if (redButton.isSelected()) { square.setFill(Color.RED); } else if(greenButton.isSelected()) { square.setFill(Color.GREEN); } else { square.setFill(Color.ORANGE); } } //This method sets up the radio buttons, their labels, and positions private VBox radiobuttons() { ToggleGroup group = new ToggleGroup(); redButton = new RadioButton("Red"); redButton.setSelected(true); redButton.setToggleGroup(group); greenButton = new RadioButton("Green"); greenButton.setToggleGroup(group); orangeButton = new RadioButton("Orange"); orangeButton.setToggleGroup(group); redButton.setOnAction(this::processRadioButton); greenButton.setOnAction(this::processRadioButton); orangeButton.setOnAction(this::processRadioButton); VBox buttons = new VBox(10); buttons.setAlignment(Pos.CENTER_LEFT); buttons.setPadding(new Insets(50)); buttons.getChildren().addAll(redButton, greenButton, orangeButton); return buttons; } //Setting up the square's alignment private HBox setShape() { HBox shapeBox = new HBox(); shapeBox.setAlignment(Pos.CENTER); shapeBox.setPadding(new Insets(0,70,0,0)); shapeBox.getChildren().add(square); return shapeBox; } //Setting up the slider to control the size of the square private VBox setSlider() { Slider slider = new Slider(0, 100, 75); slider.setShowTickMarks(true); slider.setShowTickLabels(true); square.heightProperty().bind(slider.valueProperty()); square.widthProperty().bind(slider.valueProperty()); VBox slide = new VBox(10); slide.setAlignment(Pos.CENTER); slide.setPadding(new Insets(15,0,10,0)); slide.getChildren().add(slider); return slide; } //This method sets up the warning audio that sounds every time the //mouse is clicked anywhere except for the radio buttons and the slider private void processWarningAudio(MouseEvent event) { Object target = event.getTarget(); // Check if the target is a RadioButton or the Slider itself if (target instanceof RadioButton || target instanceof Slider) { return; // Do nothing if the click is on a valid control } // Play warning sound if clicked elsewhere audio.play(); } }