Steps to Java Internationalization (i18n)
Mouse Event Handling in Java 1.0
You are here: irt.org | Articles | Java | Java #6 I Wanna hold your hand - longer [ previous next ]
Published on: Tuesday 1st September 1998 By: Tarique Sani
In the last article I showed you how to create your first Java application but every one is talking about Applets and how cool it is to have one on your web page. Lets cut out the riff raff this time and get down to business. At this stage I suggest you first go over my previous article if you have not already done so.
Creating an applet is a bit different from creating an application. Java Applets run and are displayed inside a web page with other page elements; they therefore have to follow special behavioral rules. Because of this creating an applet is a bit more complex than creating an application but it is not very difficult!
We will recreate out Hello World example, this time as an applet, proceed to place it inside a Web page and view it, as with the application in the previous article the applet source code is also written using a plain text editor. Carefully copy the following code into a text editor like notepad, wordpad, joe, vi or emac!
import java.awt.Graphics; public class HelloWorldApplet extends java.applet.Applet{ public void paint(Graphics g) { g.drawString("Hello World!", 5, 25); } }
Save this file with the filename exactly as the name of the class in our case HelloWorldApplet.java you can save it in any directory of your choice.
Just as we did for the application - from inside a DOS shell CD to the directory containing your applet source file and use the Java Compiler to compile it:
javac HelloWorldApplet.java
the procedure is same for almost all OS's just that you won't have the 'DOS' shell, if everything was proper you will have a file called HelloWorldApplet.class in the same directory. This is your Java applet file.
To have the applet run inside a Web page you must refer to that class file inside the HTML code for that page using the <APPLET> tag, though I have discussed the <APPLET> tag in detail in JAVA#1 article. For the sake of completeness here is a simple HTML file for our example.
Create the following HTML file and save it as Hello.html.
<HTML> <HEAD> <TITLE>Test Hello World Applet</TITLE> </HEAD> <BODY> <P>This Applet says - <APPLET CODE="HelloWorldApplet.class" WIDTH=150 HEIGHT=25> </APPLET> </BODY> </HTML>
Note two things here:
OK! that's it we are ready to view our first applet in all its glory.
To view the applet, you need one of the following:
If you're using a Java-enabled browser to view your applet files, you can use the File menu to navigate to the HTML file containing the applet (make sure you open the HTML file and not the class file). You don't need to install anything on a Web server yet; all this works on your local system. Note that the Java applet may take a while to start up after the page appears to be done loading, be patient. If you don't have a Web browser with Java capabilities built into it, you can use the JDK's appletviewer program to view your Java applet, use:
appletviewer Hello.html
Don't panic if your applet does not run for the first time, here are some of the errors that I remember to have faced the first time I tried my hand at Java.
These errors result when you do not have the JDK's bin directory in your execution path, or the path to that directory is wrong. On Windows, double-check your autoexec.bat file; on UNIX, check the system file withyour path commands in it(.cshrc, .login, .profile, or some similar file)
Make sure the name of the file you're giving to the javac command is exactly the same name as the file. In particular, in the DOS shell you want to use the Windows filename with a .java extension, not the DOS equivalent (HELLOV-l.jav, for example).
This error most often happens if there is a mismatch between the name of the class as defined in the Java file itself (the name following the word class), and the name of the java source file. Both the filenames must match, including upper- and lowercase letters (this particular error implies that the filename had lowercase letters). Rename either the file name or the class name, and this error will go away.
If you get any other error please do write to me at tarique@irt.org.
Steps to Java Internationalization (i18n)
Mouse Event Handling in Java 1.0