Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

hi this is my code but i dont know how to make make the ball chaange color every

ID: 3769669 • Letter: H

Question

hi this is my code but i dont know how to make make the ball chaange color every time it is clicked??

import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;

public class BallMover extends Application {

// Global variables for the application.
Timeline gameTimeLine; // TimeLine object to allow animations
final int HEIGHT = 500; // size of game board
final int WIDTH = 500; // size of game board
Circle myCircle; // ball
int circleSize = 20;
Color[] ballColors = {Color.BLUE, Color.RED, Color.GREEN, Color.PURPLE, Color.BLACK};
int circleRadius = 30; // size of ball
double speed = 1; // speed multiplier of the ball
double directionX = 1.0; // direction x that ball is moving
double directionY = 1.0; // direction y that ball is moving
Button btnGameStart = new Button(); // button to run the game
Label lblClickCount;
int clickCount = 0; // keep track of the clicks
Timeline tlMover;
Timeline rectMover;
double rotationAngle;
Rectangle rectToMove;
double motionX = 1;
double motionY = 1;

@Override
public void start(Stage primaryStage) {
// get GUI layout
// gameboard area
VBox root = new VBox();
Pane gameBoard = new Pane();
addGameBoardBorder(gameBoard);
addBall(gameBoard);
root.getChildren().add(gameBoard);
// control area
HBox gameControls = new HBox();
addControls(gameControls);
root.getChildren().add(gameControls);
Scene myScene = new Scene(root, WIDTH, HEIGHT + 50);

primaryStage.setScene(myScene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

/// Add game board.
private void addGameBoardBorder(Pane root) {

rectToMove = new Rectangle(0, 0, 500, 500);
rectToMove.setFill(Color.WHITE);
rectToMove.setStroke(Color.BLUE);
root.getChildren().add(rectToMove);
myCircle = new Circle(250, 250, circleSize, Color.RED);
root.getChildren().add(myCircle);
}

/// Add ball to the board
private void addBall(Pane root) {
myCircle.setOnMouseReleased((MouseEvent event) -> {
// handle ball click event
if (gameTimeLine == null) {
startGame();
}
// move ball in a new random direction
Math.random(directionX);
// increase the speed of the ball with every click
});
}

// Add the game controls to HBox
private void addControls(HBox gameControls) {
//Add game button
btnGameStart = new Button("Start!");
gameControls.getChildren().add(btnGameStart);

btnGameStart.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent evt) {
startGame();

}
});

// Add text to keep track of clicks
}

/// set all variables to start the game and start the ball
/// rolling.
public void startGame() {
// set default values

// create timeline animation to move ball around.
gameTimeLine = new Timeline();
gameTimeLine.setCycleCount(Timeline.INDEFINITE);
gameTimeLine.getKeyFrames().add(new KeyFrame(Duration.millis(100),
new TimerHandler()));
gameTimeLine.playFromStart();
}

// Create event handler class for game timeline.
private class TimerHandler implements EventHandler<ActionEvent> {

@Override
public void handle(ActionEvent mvt) {
// see if ball has left the gameboard
// if Game Over; stop the game
myCircle.setCenterX(myCircle.getCenterX() + motionX * directionX);
myCircle.setCenterY(myCircle.getCenterY() + motionY * directionY);
myCircle.setRadius(circleSize);
// move the ball to the next location
}
}
}

Explanation / Answer

// To change the color I use the setFill() function.

// To choose a random color everytime I use a Random Function.

import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.Random;


public class BallMover extends Application {

// Global variables for the application.
Timeline gameTimeLine; // TimeLine object to allow animations
final int HEIGHT = 500; // size of game board
final int WIDTH = 500; // size of game board
Circle myCircle; // ball
int circleSize = 20;
Color[] ballColors = {Color.BLUE, Color.RED, Color.GREEN, Color.PURPLE, Color.BLACK};
int circleRadius = 30; // size of ball
double speed = 1; // speed multiplier of the ball
double directionX = 1.0; // direction x that ball is moving
double directionY = 1.0; // direction y that ball is moving
Button btnGameStart = new Button(); // button to run the game
Label lblClickCount;
int clickCount = 0; // keep track of the clicks
Timeline tlMover;
Timeline rectMover;
double rotationAngle;
Rectangle rectToMove;
double motionX = 1;
double motionY = 1;

@Override
public void start(Stage primaryStage) {
// get GUI layout
// gameboard area
VBox root = new VBox();
Pane gameBoard = new Pane();
addGameBoardBorder(gameBoard);
addBall(gameBoard);
root.getChildren().add(gameBoard);
// control area
HBox gameControls = new HBox();
addControls(gameControls);
root.getChildren().add(gameControls);
Scene myScene = new Scene(root, WIDTH, HEIGHT + 50);

primaryStage.setScene(myScene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

/// Add game board.
private void addGameBoardBorder(Pane root) {

rectToMove = new Rectangle(0, 0, 500, 500);
rectToMove.setFill(Color.WHITE);
rectToMove.setStroke(Color.BLUE);
root.getChildren().add(rectToMove);
myCircle = new Circle(250, 250, circleSize, Color.RED);
root.getChildren().add(myCircle);
}

/// Add ball to the board
private void addBall(Pane root) {
myCircle.setOnMouseReleased((MouseEvent event) -> {
// handle ball click event
if (gameTimeLine == null) {
startGame();
}
Random randomGenerator = new Random();
int index = randomGenerator.nextInt(5);
Color c = ballColors[index];
myCircle.setFill(c);
// move ball in a new random direction
// Math.random(directionX);
// increase the speed of the ball with every click
});
}

// Add the game controls to HBox
private void addControls(HBox gameControls) {
//Add game button
btnGameStart = new Button("Start!");
gameControls.getChildren().add(btnGameStart);

btnGameStart.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent evt) {
startGame();

}
});

// Add text to keep track of clicks
}

/// set all variables to start the game and start the ball
/// rolling.
public void startGame() {
// set default values

// create timeline animation to move ball around.
gameTimeLine = new Timeline();
gameTimeLine.setCycleCount(Timeline.INDEFINITE);
gameTimeLine.getKeyFrames().add(new KeyFrame(Duration.millis(100),
new TimerHandler()));
gameTimeLine.playFromStart();
}

// Create event handler class for game timeline.
private class TimerHandler implements EventHandler<ActionEvent> {

@Override
public void handle(ActionEvent mvt) {
// see if ball has left the gameboard
// if Game Over; stop the game
myCircle.setCenterX(myCircle.getCenterX() + motionX * directionX);
myCircle.setCenterY(myCircle.getCenterY() + motionY * directionY);
myCircle.setRadius(circleSize);
// move the ball to the next location
}
}
}