import java.awt.*; // make the graphics libraries available
import java.applet.*; // make the applet libraries available
public class MovingCircle1 extends Applet // make a new applet
{
private int xCoord = 0; // distance from left hand side
private int yCoord = 0; // distance from the top
private int diameter = 40; // circle diameter
private int width = diameter; // width for fillOval call
private int depth = diameter; // depth for fillOval call
private int xChange = 20; // amount to add to xCoord
private int appletWidth = 300; // width of the applet window
public void paint(Graphics g) // this does the drawing
{
g.setColor(Color.blue); // set the pen colour blue
g.fillOval(xCoord, yCoord, width, depth); // draw the rectangle
g.drawString("Hello World", xCoord, yCoord + depth + 20); // write the string
// if the next drawing position would
// move the ball off the right hand side
// of the applet window make the xChange
// negative
// now check that the ball is within the right side
if ((xCoord + diameter + xChange) > appletWidth)
xChange = -xChange;
// now check that the ball is within the bottom
// now check that the ball is within the left side
//now check that the ball is within the top
// now change the values held in xCoord and yCoord
xCoord = xCoord + xChange; // move in xChange pixels from the left
yCoord = yCoord + 20; // move down 20 pixels from the top
}
}
|