Special Effects and Game Development
in Java(TM) - Your first applet
by Anibal Wainstein
2.0 Your first applet
Now it is time to start programming your first applets. It
is always a great experience to see your first creations in
action.
2.0.1 Inheriting the Applet
class
We have earlier described what an applet
is. In Java there is already a finished class called "Applet".
This is used as the parent class (the base) for all the types
of applets that you create. To do our own applet, we will
overwrite some of the methods in it and put extra variables.
This is called in Object Oriented Programming to inherit
the Applet class.
import java.applet.*; import java.awt.*;
public class myapplet extends Applet {
}
I know that this was a lot at once but we can
start by describing "import" that makes sure that
you have access to the Applet class and the AWT package. In
the AWT package there is the class String and other important
classes that you may need when you make applets. This is called
importing a Java class package. The "public"
declaration indicates that the applet will be able to be used
by other classes (this is something that is necessary to have
with some Java VM's). The "class" declaration indicates
that the applets name will be "myapplet". The "extends"
declaration means that the class "myapplet" will
inherit the Applet class methods. The applet do not do anything
right now, because we have not yet made any changes on the
parent class.
2.0.2 Initializing with
the init() method and how to write strings.
The first change we will do on our applet is to overwrite
the init() method. This method is the first method executed
in an applet.
import java.applet.*; import java.awt.*;
public class myapplet extends Applet
{
public void init()
{ System.out.println("Hello Sweden!");
}
}
Here we use the built-in method println() (print-line) that
exist in the class "out", which in turn exist in the
Java package "System". With this we write the string
"Hello Sweden!" on the Java console. The Java console
is the communication window with the programmer, which you can
display with your web browser when you test the applet. By using
it, we can receive applet error messages and display our own
messages like "Hello Sweden!".
Next Page >>
|
 |  |
|