Question

In: Computer Science

Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels,...

Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels, filled with a random color at a random location on the screen. When you click the circle, it disappears and a new random color circle is displayed at another random location (see display below). After twenty circles are clicked, display the time spent in the pane. To detect whether a point is inside the circle, use the contains method defined in the Node class. You can capture the initial starting time with a statement like:

startTime = System.currentTimeMillis()

Solutions

Expert Solution

/*Java program named CircleZapper that displays a circle with a radius of 10 pixels, filled with a random color at a random location on the screen. When you click the circle, it disappears and a new random color circle is displayed at another random location (see display below). After twenty circles are clicked, display the time spent in the pane. To detect whether a point is inside the circle, use the contains method defined in the Node class. You can capture the initial starting time with a statement like:
startTime = System.currentTimeMillis()
*/


import ToolKit.StopWatch;
import javafx.application.Application;
import javafx.scene.*;
import javafx.stage.Stage;

public class CircleZapper extends Application {

static int crlCount = 0;

@Override
public void start(Stage primaryStage) {

double paneWidth = 500;
double paneHeight = 500;
  
   // Create an object of circle
Circle crl = new Circle(0, 0, 10);
updateCircle(crl);
Pane pane = new Pane(crl);
Text count = new Text(50,50,crlCount + "");
pane.getChildren().add(count);
StopWatch timer = new StopWatch();

   // Create and register the handle
crl.setOnMouseClicked(e-> {

if (!timer.isOn()) {
timer.start();
       startTime = System.currentTimeMillis();
}
if (crlCount < 19) {
crlCount++;
count.setText(crlCount + "");

updateCircle(crl);
} else {
timer.stop();
       endTime = System.currentTimeMillis();
pane.getChildren().remove(crl);
      
pane.getChildren().add(new Text(paneWidth / 2, paneHeight / 2, "Total Time spent is " + timer.getElapsedTime() + " milliseconds"));
       pane.getChildren().add(new Text(paneWidth / 2, paneHeight / 2, "Total Time spent is " + (startTime - endTime) + " milliseconds"));
      
}
});

   // Create a scene and place it in the stage
primaryStage.setScene(new Scene(pane, paneWidth, paneHeight));
   primaryStage.setTitle("CircleZapper");
primaryStage.show();
}

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

// FUnction to update the circle with randon color and place at random location
private void updateCircle(Circle c) {

double min = c.getRadius() + 5;
double max = 500 - c.getRadius() - 5;

c.setCenterX((Math.random() * (max - min) + min));
max = 500 - c.getRadius() - 5;
c.setCenterY((Math.random() * (max - min) + min));

c.setFill(new Color(Math.random(), Math.random(), Math.random(), 1));
}


}


Related Solutions

(Display a Circle and Its Attributes) Write a program that displays a circle of random size...
(Display a Circle and Its Attributes) Write a program that displays a circle of random size and calculates and displays the area, radius, diameter and circumference. Use the following equations: diameter = 2 × radius, area = π × radius2, circumference = 2 × π × radius. Use the constant Math.PI for pi (π). All drawing should be done on a subclass of JPanel, and the results of the calculations should be displayed in a read-only JTextArea.
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
In java processing 3, write a program that draws a circle. The center of the circle...
In java processing 3, write a program that draws a circle. The center of the circle would be first point on the canvas where the mouse is pressed on it. While the mouse is pressed, the circle can be drawn by moving the mouse cursor farther than the first point that mouse was pressed (circle center). As soon as the code is run, a timer at the bottom of the canvas should start working. Also, the radius of the circle...
Java Programming Write a program that displays the following pattern *                         *       &nbsp
Java Programming Write a program that displays the following pattern *                         *          *          * *          *          *          *          *          *          *          *          *          *          *          *             *          *          *          *          *                         *          *          *                                     * Printing Pattern A * ** *** **** ***** ****** ******* Printing Pattern B ******* ****** ***** **** *** ** * Printing Pattern C * ** *** **** ***** ****** *******
Write a program named MakeChange that calculates and displays the conversion of an entered number of...
Write a program named MakeChange that calculates and displays the conversion of an entered number of dollars into currency denominations—twenties, tens, fives, and ones. For example, if 113 dollars is entered, the output would be twenties: 5 tens: 1 fives: 0 ones: 3. Answer in C# (P.S i'm posting the incomplete code that i have so far below.) using System; using static System.Console; class MakeChange { static void Main() { int twenties, tens, fives, ones; WriteLine("Enter the number of dollars:");...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
Write a JAVA program that displays a table of the Celsius temperatures and their Fahrenheit equivalents....
Write a JAVA program that displays a table of the Celsius temperatures and their Fahrenheit equivalents.  The formula for converting a temperature from Celsius to Fahrenheit is F = 9/ 5 C + 32 where F → Fahrenheit temperature C → Celsius temperature.  Allow the user to enter the range of temperatures in Celsius to be converted into Fahrenheit.  Your program must use a loop to display the values of the temperature conversions (see sample output). Sample...
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT