Question

In: Computer Science

Write a JavaFX application that draws 5 squares. Use a random number generator to generate random...

Write a JavaFX application that draws 5 squares. Use a random number generator to generate random values for the size, x, and y. Make the size between 100 and 200, x between 0 and 600, y between 0 and 400. You can pick any color, but you must use different colors for the 5 squares. Set your window to 600 by 400.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// DrawSquares.java

import java.util.Random;

import javafx.application.Application;

import static javafx.application.Application.launch;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

import javafx.stage.Stage;

public class DrawSquares extends Application {

    //declaring needed measurements

    private static int WIDTH = 600, HEIGHT = 400, NUM_SQUARES = 5, SIZE_MIN = 100, SIZE_MAX = 200;

    @Override

    public void start(Stage primaryStage) {

        //creating a Group

        Group root = new Group();

        //creating a Random number generator

        Random random = new Random();

        //looping for NUM_SQUARES number of times

        for (int i = 0; i < NUM_SQUARES; i++) {

            //generating a random size between SIZE_MIN and SIZE_MAX

            int size = random.nextInt(SIZE_MAX - SIZE_MIN + 1) + SIZE_MIN;

            //creating a square at random x,y point and with size

            Rectangle sq = new Rectangle(random.nextInt(WIDTH), random.nextInt(HEIGHT), size, size);

            //generating a random color, here the first three values are r,g,b values between 0.0 and 1.0,

            //last value 1 is the alpha value (transparency)

            sq.setFill(new Color(random.nextDouble(), random.nextDouble(), random.nextDouble(), 1));

            //adding sq to root

            root.getChildren().add(sq);

        }

        //setting up a Scene and displaying it

        Scene scene = new Scene(root, WIDTH, HEIGHT);

        primaryStage.setScene(scene);

        primaryStage.setTitle("Squares");

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

/*OUTPUT*/



Related Solutions

Write a JavaFX application that draws 10 circles of random radius in random locations. Leave all...
Write a JavaFX application that draws 10 circles of random radius in random locations. Leave all circles unfilled except for the largest circle which should be filled with a translucent red (30% opaque) . If multiple circles have the same largest size, fill any one of them. Hint: keep track of the largest circle as you generate them, then change fill to red. Thankuou
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is...
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.
In Java, Write a JavaFX application that draws multiple circles using a rubberbanding technique. The circle...
In Java, Write a JavaFX application that draws multiple circles using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.Thank you and could you also show the finished product?
Question 1. Go to random.org. This website is a random number generator. Use it to generate...
Question 1. Go to random.org. This website is a random number generator. Use it to generate three numbers a, b, c between -10 and 10. Now let your a, b and c be the coefficients of the quadratic function f(x)=ax2 +bx+c. (For example, if the numbers you generated happened to be a = 2,b = 12, c = −1, your function for the rest of the question would bef(x) = 2x2 +12x−1.) (a) Put f(x) into “standard” or “vertex” formf(x)=a(x−h)2...
Collect the Data: Use a random number generator to generate 50 values between 0 and 1...
Collect the Data: Use a random number generator to generate 50 values between 0 and 1 (inclusive). Theoretical Distribution In words, X = The theoretical distribution of X is X ~ U(0, 1). In theory, based upon the distribution X ~ U(0, 1), find μ = __________ σ = __________ 1st quartile = __________ 3rd quartile = __________ median = __________ Construct a box plot of the data. Be sure to use a ruler to scale accurately and draw straight...
Write a JavaFX application that presents two buttons and a number (initially 50) to the user....
Write a JavaFX application that presents two buttons and a number (initially 50) to the user. Label the buttons Increment and Decrement. When the increment button is pushed, increment the displayed value. Likewise, decrement the value when the decrement button is pushed. This has to use Java.Fx and not Java.Awt
Java Write a JavaFX application that displays a button and a number. Every time the button...
Java Write a JavaFX application that displays a button and a number. Every time the button is pushed, change the number to a random value between 1 and 100. Thank you and can you show a picture of the result as well
A computer random number generator was used to generate 950 random digits from 0 to 9....
A computer random number generator was used to generate 950 random digits from 0 to 9. The observed frequencies of the digits are given in the table below. 0 1 2 3 4 5 6 7 8 9 88 82 97 84 87 87 95 93 90 147 Using a 0.05significance level, test the claim that all of the digits are equally likely. (a) Find the rejection region. Reject H0 if χ2> (b) Find the test statistic. (Round your final...
A computer random number generator was used to generate 550 random digits (0,1,...,9). The observed frequences...
A computer random number generator was used to generate 550 random digits (0,1,...,9). The observed frequences of the digits are given in the table below. 0 1 2 3 4 5 6 7 8 9 58 55 45 50 53 50 57 57 46 79 Test the claim that all the outcomes are equally likely using the significance level ?=0.05. The expected frequency of each outcome is E= The test statistic is ?2= The p-value is Is there sufficient evidence...
A computer random number generator was used to generate 750 random digits (0,1,...,9). The observed frequences...
A computer random number generator was used to generate 750 random digits (0,1,...,9). The observed frequences of the digits are given in the table below. 0 1 2 3 4 5 6 7 8 9 81 62 74 82 76 75 70 66 80 84 Test the claim that all the outcomes are equally likely using the significance level α=0.05α=0.05. The expected frequency of each outcome is E= The test statistic is χ2= The p-value is Is there sufficient evidence...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT