public class Apjava4 extends Applet implements Runnable
{
Thread animation; //declares a thread called animation
static final int REFRESH_RATE = 50; //Constant for “delay”
//declare other class data members
public void init()
{
//initialize class data members
}
public void paint(Graphics g)
{
//painting
}
public void start()
{
animation = new Thread(this);
if(animation != null)
{
animation.start();
}
}
public void run()
{
while(true)
{
//put code that runs in loop here
repaint();
try
{
Thread.sleep(REFRESH_RATE);
}catch(Exception exc){};
}
}
public void stop()
{
animation = null;
}
}
BouncingBall() the class constructor. This will initialize the data members.
void move () moves the ball by first checking to see if the ball has "hit" the bottom of the applet.
If it has hit bottom, move changes the speed of the ball from positive to negative. Otherwise, the speed of the ball is
increased by the amount of acceleration. Then the speed of the ball is added to the Y coordinate.
void draw (Graphics g) draws the ball at its current position.
public class Apjava4 . . . .
{
//1. two added declarations
Graphics offscreen;
Image image;
public void init()
{
. . . .
//2. two added initializations
image = createImage(nSCREEN_WIDTH, nSCREEN_HEIGHT);
offscreen = image.getGraphics();
}
public void paint(Graphics g)
{
//3. clear offscreen
offscreen.setColor(Color.white);
offscreen.fillRect(0,0,nSCREEN_WIDTH, nSCREEN_HEIGHT);
//4. modify code so that drawing is offscreen
theBall.draw(offscreen);
. . . .
//5. copy offscreen to visible screen
g.drawImage(image,0,0,this);
}
//6. override update()
public void update(Graphics g)
{
paint(g);
}
}
You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.
Submit the URL of your applet with the attached Java code (the .java file(s)) in an email message
to mrsimon@lycos.com.