Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simulates a bouncing ball. Extend to allow mulitple balls. You can use the +I or

ID: 3549764 • Letter: S

Question

Simulates a bouncing ball. Extend to allow mulitple balls. You can use the +I or -I button to increase or decrease the number of the balls and use teh Suspend and Resume buttoms to freeze the balls or resume bouncing. For each ball, assign a random color.

I am having trouble getting my program to run. Here is what I came up with:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class BouncingBalls extends JApplet {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame();
JApplet applet = new BouncingBalls();

frame.add(applet);

frame.setTitle("BouncingBalls");

frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(3);

frame.setSize(400,200);
frame.setVisible(true);
}
public BouncingBalls()
{

add(new BallControl());
}
class BallControl extends JPanel implements ActionListener, AdjustmentListener
{
private Ball ball;
private JButton jbtSuspend;
private JButton jbtResume;
private JButton jbtAdd;
private JButton jbtSubtract;
private JScrollBar jsbDelay;

public BallControl()
{
ball = new Ball();

jbtSuspend = new JButton("Suspend");

jbtResume = new JButton("Resume");

jbtAdd = new JButton("+1");


jbtSubtract = new JButton("-1");
jsbDelay = new JScrollBar();
JPanel panel = new JPanel();

panel.add(jbtSuspend);

panel.add(jbtResume);

panel.add(jbtAdd);

panel.add(jbtSubtract);

ball.setBorder(new LineBorder(Color.red));
jsbDelay.setOrientation(0);
ball.setDelay(jsbDelay.getMaximum());
setLayout(new BorderLayout());
add(jsbDelay, "North");
add(ball, "Center");
add(panel, "South");
jbtSuspend.addActionListener(this);
jbtResume.addActionListener(this);
jbtAdd.addActionListener(this);
jbtSubtract.addActionListener(this);
jsbDelay.addAdjustmentListener(this);
}


class Ball extends JPanel implements Runnable
{

class BallState
{
int x;

int y;
int dx;
int dy;
Color color;

public BallState()
{
x = 0;
y = 0;
dx = 2;
dy = 2;

int r = (int)(Math.random() * 1000) % 256;
int g = (int)(Math.random() * 1000) % 256;
int b = (int)(Math.random() * 1000) % 256;

color = new Color(r,g,b);
}
}
public void add()
{
list.add(new BallState());
}
public void subtract()
{
if(list.size() > 0)
list.remove(list.size() - 1);
}
protected void paintComponent (Graphics g)
{
super.paintComponent(g);

for(int i = 0; i < list.size(); i++);
{
BallState ball;
ball = (BallState)list.get(i);
if(ball.x < radius) ball.dx = Math.abs(ball.dx);
if(ball.x > getWidth() - radius) ball.dx = -Math.abs(ball.dx);
if(ball.y < radius) ball.dy = Math.abs(ball.dy);
if(ball.y > getHeight() - radius) ball.dy = -Math.abs(ball.dy);

ball.x += ball.dx;
ball.y += ball.dy;
g.setColor(ball.color);
g.fillOval(ball.x - radius, ball.y - radius, radius*2, radius*2);
}
}
public void run()
{
try
{
while(true)
{
while(!isSuspended)
{
repaint();
Thread.sleep(speed);
}
lock.lock();
suspended.await();
lock.unlock();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}

public void resume()
{
lock.lock();
isSuspended = false;
suspended.signalAll();
lock.unlock();
}
public void suspend()
{
lock.lock();
isSuspended = true;
lock.unlock();
}

public void setDelay(int ms)
{
speed = ms;
}
private int speed;
protected Thread timer;
private ArrayList list;
private int x;
private int y;
private int radius ;
private int dx;
private int dy;
boolean isSuspended;
Lock lock;
Condition suspended;

public Ball()
{
speed = 10;
timer = new Thread(this);
list = new ArrayList();
x = 0;
y = 0;
radius = 5;
dx = 2;
dy = 2;
isSuspended = false;
lock = new ReentrantLock();
suspended = lock.newCondition();
timer.start();
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jbtSuspend) ball.suspend();
else
if(e.getSource() == jbtResume) ball.resume();
else
if(e.getSource() == jbtAdd) ball.add();
else

if(e.getSource() == jbtSubtract) ball.subtract();
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
ball.setDelay(jsbDelay.getMaximum() - e.getValue());
}
}

}

Explanation / Answer

Run this below code it will excecute



import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import java.util.concurrent.locks.*;

import javax.swing.*;

import javax.swing.border.LineBorder;


public class BouncingBalls extends JApplet {

/**

* @param args

* the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

JFrame frame = new JFrame();

JApplet applet = new BouncingBalls();


frame.add(applet);


frame.setTitle("BouncingBalls");


frame.setLocationRelativeTo(null);

frame.setResizable(false);

frame.setDefaultCloseOperation(3);


frame.setSize(400, 200);

frame.setVisible(true);

}


public BouncingBalls() {

add(new BallControl());

}


class BallControl extends JPanel implements ActionListener,

AdjustmentListener {

private Ball ball;

private JButton jbtSuspend;

private JButton jbtResume;

private JButton jbtAdd;

private JButton jbtSubtract;

private JScrollBar jsbDelay;


public BallControl() {

ball = new Ball();


jbtSuspend = new JButton("Suspend");


jbtResume = new JButton("Resume");


jbtAdd = new JButton("+1");


jbtSubtract = new JButton("-1");

jsbDelay = new JScrollBar();

JPanel panel = new JPanel();


panel.add(jbtSuspend);


panel.add(jbtResume);


panel.add(jbtAdd);


panel.add(jbtSubtract);


ball.setBorder(new LineBorder(Color.red));

jsbDelay.setOrientation(0);

ball.setDelay(jsbDelay.getMaximum());

setLayout(new BorderLayout());

add(jsbDelay, "North");

add(ball, "Center");

add(panel, "South");

jbtSuspend.addActionListener(this);

jbtResume.addActionListener(this);

jbtAdd.addActionListener(this);

jbtSubtract.addActionListener(this);

jsbDelay.addAdjustmentListener(this);

}


class Ball extends JPanel implements Runnable {


class BallState {

int x;

int y;

int dx;

int dy;

Color color;


public BallState() {

x = 0;

y = 0;

dx = 2;

dy = 2;


int r = (int) (Math.random() * 1000) % 256;

int g = (int) (Math.random() * 1000) % 256;

int b = (int) (Math.random() * 1000) % 256;


color = new Color(r, g, b);

}

}


public void add() {

list.add(new BallState());

}


public void subtract() {

if (list.size() > 0)

list.remove(list.size() - 1);

}


protected void paintComponent(Graphics g) {

super.paintComponent(g);


for (int i = 0; i < list.size(); i++){

BallState ball;

ball = (BallState) list.get(i);

if (ball.x < radius)

ball.dx = Math.abs(ball.dx);

if (ball.x > getWidth() - radius)

ball.dx = -Math.abs(ball.dx);

if (ball.y < radius)

ball.dy = Math.abs(ball.dy);

if (ball.y > getHeight() - radius)

ball.dy = -Math.abs(ball.dy);

ball.x += ball.dx;

ball.y += ball.dy;

g.setColor(ball.color);

g.fillOval(ball.x - radius, ball.y - radius, radius * 2,

radius * 2);

}

}


public void run() {

try {

while (true) {

while (!isSuspended) {

repaint();

Thread.sleep(speed);

}

lock.lock();

suspended.await();

lock.unlock();

}

} catch (Exception ex) {

ex.printStackTrace();

}

}


public void resume() {

lock.lock();

isSuspended = false;

suspended.signalAll();

lock.unlock();

}


public void suspend() {

lock.lock();

isSuspended = true;

lock.unlock();

}


public void setDelay(int ms) {

speed = ms;

}


private int speed;

protected Thread timer;

private ArrayList list;

private int x;

private int y;

private int radius;

private int dx;

private int dy;

boolean isSuspended;

Lock lock;

Condition suspended;


public Ball() {

speed = 10;

timer = new Thread(this);

list = new ArrayList();

x = 0;

y = 0;

radius = 5;

dx = 2;

dy = 2;

isSuspended = false;

lock = new ReentrantLock();

suspended = lock.newCondition();

timer.start();

}

}


public void actionPerformed(ActionEvent e) {

if (e.getSource() == jbtSuspend)

ball.suspend();

else if (e.getSource() == jbtResume)

ball.resume();

else if (e.getSource() == jbtAdd)

ball.add();

else if (e.getSource() == jbtSubtract)

ball.subtract();

}


public void adjustmentValueChanged(AdjustmentEvent e) {

ball.setDelay(jsbDelay.getMaximum() - e.getValue());

}

}

}