Part 1: What About Applets?
Java Game Programming Tutorial
Getting Started
To program in the Java language, you will need to install the free Java Development Kit (JDK) on your system.
You can find the latest version of the JDK, documentation, and online tutorials here at the JDK Download Site.
You will also require a Java-enabled web browser to view the tutorial examples.
My aim is not to teach every aspect of Java programming but to help those with a bit of programming knowledge with graphics and game programming issues in Java relating to web pages and the internet. I hope you find it helpful.
Download javaGameProgrammingTutorialClasses.zip to receive the following classes:
- DBuffer.class
- Flicker.class
- FXCycle.class
- FXImage.class
- Hello.class
- Lines.class
Part I: What about Applets?
Applets are compiled Java programs which are run through the
Applet Viewer or through any world wide web browser which
supports Java. Applets are considered quite "safe" because of
their limited file and network access, and are nevertheless impressive.
Each applet contains five functions to be called during its existence:
- init - called when applet is first loaded to initialize values such as sounds.
- start - called after init is finished to perform task such as playing a sound.
- stop - called when user leaves web page to stop actions in progress from start.
- destroy - called when browser is quit to release system resources used by applet.
- paint - called whenever the screen is redrawn.
These will often be rewritten but an applet still has a default function for each.
The Java programmer writes the applet source code in files with
the .java extension. The resulting compiled files have the same
name with the .class extension.
e.g. Hello.java will be compiled into Hello.class
Here is the Java code for a tiny applet which draws a message:
// Hello.java
// by Garry Morse
import java.awt.*;
import java.applet.*;
public class Hello extends Applet {
public void paint(Graphics g) {
g.drawString("Hello there!",10,10);
}
}
The import statements are common to all applets which inherit the
functionality of Java classes. Here, our class Hello is declared
as a subclass of the Applet class. For C/C++ programmers, this is
rather like the #include statement for header files.
Since users of the world wide web are viewing pages coded in
HTML, it seems logical that Java applets are embedded in these
pages through the use of the <APPLET> tag.
Here is the HTML for a blank web page containing a Java applet:
<APPLET CODE="Hello.class" WIDTH=200 HEIGHT=50>
There are required tags which follow the applet tag:
- CODE - filename of executable code with class extension. (required)
- WIDTH - width of the window to open for the applet. (required)
- HEIGHT - height of the window to open for the applet. (required)
These are other tags which will appear in later examples:
- ALT - alternate image or text for web browsers which are not Java-capable.
- ALIGN - position applet on web page, left, right, or center.
- CODEBASE - location of Java code if in different directory than HTML page.
- HSPACE - amount of horizontal space around an aligned applet.
- NAME - symbolic name for applets on same page to refer to one another by.
- PARAM - repeated for applets designed to take parameters such as filenames.
- VSPACE - amount of vertical space around an aligned applet.
|