Please Help For this assignment you will create a bouncing balls animation (very similar to the BouncingBall.java sample

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Please Help For this assignment you will create a bouncing balls animation (very similar to the BouncingBall.java sample

Post by answerhappygod »

Please Help
For this assignment you will create a bouncing balls animation
(very similar to the BouncingBall.java sample program). To complete
the assignment you will complete and submit two files:
1) Ball.java - this file will contain a class that defines the
attributes and methods for a single Ball object that can be drawn
in the animation program
2) BouncingBalls.java - this file will contain the main program
that draws the animation of the Ball objects in an infinite
loop
Requirements for Ball.java:
+ constructor method that sets the initial position of the
center to the coordinates (0,0) in the window (Note: we are
assuming a window that ranges from -1 to 1 in both the x and y
direction) and sets the velocity, radius, and color to random
values. You can decide on a reasonable range for these random
values, but remember that the window only ranges from -1 to 1, so
the values need to be small enough for the Ball to fit in the
window and not move too "fast" (try values <0.1 for the radius
and <0.03 for the velocity components). You can also decide on
which colors to pick from, but your code should select from at
least three different colors randomly for the color of the
Ball.

+ "getter" and "setter" methods that allow retrieving and changing
the Ball objects center coordinates and velocity components. all
together, there should be 8 of these methods in total. Note:
IntelliJ can automatically generate these methods for you.

+ "getter" methods that return the radius and color of the Ball
object.
Requirements for BouncingBalls.java:
+ using the given file, complete the program to draw the
animation

+ to start, the program should have one Ball added to an ArrayList
of Ball objects that is created and drawn to bounce around the
window. when the ball "hits" one of the sides, it should reflect
and continue moving, as in the BouncingBall sample program

+ if the user presses the mouse button anywhere on the window (use
StdDraw.isMousePressed() to check for this in the animation loop),
generate a new Ball and add it to your ArrayList. thus, the loop
should draw every Ball object in the ArrayList every time through
the loop. if the user keeps pressing the mouse button, more and
more Ball objects should be created, added to the ArrayList and
drawn in the window to bounce around. note: it's ok if a bunch of
balls get added with a single mouse press (a single press actually
will be registered multiple times if you put it in a loop because
of the speed of the program execution)
PreviousNext
/*
definition for a single Ball object
a Ball can be created on the drawing window and will move and
"bounce"
each ball has a position (indicated by (x,y) coordinates of its
center, a velocity (in x and y components),
a radius, and a color
*/
import java.awt.Color;
import java.util.Random;
public class Ball {
//private instance variables that describe attributes of this Ball
object
private double centerX, centerY; //coordinates of the center of
this Ball
private double velocityX, velocityY; //x & y components of the
velocity of this Ball
private double radius;
private Color ballColor;
//TODO: create default constructor according to assignment
instructions
//TODO: create "getter" and "setter" methods according to
assignment instructions
}
/*
Bouncing Ball sample program
Implementation of a 2-d bouncing ball in the box from (-1, -1) to
(1, 1).
*/
import java.awt.Color; //used for the color of the ball
public class BouncingBall {
public static void main(String[] args) {
// set the scale of the coordinate system
StdDraw.setXscale(-1.0, 1.0);
StdDraw.setYscale(-1.0, 1.0);
StdDraw.enableDoubleBuffering(); //makes animation more
efficient
// initial values (just some sample numbers)
double rx = -0.480, ry = 0.860; // position (x and y coordinates of
the center of the ball)
double vx = 0.015, vy = -0.023; // velocity (x and y
components)
double radius = 0.05; // radius of the ball
StdDraw.setPenColor(StdDraw.RED); //will draw the ball with red
color
// main animation loop
while (true) { //infinite loop! will repeat as long as we keep the
program running
// bounce off wall according to law of elastic collision
if (Math.abs(rx + vx) > (1.0 - radius)) { //hit right or left of
window
vx = -vx; //change vx to opposite direction (moving left if it was
moving right, and vice versa)
}
if (Math.abs(ry + vy) > (1.0 - radius)) { //hit top or bottom of
window
vy = -vy; //change vy to opposite direction (moving down if it was
moving up, and vice versa)
}
// update position
rx = rx + vx; //add x component of velocity to current x position
to "move" it
ry = ry + vy; //add y component of velocity to current y position
to "move" it
// clear the background
StdDraw.clear();
// draw ball on the screen
StdDraw.filledCircle(rx, ry, radius); //draw the ball at the new
position
// copy offscreen buffer to onscreen
StdDraw.show();
// pause for 20 ms
StdDraw.pause(20);
}
}
}
Please Help
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply