Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help to add ( Java code ) methods so the balls alternately grow and shrink. Help with determine the minimum size as well as
Please help to add Java code methods so the balls alternately grow and shrink. Help with determine the minimum size as well as the maximum size of balls. Also need to add an instance variable to the Ball class that indicates whether the ball is currently shrinking or growing. This variable can be of type boolean. Furthermore, need help to create code that changes the value of the variable when the ball reaches its minimum size and maximum size.
Also help with modify the action method to give the ball the pulsating property.
Please add the complement code in the below code and send the code back in whole with the changes in it
Thanks!
import java.awt.;
import java.awt.event.;
import javax.swing.;
class Ball
static int defaultDiameter ;
static Color defaultColor Color.yellow;
static Rectangle defaultBox new Rectangle;
private int x y;
private int dx dy;
private int diameter;
private Color color;
private Rectangle box;
private Object ball;
private Ball ball;
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;
public void setDiameterint d
diameter d;
class BallPanel extends JPanel implements ActionListener
private int width, height;
private Ball ball;
private Ball ball;
private Timer timer new Timer this;
public BallPanelint width, int height
this.width width;
this.height height;
ball new Ballwidth height ;
ball new Ballwidth height ;
ball.setBoundingBoxnew Rectangle width, height;
ball.setColorColorWHITE;
timer.start;
ballsetColorColorRED;
ballsetBoundingBoxnew Rectangle width, height;
public void paintComponentGraphics g
gsetColorColorblack;
gfillRect width, height;
ball.paintg;
ballpaintg;
public void actionPerformedActionEvent e
if width getWidth height getHeight
wasResizedgetWidth getHeight;
ball.action;
ballaction;
repaint;
public void wasResizedint newWidth, int newHeight
width newWidth;
height newHeight;
ball.setBoundingBox new Rectangle width, height ;
ballsetBoundingBox new Rectangle width, height ;
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