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

I need a program written in the JAVA computer language and commented on for refe

ID: 3605383 • Letter: I

Question

I need a program written in the JAVA computer language and commented on for referencing. Here is the assignment:

Write a program that bounces a blue ball inside a JPanel. The ball should begin moving with a mousePressed event. When the ball hits the egde of the JPanel, it should bounce off the edge and continue in the opposite direction. The ball should be updated using a Runnable. 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. 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.

Explanation / Answer

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Ball 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 ) {}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote