This week we dissect a large applet. Hopefully, the code is more explicit than we are about what goes into this language and what we can get out of it. A Close Examination of a Large Applet
One of the bigger problems when it comes to this kind of expository writing is the catch-22 of the example: If it's simple enough to understand, it's usually too simple to make a good point. Thus far, over the months that we've been covering Java, we've erred (sometimes heavily, although occasionally not heavily enough) on the side of simplicity. This week we're going to make up for it.
This isn't the best kind of example, that is, an example of a genuinely "cool program." You're not going to want to rush out and grab this applet for yourself so you can use it every day, but it's not trivial either. It does something "real," and it does it using bits of all of the things we've talked about here. (Not that we expect you to remember it all. We'll try to weave in as many links as we can to explain what we're talking about as we go along.)
If you've been following the streams columns and were expecting to see a good example of streams in action, you will. However, it is worth taking a look at them from every angle: from the classes we use; to how the source files are organized, compiled, and distributed; to how multiple classes fit together to make a coherent applet. Readers familiar with the past few columns will notice that we've pulled out the Java applet we wrote to read Web pages over the Net.
It does a little of everything, actually--much as you would see in a "real world" applet. So what, then, does it actually do? It reads and "parses" an HTML document over the network, collecting information about each of the tags in the document. Every tag becomes a button, and if the button is clicked on, it will display what information it has. Like so:
In this case the document was loaded as the example page. All the HTML tags that were used in the form of buttons is visible here. If you click on one of the buttons, the tag itself will appear in the upper box, which is not too exciting if you click on an "I" or a "P" button. However, try clicking on the "APPLET" button, or an "A" button. You'll be able to see the rest of the tag that was used.
The example was created using three classes, or three .java files in practical terms. The biggest class is the applet itself, which takes care of the network operations and assembling and orchestrating the data and the user interface, or basically everything. The second class we created is more of an accessory, or a special subclass of the
java.awt.Button. The third class is a tiny, simple class that just holds information about a given HTML tag--what it's name is, where it starts in the HTML, and where it ends.If you're wondering why we would need to make a new kind of button to make this applet work, good. It is critical to creating the style of programming graphical applications (and applets) that we're trying to convey here. Unfortunately, there's so much ground to cover with this applet that it's just not possible to do it all in one column. So, over the next few weeks we're going to be looking at this program from a number of different angles, using it as a springboard to jump into explanations of a number of important facets of Java. We recommend that you come back, and keep coming back, to see what it's all about.