| the complete webmaster | ||||
| tutorials | reviews | reference | ||
|
Anatomy of an AppletWell now that you've learned a bit about how to use a Java development environment, if you haven't go back to the previous article, it's time to see what a Java applet looks like. First I'll show you the whole applet so you can see how simple it is then we'll walk through each element of the program so you'll see how it works. This applet was essentially autogenerated by Roaster, a great Java development environment on the Mac, all I've done is add some print statements and modify the comments a bit.
When you compile this code you'll get an applet that prints out various messages when the methods, those functions inside the applet. The first part contains the import statements that tell Java which supporting capabilities you need. For now you don't have to worry about which imports to use just use these in any applet you write.
Note that every every statement in Java ends with either a ; or a }. The next part of the applet is the definition of the applet. In this line you tell Java that your applet inherits the standard characteristics of an applet, in Javese you applet "extends" the standard applet, and you tell Java what you want to call your applet. You must store the code for your applet in a file whose name is the name of your applet, my_applet in this case, followed by a .java.
Comments are lines in your program that are in English for you and others to read so you can remember why you wrote your applet the way you did. In Java you can mark a bunch of lines as being comments--which the Java compiler ignores-- by starting them with a /* and ending them with a */. In addition anything after a // on a line is ignored by the Java compiler. The next part of the applet is the first method. A method is a piece of code that does something. It's like a function or an AppleScript handler. Applets have several standard methods. The public means that the method can be called by other parts of Java. The void means the method doesn't return any value to whatever calls it. The methods name is init. The () after the method name is where the input arguments would go. You'll see an example of a method that has input arguments in a bit. The init method is called when your applet is initialized. To find out when that is compile this applet and run it in an AppletViewer or a browser. If you use a browser make sure the Java console is visible. The System.out.println line will print the message in quotes in the Java console. System.out.println is a method which is part of the Java environment. In general you set up the initial conditions of your applet in this method. For example if your applet is supposed to draw ten circles you might initialize a variable called count to 0 and then add one to it every time you drew a new circle in the paint method. When count was 10 your applet would stop drawing circles.
The methods in your applet can be in any order. This next one, paint, is called whenever your applet has to be redrawn. This particular paint method just draws the string "Hello World" to your applets window. The gworld input parameter, which is a value of type Graphics, tells Java all it needs to know to know where to draw the string. The drawString method takes three arguments, ie inputs, the string to draw and the x and y coordinates to draw the string at. In Java x = 0 is at the left side of the window and y = 0 is at the top.
The next method is the start method. You put the code that actually does the work of the applet here. Right now there's just a print method to let you know when start is called.
These last two methods let you gracefully stop and destroy your applet. The stop method should contain the code that finishes up things and insures that your applet stops in a controlled manner. The destroy method gives you a chance to free up any system level resources you may be using.
Author: tom trinko More articles
about Java |
| write for us | about us | advertise |
Copyright 1997, 1998 A Big Lime. All rights reserved.