Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help with add the the code for the tasks below in the Java code below, so the below code perfoms the tasks. Thanks! Task
Please help with add the the code for the tasks below in the Java code below,
so the below code perfoms the tasks. Thanks!
Task
Compile the program with javac BallWorld.java and test it with java BallWorld. Describe in a few words what is happening.
Task
Change the color of the ball to eg white without changing the constructor Ball where the default color is set. However, you need to make a call to a method that sets the color of a ball. You must figure out which call and where in the code it should be performed yourself. Describe in a few words what changes you made.
Task
A "bounding box" is a rectangle that indicates the boundaries within which the ball bounces. If you resize the window, you will find that the area within which the ball bounces is unchanged. The wasResized method is called when the window size changes. Modify wasResized so that the "bounding box" changes to its correct size. Look elsewhere in the code to get an idea of which method is intended and how it should be used. Describe the changes you made.
Task
In the Ball class there are methods to change the color and "bounding box", but there is no method to set the ball's size diameter Add such a method and then use it to resize the ball. Describe the changes you made.
Task
Put in an extra ball of a different color, size and speed so that there are a total of two bouncing balls. Describe the changes you made.
Task Pulsars
Let the balls alternately grow and shrink. For this you need to determine the minimum size as well as the maximum size of balls these limits are chosen wisely You also need to add an instance variable to the Ball class that indicates whether the ball is currently shrinking or growing. This variable must be of type boolean. Furthermore, you need code that changes the value of the variable when the ball reaches its minimum size and maximum size.
Modify the action method to give the ball the pulsating property. Describe the changes you made. import java.awt.;
import java.awt.event.;
import javax.swing.;
class Ball
static int defaultDiameter ;
static Color defaultColor Color.white;
static Rectangle defaultBox new Rectangle;
private int x y;
private int dx dy;
private int diameter;
private Color color;
private Rectangle box;
public Ball int x int y int dx int dy
x x;
y y;
dx dx;
dy dy;
color defaultColor;
diameter defaultDiameter;
public void setColor Color c
color c;
public void setBoundingBox Rectangle r
box r;
public void paint Graphics g
gsetColor color ;
gfillOval x y diameter, diameter ;
void constrain
int x box.x;
int y box.y;
int x x box.width diameter;
int y y box.height diameter;
if x x
dx Math.absdx;
if x x
dx Math.absdx;
if y y
dy Math.absdy;
if y y
dy Math.absdy;
public void action
x x dx;
y y dy;
constrain;
class BallPanel extends JPanel implements ActionListener
private int width, height;
private Ball ball;
private Timer timer new Timer this;
public BallPanel int width, int height
this.width width;
this.height height;
ball new Ball width height ;
ball.setBoundingBox new Rectangle width, height ;
timer.start;
public void paintComponent Graphics g
gsetColor Color.black ;
gfillRect width, height ;
ball.paint g ;
public void actionPerformedActionEvent e
ifwidth getWidth height getHeight
wasResizedgetWidthgetHeight;
ball.action;
repaint;
public void wasResized int newWidth, int newHeight
width newWidth;
height newHeight;
class BallWorld extends JFrame
private BallPanel panel new BallPanel ;
public BallWorld
Container c getContentPane;
caddpanel BorderLayout.CENTER;
setSize;
setLocation;
setVisibletrue;
setDefaultCloseOperationEXITONCLOSE;
public static void mainString args
BallWorld world new BallWorld;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started