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

The following code creates a bouncing ball animation using GUIs in Java. Please

ID: 3813693 • Letter: T

Question

The following code creates a bouncing ball animation using GUIs in Java. Please edit the code so that it creates multiple balls in the animation rather than just one. Please also comment throughout the code.

//BallPane.java

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;

public class BallPane extends Pane {
   public final double radius = 20;
   private double x=radius, y=radius;
   private double dx=1, dy=1;
   private Circle circle = new Circle (x,y,radius);
   private Timeline animation;
  
   public BallPane(){
circle.setFill (Color.GREEN);
getChildren().add(circle);
animation = new Timeline (new KeyFrame(Duration.millis(10), e->moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
   }
   public void play (){
animation.play();
   }
   public void pause (){
animation.pause();
   }
   public void increaseSpeed(){
animation.setRate(animation.getRate()+0.1);
   }
   public void decraseSpeed(){
animation.setRate (animation.getRate() > 0 ? animation.getRate()- 0.1 : 0);
   }
   public DoubleProperty rateProperty(){
return animation.rateProperty();
   }
   protected void moveBall(){
if (y<radius||x>getWidth()-radius){
   dx*=-1;
}
x+=dx;
y+=dy;
circle.setCenterX(x);
circle.setCenterY(y);
   }
}

//BounceBallControl1.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.KeyEvent;

public class BounceBallControl1 extends Application {
   private BallPane ballPane = new BallPane();
  
   @Override
   public void start(Stage primaryStage){
  
Scene scene = new Scene(ballPane, 250, 150);
scene.setOnMousePressed(this::processMousePressed);
scene.setOnMouseReleased(this::processMouseReleased);
scene.setOnKeyPressed(this::processKeyPress);

primaryStage.setTitle("Bounce Ball Control");
primaryStage.setScene(scene);
primaryStage.show();

ballPane.requestFocus();
   }
   public void processMousePressed(MouseEvent e){
ballPane.pause();
   }
   public void processMouseReleased(MouseEvent e){
ballPane.play();
   }
   public void processKeyPress(KeyEvent e){
if (e.getCode()==KeyCode.UP){
   ballPane.increaseSpeed();
}
else if (e.getCode()==KeyCode.DOWN){
   ballPane.decreaseSpeed();
}
   }
   public static void main (String[]args){
launch(args);
   }
}

Explanation / Answer

private Ball ball; protected List<Ball> balls = new ArrayList<Ball>(20); private Container container; private DrawCanvas canvas; private int canvasWidth; private int canvasHeight; public static final int UPDATE_RATE = 30; int x = random(480); int y = random(480); int speedX = random(30); int speedY = random(30); int radius = random(20); int red = random(255); int green = random(255); int blue = random(255); int count = 0; public static int random(int maxRange) { return (int) Math.round((Math.random() * maxRange)); } public BouncingBalls(int width, int height){ canvasWidth = width; canvasHeight = height; ball = new Ball(x, y, speedX, speedY, radius, red, green, blue); container = new Container(); canvas = new DrawCanvas(); this.setLayout(new BorderLayout()); this.add(canvas, BorderLayout.CENTER); this.addMouseListener(this); } public void start(){ Thread t = new Thread(){ public void run(){ while(true){ update(); repaint(); try { Thread.sleep(1000 / UPDATE_RATE); } catch (InterruptedException e) {} } } }; t.start(); } public void update(){ ball.move(container); } class DrawCanvas extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); container.draw(g); ball.draw(g); } public Dimension getPreferredSize(){ return(new Dimension(canvasWidth, canvasHeight)); } } public static void main(String[] args){ javax.swing.SwingUtilities.invokeLater(new Runnable(){ public void run(){ JFrame f = new JFrame("Bouncing Balls"); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); f.setContentPane(new BouncingBalls(500, 500)); f.pack(); f.setVisible(true); } }); } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { count++; balls.add(new Ball(x, y, speedX, speedY, radius, red, green, blue)); balls.get(count-1).start(); start(); } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote