CS 142 Java Assignment 5 Colorful Bouncing Circle In class, we wrote the Circle class representing circle objects which have a radius, center x, and center y. It included overlaps and draw methods....












CS 142 Java Assignment 5



Colorful Bouncing Circle



In class, we wrote the Circle class representing circle objects which have a radius, center x, and center y. It included overlaps and draw methods. Then, we wrote a ColorfulCircle class which included a Color property and overrode the draw method of Circle. For this assignment, you are asked to create a ColorfulBouncingCircle class.Begin with the versions of Circle and ColorfulCircle uploaded to Canvas.


ColorfulBouncingCircle shouldextendColorfulCircle. Unlike the other Circle classes, it will include a velocity for the x and y axes. The constructor for ColorfulBouncingCircle includes these parameters:

publicColorfulBouncingCircle(doubleradius,doublecenterX,doublecenterY, Color color,doublexVelocity,doubleyVelocity)



Note that we are treating x as increasing to the right and y as increasing down, as with Java’s graphics coordinates system. This was also seen with Circle’s draw method.


ColorfulBouncingCircles will bounce around in a playing field which the tester will provide. The ColorfulBouncingCircle class should have a public static method to set the playing field width and height:



public static voidsetPlayingFieldSize(doublenewWidth,doublenewHeight)


ColorfulBouncingCircle will also include a method which should alter its centerX and centerY with each time tick (similar to a metronome’s noise guiding a musician to play a song). The circle’s x position should be altered according to x velocity and y position according to y velocity.However, before changing the positions, check if the center position would, after moving, be either less than 0 or greater than the playing field dimensions. If so,instead of changing the center position, alter the velocity. If the ball hits the top or bottom, the sign of its vertical velocity should be flipped; if it hits the left or right, its horizontal velocity sign should be flipped. If it hits a corner, both should be. Notice that velocity may be positive or negative! The tick method which will be called by the tester is defined in this way:



public voidtick()


Finally, we should override the overlaps method to make balls bounce away from each other. You should call the super method to see if your circle overlaps another circle. If so, alterthiscircle’s velocity according to its center relative to the other circle. If “this circle” is above or below the “other circle,” flip the sign of “this circle’s” vertical velocity. If it’s to the left or right, flip the horizontal velocity. As before, both may be flipped.This is NOT the same as a true elastic collision, but it should be simpler for you to implement! The sign flipping will sometimes cause the balls to “vibrate” when caught between each other. Please review the velocity changing rules carefully; they are easy to get wrong!!



To review: Write the ColorfulBouncingCircle class, where you should write the constructor, setPlayingFieldSize, and tick methods, and override the overlaps method, each according to the above descriptions. You should not have to write any code which draws; instead, a test program will be provided which will animate ColorfulBouncingCircles based on your implementation. For every time tick, the test program will compare every circle against every other circle using the overlaps method.The test program will ask you to press Enter in the console to launch the automated tests which will assign a tentative score based on your implementation. DO NOT ALTER the Circle OR ColorfulCircle files!!



Given Files


import java.awt.Graphics2D;


// UPDATE 3/8/2018: Added setCenterCoordinates method
public class Circle {
private double radius;
// X and Y are in Java graphics coordinate system
// (0, 0 in upper left corner)
private double centerX, centerY;
public Circle(double radius, double centerX, double centerY) {
if (radius

throw new IllegalArgumentException();
}
this.radius = radius;
this.centerX = centerX;
this.centerY = centerY;
}
public double getRadius() {
return radius;
}
public double getCenterX() {
return centerX;
}
public double getCenterY() {
return centerY;
}
public void setRadius(double radius) {
if (radius

throw new IllegalArgumentException();
}
this.radius = radius;
}
public void setCenterCoordinates(double centerX, double centerY) {
this.centerX = centerX;
this.centerY = centerY;
}
public double area() {
return Math.PI * radius * radius;
}
public boolean overlaps(Circle c) {
// First, calculate distance
double dx = this.centerX - c.centerX;
double dy = this.centerY - c.centerY;
double dist = Math.sqrt(dx*dx + dy*dy);
return dist

}



public void draw(Graphics2D g) {
g.fillOval((int)(centerX-radius),
(int)(centerY-radius),
(int)(2*radius),
(int)(2*radius));
}
}



And


import java.awt.Color;
import java.awt.Graphics2D;


public class ColorfulCircle extends Circle {



private Color color;
public ColorfulCircle(double radius, double centerX, double centerY, Color color) {
super(radius, centerX, centerY);
this.color = color;
}



// The Override annotation below is not necessary, but it ensures that this method
// correctly matches up with a method in Circle or another superclass.
@Override
public void draw(Graphics2D g) {
Color c = g.getColor();
g.setColor(color);
super.draw(g);
g.setColor(c);
}


}

Mar 21, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here