For this assignment, you are asked to create a ColorfulBouncingCircle class.  ColorfulBouncingCircle should extend ColorfulCircle. Unlike the other Circle classes, it will include a velocity for the x and y axes. The constructor for ColorfulBouncingCircle includes these parameters: public ColorfulBouncingCircle(double radius, double centerX, double centerY, Color color, double xVelocity, double yVelocity) 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 void setPlayingFieldSize(double newWidth, double newHeight) 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 void tick() 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, alter this circle’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!!   Colorful Circle Code : 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); } }Circle code: 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 < 0) { 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 < 0) { 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 <= this.radius + c.radius; } public void draw(Graphics2D g) { g.fillOval((int)(centerX-radius), (int)(centerY-radius), (int)(2*radius), (int)(2*radius)); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 For this assignment, you are asked to create a ColorfulBouncingCircle class. 

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

public ColorfulBouncingCircle(double radius, double centerX, double centerY, Color color, double xVelocity, double yVelocity)

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 void setPlayingFieldSize(double newWidth, double newHeight)

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 void tick()

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, alter this circle’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!!

 

Colorful Circle Code :

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);
}
}Circle code:

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 < 0) {
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 < 0) {
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 <= this.radius + c.radius;
}
public void draw(Graphics2D g) {
g.fillOval((int)(centerX-radius),
(int)(centerY-radius),
(int)(2*radius),
(int)(2*radius));
}
}

 

 

 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education