Answered step by step
Verified Expert Solution
Link Copied!

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 =10;
static Color defaultColor = Color.yellow;
static Rectangle defaultBox = new Rectangle(0,0,100,100);
private int x, y;
private int dx, dy;
private int diameter;
private Color color;
private Rectangle box;
private Object ball;
private Ball ball1;
public Ball( int x0, int y0, int dx0, int dy0){
x = x0;
y = y0;
dx = dx0;
dy = dy0;
color = defaultColor;
diameter = defaultDiameter;
}
public void setColor( Color c ){
color = c;
}
public void setBoundingBox( Rectangle r ){
box = r;
}
public void paint( Graphics g ){
g.setColor( color );
g.fillOval( x, y, diameter, diameter );
}
void constrain(){
int x0= box.x;
int y0= box.y;
int x1= x0+ box.width - diameter;
int y1= y0+ box.height - diameter;
if (x < x0){
dx = Math.abs(dx);
}
if (x > x1){
dx =-Math.abs(dx);
}
if (y < y0){
dy = Math.abs(dy);
}
if (y > y1){
dy =-Math.abs(dy);
}
}
public void action(){
x = x + dx;
y = y + dy;
constrain();
}
public void setDiameter(int d){
diameter = d;
}
}
class BallPanel extends JPanel implements ActionListener {
private int width, height;
private Ball ball;
private Ball ball2;
private Timer timer = new Timer(50, this);
public BallPanel(int width, int height){
this.width = width;
this.height = height;
ball = new Ball(width /10, height /5,5,5);
ball2= new Ball(width /10, height /5,6,6);
ball.setBoundingBox(new Rectangle(0,0, width, height));
ball.setColor(Color.WHITE);
timer.start();
ball2.setColor(Color.RED);
ball2.setBoundingBox(new Rectangle(0,0, width, height));
}
public void paintComponent(Graphics g){
g.setColor(Color.black);
g.fillRect(0,0, width, height);
ball.paint(g);
ball2.paint(g);
}
public void actionPerformed(ActionEvent e){
if (width != getWidth()|| height != getHeight())
wasResized(getWidth(), getHeight());
ball.action();
ball2.action();
repaint();
}
public void wasResized(int newWidth, int newHeight){
width = newWidth;
height = newHeight;
ball.setBoundingBox( new Rectangle(0,0, width, height ));
ball2.setBoundingBox( new Rectangle(0,0, width, height ));
}
}
class BallWorld extends JFrame {
private BallPanel panel = new BallPanel (180,180);
public BallWorld (){
Container c = getContentPane();
c.add(panel, BorderLayout.CENTER);
setSize(200,200);
setLocation(100,100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
BallWorld world = new BallWorld();
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

Describe the concept of channel length.

Answered: 1 week ago