Using the class Particle
given below, write an applet that will show the particle in actual freefall. You should include the following instance variables:
Particle
BufferedImage
int
to hold the timeTimer
int
to hold the x-coordinate of the particle (which doesn't change in free-fall)You should also include implementations for the following methods:
init()
methodpaint(Graphics)
methodupdateScreen()
methodYou only are required to have three import statements (others shouldn't be necessary)
java.awt.*
java.awt.image.*
javax.swing.*
The applet should update the screen every ~100ms for a smoother animation, but other update values are fine. Make sure when you copy-and-paste that the package is correct (Netbeans should yell at you if not, see image below code).
package edu.govschool;
public class Particle
{
// We don't want static in case we have multiple
// displayed particles, so we can have them fall
// at different times.
private double time;
// Gravity, however, is THE LAW
private static final double GRAVITY = 9.81;
/**
* Default constructor.
* Creates a new <code>Particle</code> with the
* time set to 0.
*/
public Particle()
{
this.time = 0;
}
/**
* Gets the current y-coordinate of the <code>Particle</code>.
* @return the current y-coordinate
*/
public int getY()
{
return (int) (0.5 * GRAVITY * Math.pow(time, 2));
}
/**
* Sets the time.
* @param time the time to set
*/
public void setTime(int time)
{
this.time = time;
}
}