In: Computer Science
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);
}
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.