|
Your first Java applet
In previous columns we have fiddled about with some basic
javascript so I thought this month we we do something a little
different. We're going to write a simple java applet. Yes
you guessed it, its going to be one of those annoying "Hello
World!" applets that every single damn language makes
you do to get started. The sad fact is though is that they
are great a teaching people the basics.
Article Overview
Before we start
Since we are going to be compiling the java code for the
applet it means your going to need The JavaTM 2 Platform,
Standard Edition. Its about 37 MB so it may take
a while if your on a 56k modem. Use a download manager such
as Flash Get. Also - make sure you download the SDK and not
the JRE. You can download it from:
http://java.sun.com/j2se/1.4/download.html
Now no one can say I don't work hard for you. I wish someone
had sold me I needed that while I was wondering why it wasn't
working. Your also going to need to set the PATH perminantly
unless you know what your doing. Do to the documentation below
and follow the steps to settings your path. You need to add
some text to the path command rather than replace it by the
way. Again I wish someone had told me that. The documentation
on how to do this can be found at:
http://java.sun.com/j2se/1.4/install-windows.html
The applet overview
An applet requires a java enabled browser to work rather
than being a stand alone java application. Most major browsers
supporrt java now although it may be an optional extra on
your copy of Netscape or Opera. Internet Explorer automatically
supports it. There are three steps in creating your first
java applet:
- Creating the Java source code.
- Compiling the source code.
- Running the program.
1. Creating the Java source code
Open your text editor (Notepad will be fine but I prefer
EmEditor) and type in the following:
import java.applet.*;
import java.awt.*;
/**
* The HelloWorld class implements an applet that
* simply displays "Hello World!".
*/
public class HelloWorld extends Applet {
public void paint(Graphics g) {
// Display "Hello World!"
g.drawString("Hello world!", 50, 25);
}
}
Save this code to a file called HelloWorld.java.
You also need an HTML file to accompany your applet. Type
the following code into a new text document:
<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
Save this code to a file called Hello.htm.
2. Comiling the source code
Open up command promt (Start > Run > "Command").
Once your at the command promt, go to the directory where
you have saved the source code. For expample if you saved
them in C:\code you would type cd
C:\code. At the prompt, type the following command
and press Return:
jjavac HelloWorld.java
The compiler should generate a Java bytecode file, HelloWorld.class.
This file will appear in the same directory.
3. Running the program
Although you can view your applets using a Web browser, you
may find it easier to test your applets using the simple appletviewer
application that comes with the JavaTM Platform. To view the
HelloWorld applet using appletviewer, enter at the prompt:
appletviewer Hello.htm
Now you should see an application come up with "appletviewer"
or something similar at the top. It should then say applet
loaded at the bottom and hello world in the middle. Congratualtions!
Your applet works.
Don't work if you got a error from command promt. I got that
too saying it was using the default settings etc, but it didn't
seem to cause a problem. For more information on java applets
goto http://java.sun.com.
|