Question
I need this JAVA program fixed so that the program should be able to add a new ball every time the user clicks the mouse.
I need this JAVA program fixed so that the program should be able to add a new ball every time the user clicks the mouse. Provide a minimum of 20 balls. Randomly choose a color for each new ball. The program should also use shadows. As a ball moves, draw a black solid oval at the bottom of the JPanel. Here is my code so far that I need fixed please.
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class BouncingBall extends JApplet implements Runnable, MouseListener { private Thread blueBall; private boolean xUp, yUp, bouncing; private int x, y, xDx, yDy;
public void init() { xUp = false; yUp = false; xDx = 1; yDy = 1; addMouseListener( this ); bouncing = false; }
public void mousePressed( MouseEvent e ) { if ( blueBall == null ) { x = e.getX(); y = e.getY(); blueBall = new Thread( this ); bouncing = true; blueBall.start(); } }
public void stop() { if ( blueBall != null ) { blueBall = null; } }
public void paint( Graphics g ) { if ( bouncing ) { g.setColor( Color.blue ); g.fillOval( x, y, 10, 10 ); } }
public void run() { while ( true ) {
try { blueBall.sleep( 100 ); } catch ( Exception e ) { System.err.println( "Exception: " + e.toString() ); }
if ( xUp == true ) x += xDx; else x -= xDx;
if ( yUp == true ) y += yDy; else y -= yDy;
if ( y <= 0 ) { yUp = true; yDy = ( int ) ( Math.random() * 5 + 2 ); } else if ( y >= 190 ) { yDy = ( int ) ( Math.random() * 5 + 2 ); yUp = false; }
if ( x <= 0 ) { xUp = true; xDx = ( int ) ( Math.random() * 5 + 2 ); } else if ( x >= 190 ) { xUp = false; xDx = ( int ) ( Math.random() * 5 + 2 ); }
repaint(); } }
public void mouseExited( MouseEvent e ) {} public void mouseClicked( MouseEvent e ) {} public void mouseReleased( MouseEvent e ) {} public void mouseEntered( MouseEvent e ) {} }
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