lease help me fix the errors in the java program below. There are two class AnimatedBall and BouncingBall
lease help me fix the errors in the java program below. There are two class AnimatedBall and BouncingBall
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Please help me fix the errors in the java program below. There are two class AnimatedBall and BouncingBall
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AnimatedBall extends JFrame implements ActionListener
{
private Button btnBounce; // declare a button to play
private TextField txtSpeed; // declare a text field to enter
private Label lblDelay;
private Panel controls; // generic panel for controls
private BouncingBall display; // drawing panel for ball
Container frame;
public AnimatedBall ()
{
// Set up the controls on the applet
frame = getContentPane();
btnBounce = new Button ("Bounce Ball");
//create the objects
txtSpeed = new TextField("10000", 10);
lblDelay = new Label("Enter Delay");
display = new BouncingBall();//create the panel objects
controls = new Panel();
setLayout(new BorderLayout()); // set the frame layout
controls.add (btnBounce); // add controls to panel
controls.add (lblDelay);
controls.add (txtSpeed);
frame.add(controls, BorderLayout.NORTH);
// add panel to top
frame.add(display, BorderLayout.CENTER);
//add panel to center
btnBounce.addActionListener(this);
//add action listener
setSize(450,500);
setVisible(true);
} // init method
public void paint (Graphics g)
{
// paint no longer used
// we are not painting on the JFrame - remove
} // paint method
public void actionPerformed (ActionEvent e)
{
long speed;
speed = Long.parseLong(txtSpeed.getText());
//read text field
if (e.getSource() == btnBounce)
{
display.draw(speed);
}
} // action method
}
BouncingBall.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AnimatedBall extends JFrame implements ActionListener
{
private Button btnBounce; // declare a button to play
private TextField txtSpeed; // declare a text field to enter
private Label lblDelay;
private Panel controls; // generic panel for controls
private BouncingBall display; // drawing panel for ball
Container frame;
public AnimatedBall ()
{
// Set up the controls on the applet
frame = getContentPane();
btnBounce = new Button ("Bounce Ball");
//create the objects
txtSpeed = new TextField("10000", 10);
lblDelay = new Label("Enter Delay");
display = new BouncingBall();//create the panel objects
controls = new Panel();
setLayout(new BorderLayout()); // set the frame layout
controls.add (btnBounce); // add controls to panel
controls.add (lblDelay);
controls.add (txtSpeed);
frame.add(controls, BorderLayout.NORTH);
// add panel to top
frame.add(display, BorderLayout.CENTER);
//add panel to center
btnBounce.addActionListener(this);
//add action listener
setSize(450,500);
setVisible(true);
} // init method
public void paint (Graphics g)
{
// paint no longer used
// we are not painting on the JFrame - remove
} // paint method
public void actionPerformed (ActionEvent e)
{
long speed;
speed = Long.parseLong(txtSpeed.getText());
//read text field
if (e.getSource() == btnBounce)
{
display.draw(speed);
}
} // action method
}
BouncingBall.java
import java.awt.*;
public class BouncingBall extends Panel
{
private long delay;
// Overridden paint method for painting the BouncingBall panel
public void paint( Graphics g )
{
super.paint( g ); // calls paint method from Panel.
int radius = (getSize().width/10)/2;
int Z = getSize().height/2 - radius;
int X = (int)(Math.random()*(getSize().width)) + radius;
int Y = (int)(Math.random()*(getSize().height)) + radius;
int dy = 1;
for (int count = 0; count < 1000 ;count++ )
{
g.setColor(getBackground());
//get the panel background color to erase
g.drawOval(X, Y, 2*radius, 2*radius);
if (Y<=0 || Y>=(getSize().height)- 2*radius)
{
dy = -dy;
}
Y += dy;
g.setColor( Color.BLUE );
g.fillOval(X, Y, 2*radius, 2*radius);
try
{
Thread.sleep(delay);
}
catch (InterruptedException ex)
{
}
}
}
// method to set delay value and draw the BouncingBall Panel
public void draw(long d)
{
this.delay = d;
repaint();
}
} // end class BouncingBall
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education