Note: the Java source created the class file. The applet relies on the class file.

Today's task: copy the source below into a new file call "Ball.java". Then compile it, creating "Ball.class". We will then create an html file that incorporates the class file as an applet. After that html file is created and saved, we will open it in Internet Explorer and "hopefully" it will work.

We will not be able to take the final step of uploading and testing from a web server. For that reason, I have my fingers crossed in the hope that this works.

 

Sample One

The Java Source for the above applet:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Ball extends Applet implements Runnable {
int x,y,dx,dy,diam,sizex,sizey;

public void init() {
setBackground(Color.black);

x=y=0; dx=dy=5; diam=20;

// Getting Parameters form the HTML
sizex=Integer.parseInt(getParameter("WIDTH"));
sizey=Integer.parseInt(getParameter("HEIGHT"));

(new Thread(Ball.this)).start();
}

public void run() {
while (true) {
try {
Thread.currentThread().sleep(40);
}
catch (InterruptedException e) {};

x+=dx; y+=dy;
if ((x<=0)||(x+dx+diam>=sizex)) dx=-dx;
if ((y<=0)||(y+dy+diam>=sizey)) dy=-dy;

repaint();
}
}

public void paint(Graphics g) {
g.setColor(Color.white);
g.fillArc(x,y,diam,diam,0,360);

// Converting double to string
g.drawString(String.valueOf(x),20,20);
g.drawString(String.valueOf(y),20,40);
g.drawString(""+dx,80,20);
g.drawString(""+dy,80,40);
}
}