Question

Write a JavaFX application that allows the user to select a color out of five options...

Write a JavaFX application that allows the user to select a color out of five options provided by a set of radio buttons. Change the color of a displayed square accordingly.

Can you provide a picture as well of the application? Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java code

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;



public class SquareFillGUI extends Application {

    RadioButton rd1, rd2,rd3,rd4,rd5;
    @Override
    public void start(Stage primaryStage) throws Exception {
        /**
         * Create radio buttons
         */
        rd1 = new RadioButton("Blue");
        rd2 = new RadioButton("Yellow");
        rd3 = new RadioButton("Red");
        rd4 = new RadioButton("Green");
        rd5 = new RadioButton("Purple");
        /**
         * Add radio buttons to toggle group
         * So the at a time one button is
         * active
         */
        ToggleGroup toggleGroup = new ToggleGroup();
        rd1.setToggleGroup(toggleGroup);
        rd2.setToggleGroup(toggleGroup);
        rd3.setToggleGroup(toggleGroup);
        rd4.setToggleGroup(toggleGroup);
        rd5.setToggleGroup(toggleGroup);
        //Create rectangle
        Rectangle rectangle = new Rectangle(200,200);

        rectangle.setFill(Color.TRANSPARENT);
        rectangle.setStroke(Color.BLACK);
        rd1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                rectangle.setFill(Color.BLUE);
            }
        });
        rd2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                rectangle.setFill(Color.YELLOW);
            }
        });
        rd3.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                rectangle.setFill(Color.RED);
            }
        });
        rd4.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                rectangle.setFill(Color.GREEN);
            }
        });
        rd5.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                rectangle.setFill(Color.PURPLE);
            }
        });



        FlowPane pane = new FlowPane();
        pane.setAlignment(Pos.BASELINE_CENTER);
        pane.setVgap(10);
        pane.setHgap(10);
        pane.getChildren().addAll(rd1,rd2,rd3,rd4,rd5);
        pane.getChildren().add(rectangle);
        Scene scene = new Scene(pane,350,250);
        primaryStage.setScene(scene);

        primaryStage.setTitle("Fill Square");
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}

//Output

//If you need any help regarding this solution ....... please leave a comment ........ thanks

Add a comment
Know the answer?
Add Answer to:
Write a JavaFX application that allows the user to select a color out of five options...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I. User Interface Create a JavaFX application with a graphical user interface (GUI) based on the...

    I. User Interface Create a JavaFX application with a graphical user interface (GUI) based on the attached “GUI Mock-Up”. Write code to display each of the following screens in the GUI: A. A main screen, showing the following controls: • buttons for “Add”, “Modify”, “Delete”, “Search” for parts and products, and “Exit” • lists for parts and products • text boxes for searching for parts and products • title labels for parts, products, and the application title B. An add...

  • Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

    Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...

  • JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming...

    JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming and image handling using JavaFX. Create an application that responds to the user clicking command buttons. Initially, it will display one of two graphic images and, based upon the button clicked by the user, will switch the image displayed. If the user clicks the mouse button to display the same image as is currently displayed, the image is not changed, but a message at...

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • (Android Studio) Create a camera app that allows the user to upload a picture from their...

    (Android Studio) Create a camera app that allows the user to upload a picture from their phone camera gallery, see the picture via summary page, and edit the picture as well. Clicking on "Insert picture here from camera photo gallery" in the add page will allow the user to upload a picture from their existing camera photo gallery on their phone. The user can choose where to save the picture, starting with Picture 1-5. Once they choose where to save...

  • In Python Calculator Write a program that allows the user to enter two numbers and then...

    In Python Calculator Write a program that allows the user to enter two numbers and then adds, subtracts, divides or multiplies them when the user clicks on the appropriate button. Make sure you have a Quit button. Modification and requirements”: _____Window title should be  your name Calculate self.main_window.title("?????") _____Labels - change the attributes of the color, font, size and bold the answerExample self.label = tkinter.Label(self.main_window, \ text='?????????!', font=(" should Arial", 12, "bold"), fg="blue", bg="pink")Tip: choose a color from the...

  • you need to use javafx for following problem these instructions is for JavaFX program so you...

    you need to use javafx for following problem these instructions is for JavaFX program so you need to follow the instructions and according to images you have design the GUI Write a program to design the following interface and use event handling and File 10 to perform the given operation. Make sure you have a background image and different color for labels and buttons for this interface. Input Information Course Quiz Grade Assignment Grade Fal Grade Submit View Grade Info...

  • Write a JavaFX program to solve the following problem. Two buttons appear at the top side...

    Write a JavaFX program to solve the following problem. Two buttons appear at the top side by side. When the Park button is clicked, a small car drives from the right side onto the road and parks in the garage. The garage can hold five cars. When the Unpark button is clicked, the bottom car drops down from the garage, drives left, drives up, and drives right off the screen. A car is designed, minimally as rectangle on two circular...

  • Write a C++ console application that allows your user to capture rainfall statistics. Your program should...

    Write a C++ console application that allows your user to capture rainfall statistics. Your program should contain an array of 12 doubles for the rainfall values as well as a parallel array containing the names of the months. Using each of the month names, prompt your user for the total rainfall for that month. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT