IN JAVA For parts 4 and 5, we will use the BouncingBall.java code on the website
ID: 3778963 • Letter: I
Question
IN JAVA
For parts 4 and 5, we will use the BouncingBall.java code on the website. The code is fully functional and will have a ball that bounces (before sinking into the ground...).
Part 1. Add a feature so that if you click the mouse on the window, the ball will warp to that position. Also change the ball's velocity to 0 after warping.
Part 2. Modify the previous part by instead of moving the current ball to where the mouse was clicked, add a new ball there (so there are multiple balls). You should be able to add as many balls as you want (and they should all bounce).
Here's the code:
Explanation / Answer
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
public class BouncingBall extends JFrame implements MouseListener{
private double ballRadius = 25;
// X and Y for the ball
private double ballX=85;
private double ballY=250;
// ball horizontal speed
private double ballVelocityY = 0;
public BouncingBall()
{
setSize(700, 900); // Make the window 400 by 400
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
updateBallOnClick(x, y);
}
@Override
public void mouseEntered(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
@Override
public void mouseExited(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
public static void main(String[] args)
{
// make a our drawing window (JFrame child)
BouncingBall me = new BouncingBall();
// make it visible
me.setVisible(true);
/*me.addMouseListener(new MouseListener(){
@Override
public void mousePressed(MouseEvent e) {
int mouseX=e.getX();
int mouseY=e.getY();
//System.out.println(x+","+y);//these co-ords are relative to the component
me.UpdateBallOnClick(mouseX, mouseY);
}
});*/
try{
while(true)
{
// update the ball
me.updateBall();
// wait 1/20 a second (20 FPS)
Thread.sleep(50);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
public void updateBallOnClick(int x, int y){
ballX = x;
ballY = y;
repaint();
}
public void updateBall()
{
// get the our horizontal window size
int windowMaxY = getContentPane().getHeight();
ballVelocityY += 9.8/10;
// update the ball's position (add velocity on)
ballY += ballVelocityY;
// if the ball went over the right side of the screen
if(ballY + 2*ballRadius > windowMaxY)
{
// give the ball a random negative velocity
ballVelocityY = -Math.abs(ballVelocityY*0.9); // random number between 1 and 50.999999
}
repaint();
}
@Override
public void paint(Graphics g)
{
// Make sure you run super.paint(g) to run the normal JFrame paint
super.paint(g);
// Make the ball red
g.setColor(Color.red);
// Draw the ball (need to convert doubles to int)
g.fillOval((int)ballX, (int)ballY, 2*(int)ballRadius, 2*(int)ballRadius);
}
}