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 Java program that reads a name and displays on the screen.
Write a Java program that reads a name and displays on the screen.
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 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.
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT