I need to get the answer to the following 2 questions based on the code I put on
ID: 3705446 • Letter: I
Question
I need to get the answer to the following 2 questions based on the code I put on here
23.11 (Bouncing Balls) Modify the program in Exercise 23.10 to add a new ball each time the user clicks the mouse. Provide for a minimum of 20 balls. Randomly choose the color for each new ball.
23.12 (Bouncing Balls with Shadows) Modify the program in Exercise 23.11 to add shadows. As a ball moves, draw a solid black oval at the bottom of the JPanel. You may consider adding a 3-D effect by increasing or decreasing the size of each ball when it hits the edge of the JPanel.
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class BouncingBall extends JPanel {
int width;
int height;
float radius = 5;
float diameter = radius * 2;
float X = radius + 40;
float Y = radius + 15;
float dx = 2;
float dy = 2;
public BouncingBall() {
Thread thread = new Thread() {
public void run() {
while (true) {
width = getWidth();
height = getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
} else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}
}
}
};
thread.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Blue Bouncing Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setContentPane(new BouncingBall());
frame.setVisible(true);
}
}
Explanation / Answer
Hi, Please find the modified program below
//Bouncing.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;
public class Bouncing extends JFrame implements MouseListener
{
private JPanel canvas;
private JPanel buttonPanel;
private Ball ball;
private int x = 0, y = 0, xCoord, yCoord;
public Color SelectiverandomColor()
{
Random SelectiverandomColor = new Random();
int red = (int)(Math.random()*256);
int green = (int)(Math.random()*256);
int blue = (int)(Math.random()*256);
return (new Color(red, green, blue));
}
public Bouncing()
{
setTitle("Bouncing");
Container contentPane = getPaneContent();
canvas = new JPanel();
contentPane.add(canvas);
buttonPanel = new JPanel();
LineBorder line = new LineBorder(Color.black);
buttonPanel.setBorder(line);
contentPane.add(buttonPanel, "South");
setSize(300, 300);
setVisible(true);
canvas.addMouseListener(this);
}
public void PressingMouse(MouseEvent e)
{
ball = new Ball(canvas); // create a new ball
Thread t = new Thread(ball);
SelectiverandomColor();
//gets coordinates from where mouse is clicked
x = e.getX();
y = e.getY();
//gives coordinates to ball object
ball.setXCoord(x);
ball.setYCoord(y);
ball.start();
}
public void ClickMouse(MouseEvent e) {}
public void ReleaseMouse(MouseEvent e) {}
public void EnterMouse(MouseEvent e) {}
public void ExitMouse(MouseEvent e) {}
} // end class Bouncing
//Ball.java
import java.awt.*;
import javax.swing.*;
public class Ball extends Thread implements Runnable
{
private JPanel box;
private static final int XSIZE = 30; //constant on size of ball move x-axis
private static final int YSIZE = 30; //constant on size of ball move y-axis
//declare variables
private int x = 0;
private int y = 0;
//declare variables for dimension of x and y
private int dx = 2;
private int dy = 2;
public Ball(JPanel b)
{
box = b;
}//end ball constructor
//sets the ball coordinates of x and y axis
public void setXCoord(int XIn)
{x=XIn;}// end setXCoord
public void setYCoord(int YIn)
{y=YIn;}// end setYCoord
public void draw()
{
//algorithm to draw the ball and size
Graphics g = box.getGraphics();
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}//end draw
public void move()
{
//algorithm for the move of the ball
Graphics g = box.getGraphics();
g.clearRect(x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.getSize();
if (x < 0)
{
x = 0; dx = -dx;
}
if (x + XSIZE >= d.width)
{
x = d.width - XSIZE; dx = -dx;
}
if (y < 0)
{
y = 0; dy = -dy;
}
if (y + YSIZE >= d.height)
{
y = d.height - YSIZE; dy = -dy;
}
//sets the color of the ball and its radius use of fillOval
g.setColor(Color.red);
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}//end move
public void Bouncing()
{
draw();//call for the draw method
//loop and call the move method
for (int i = 1; i <= 2000; i++)
{
move();
try
{
//this is the thread to sleep in 5 seconds
Thread.sleep(5);
}
catch(InterruptedException e)
{
System.err.println("InterruptedException" + e.getMessage());
}//if some how interrupted it throw an exception caught by InterruptException
}
}//end Bouncing
public void run()
{
try
{
for (int run=0; run < 5; run++)
{
Bouncing ();
Thread.sleep((long)(Math.random() * 500));//this is the thread random generator multiply by 500 for 5 seconds
} //end run
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
} // end class Ball
//BallDriver.java
class BallDriver
{
public static void main(String[] args)
{
Bouncing app = new Bouncing();
}
}