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

26.8 Write a program that bounces a blue ball inside a JPanel. The ball should b

ID: 3647395 • Letter: 2

Question

26.8

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 edge of the JPanel, it should bounce off the edge and continue in the opposite direction. The ball should be updated using a Runnable.

My problem is that the ball will not appear. The applet starts but no ball.

Thanks to all in advance!!

Here is what I have so far:

// Fig. 26.8: Ball.java
// LAB 26.8
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Ball extends JApplet implements Runnable
{
private Thread blueBall;
private boolean xUp, yUp, bouncing;
private int x, y, xDx, yDy;
private final int MAX_X = 200, MAX_Y = 200;

public void init()
{
//initialize values
xUp = false;
yUp = false;
xDx = 1;
yDy = 1;
bouncing = false;

//let ball applet be its own MouseListener
addMouseListener(

new MouseListener()
{

public void mousePressed( MouseEvent event )
{
createBall( event );
//delegate call to ball starter
}
public void mouseExited( MouseEvent event ) {}
public void mouseClicked( MouseEvent event ) {}
public void mouseReleased( MouseEvent event ) {}
public void mouseEntered( MouseEvent event ) {}
}
)
;

setSize( MAX_X, MAX_Y ); // set size of Applet
}

//creates a ball and sets it in motion if
//no ball exists
private void createBall( MouseEvent event )
{
if ( blueBall == null )
{
x = event.getX();
y = event.getY();
blueBall = new Thread( this );

bouncing = true; // start ball's bouncing
blueBall.start();
}
}

//called if applet is closed. by setting blueBall to
//null, threads will be ended.
public void stop()
{
blueBall = null;
}

// draws ball at current position
public void paint( Graphics g )
{
super.paint( g );

if ( bouncing )
{
g.setColor( Color.blue );
g.fillOval( x, y, 10, 10 );
}
}

// action to perform on execution, bounces ball
// perpetually until applet is closed.
public void run()
{

while ( true )
{

//sleep for a random interval
try
{
blueBall.sleep( 20 );
}

//process InterrupedException during sleep
catch( InterruptedException exception )
{

System.err.println( exception.toString() );
}

// determine new x position
if ( xUp == true )
x += xDx;
else
x -= xDx;

//determine new y position
if ( yUp == true )
y += yDy;
else
y -= yDy;

// randomize variables for creating next move
if ( y <= 0 )
{
yUp = true;
yDy = ( int ) ( Math.random() * 5 + 2 );
yUp = false;
}

if ( x <= 0 )
{
xUp = true;
xDx = ( int ) ( Math.random() * 5 + 2 );
}

else if ( x >= MAX_X - 10 )
{
xUp = false;
xDx = ( int ) ( Math.random() * 5 + 2 );
}

repaint();

}

}

}

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 = 190 ) { yDy = ( int ) ( Math.random() * 5 + 2 ); yUp = false; } 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