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
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); } }
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