Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

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

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions

Question

What are the two key reasons for the success of Tableau?

Answered: 1 week ago

Question

8. Describe the steps in the development planning process.

Answered: 1 week ago