Tom Kelliher, CS 116
Sept. 4, 2002
Lab 1.
Applets and variables.
import java.applet.*; import java.awt.*; // The Hello class simply displays the string "Hello, world!" // within the graphics object. public class Hello extends Applet { public void paint(Graphics g) { g.drawString("Hello, world!", 20, 10); } }
Classes must be imported before you can use them. We are importing the applet and awt packages.
We are defining our own applet which inherits everything from the Applet class:
public class myClassName extends Applet { // ... }
Within our applet we define a method paint which is used to draw the applet on the screen. A method is defined as follows.
visibility returnType methodName(formal argument list) { ...statements... }
public
or
private
to indicate whether method can be used outside the
class or not.
paint
takes one argument, a Graphics object)
paint
method.
The paint method changes the Graphics object g
to specify how the
Applet should be drawn.