Question

In: Computer Science

Write a JavaFX application that presents a button and a circle. Every time the button is...

Write a JavaFX application that presents a button and a circle. Every time the button is pushed, the circle should be moved to a new random location within the window.

This must only be one .java file. I cannot use

import javafx.geometry.Insets;

import javafx.scene.canvas.Canvas;

import javafx.scene.canvas.GraphicsContext;

import javafx.scene.layout.HBox;

This is what I have so far:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class CircleJumper extends Application
{
public double circlePositionX = 300;
public double circlePositionY = 300;
  
@Override
   public void start(Stage primaryStage)
{
Circle circle = new Circle(circlePositionX, circlePositionY, 70);
circle.setFill(Color.BLUE);

//create button to create new circle
Button push = new Button("PRESS!");
push.setOnAction(this::processButtonPress);
  
  
Group root = new Group(circle, push);
Scene Scene = new Scene(root, 700, 500, Color.WHITE);
  
       primaryStage.setTitle("Circle Jumper"); // Set the stage title
       primaryStage.setScene(Scene); // Place the scene in the stage
       primaryStage.show(); // Display the stage
   }
  
public void processButtonPress(ActionEvent event)
{   
Circle circle = new Circle(Math.random(), Math.random(), 50);
circle.setFill(Color.BLUE);
Group root = new Group(circle);
Scene Scene = new Scene(root, 700, 500, Color.WHITE);
}
  
public static void main(String[] args)
{
launch(args);
}

Solutions

Expert Solution

Code


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class CircleJumper extends Application
{
public double circlePositionX = 300;
public double circlePositionY = 300;
private double newY, newX = 0;
public Circle circle = new Circle(circlePositionX, circlePositionY, 70);
  
@Override
public void start(Stage primaryStage)
{
  
circle.setFill(Color.BLUE);
//create button to create new circle
Button push = new Button("PRESS!");
push.setOnAction(this::processButtonPress);
  
  
Group root = new Group(circle, push);
Scene Scene = new Scene(root, 700, 500, Color.WHITE);
  
primaryStage.setTitle("Circle Jumper"); // Set the stage title
primaryStage.setScene(Scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
  
public void processButtonPress(ActionEvent event)
{   
int max = 500;
int min = 1;
int range = max - min + 1;
newX = (Math.random() * range) + min;
newY = (Math.random() * range) + min;
circle.setCenterY(newY);
circle.setCenterX(newX);
}
  
public static void main(String[] args)
{
launch(args);
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

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
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.
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
javaFX is there a way to change the text of a button and then that button...
javaFX is there a way to change the text of a button and then that button repeats to actions of the first button
JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player...
JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player should be 800 pixels in width and 650 pixels in height. The media player should have a TextField that allows the user to type the full path of the .mp3 file to be played. The application should also use 2 Checkbox controls to show/hide the total playing time and the status (methods of MediaPlayer class). Use 2 RadioButton controls to change background colors (red...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
Write a JavaFX application that displays a Label containing the opening sentence or two from your...
Write a JavaFX application that displays a Label containing the opening sentence or two from your favorite book. Save the project as FXBookQuote1a. Add a button to the frame in the FXBookQuote program. When the user clicks the button, display the title of the book that contains the quote in a second label. Save the project as FXBookQuote1b // FXBookQuote1a.java import javafx.application.Application; import static javafx.application.Application.launch; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.Pane; import javafx.scene.text.Font; import javafx.stage.Stage; public class FXBookQuote1a...
Using NetBeans IDE, write a JavaFX application that allows theuser to choose insurance options. Use...
Using NetBeans IDE, write a JavaFX application that allows the user to choose insurance options. Use a ToggleGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). Use CheckBoxes for dental insurance and vision insurance options; the user can select one option, both options, or neither option. As the user selects each option, display its name and price in a text field; the HMO costs $200 per month, the...
(Not in Swing) Write a JavaFX application that creates polyline shapes dynamically using mouse clicks. Each...
(Not in Swing) Write a JavaFX application that creates polyline shapes dynamically using mouse clicks. Each mouse click adds a new line segment to the current polyline from the previous point to the current mouse position. Allow the user to end the current polyline with the double click. Also, provide a button that clears the window and allows the user to begin again.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT