Page 1 of 1

Must be written in Java and use std.Draw

Posted: Sun May 15, 2022 10:05 am
by answerhappygod
Must be written in Java and use std.Draw
Must Be Written In Java And Use Std Draw 1
Must Be Written In Java And Use Std Draw 1 (135.04 KiB) Viewed 58 times
Must Be Written In Java And Use Std Draw 2
Must Be Written In Java And Use Std Draw 2 (122.82 KiB) Viewed 58 times
Must Be Written In Java And Use Std Draw 3
Must Be Written In Java And Use Std Draw 3 (101.34 KiB) Viewed 58 times
Must Be Written In Java And Use Std Draw 4
Must Be Written In Java And Use Std Draw 4 (33.02 KiB) Viewed 58 times
Must Be Written In Java And Use Std Draw 5
Must Be Written In Java And Use Std Draw 5 (73.64 KiB) Viewed 58 times
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) Bouncing Balls.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 Std Draw.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)

Ball.java Bouncing Balls.java 1 e 2 main animation program that draws the bouncing balls in an infinite loop 3 4 5 import java.awt.Color; import java.util.ArrayList; 6 7 8 9 10 public class BouncingBalls { 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 11 12 13 14 15 //TODO: create ArrayList to store Ball objects and add one new Ball abject to start 16 17 18 Ball[] balls = new Ball[n]; for (int i = 0; i <n; i++) balls = new Ball(); 19 20 21 22 // main animation loop while (true) { //infinite loop! will repeat as long as we keep the program running // clear the background StdDraw.clear(); 23 24 25 26 //TODO: complete the animation drawing loop according to the instructions 27 28 for (int i = 0; i<n; i++) { ballslil.move(): 29

} 30 31 32 33 34 35 36 37 38 // draw the n balls StdDraw.clear(Std Draw.GRAY); StdDraw.setPenColor(StdDraw.BLACK); for (int i = 0; i<n; i++) { balls.draw(); } O 39 40 41 // copy offscreen buffer to onscreen StdDraw.show(); 42 43 44 45 // pause for 20 ms StdDraw.pause (20); 46 47 48 } } 49 50

1 2 3 Ball.java /* 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 o */ 5 6 7 8 Fimport bava.awt.Color; import java.util.Random; 9 10 11 12 13 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; 14 15 16 17 //TODO: create default constructor according to assignment instructions 18 19 20 //TODO: create "getter" and "setter" methods according to assignment instructions e } 21 22