Question: import java.awt. * ; import java.awt.event. * ; import javax.swing. * ; class Ball { static int defaultDiameter = 1 0 ; static Color defaultColor
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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
