Extending the Browser with Java

[ LiB ]

If you're tired of playing the plug-in game, there's another totally different way to extend the browser's capabilities: Java.

How Java Works in the Browser

Java is a platform-independent, object-oriented programming language created by Sun Microsystems. (Despite the similarity in names , Java is not related to JavaScript.) Java can create fully functional, freestanding applications. It also can create miniapplications, called applets , that run inside a web browser. Because Java is a complete programming language, similar to C or C++, Java applets can be as powerful and diverse as you like. Java applets are commonly used for everything from online games to animation and special effects to visitor counters, clocks, calculators , and navigation tools.

The Nature of Java

To understand what makes Java so well-suited to web use, you need to know how it differs from other programming languages.

Computers don't directly "understand" C++ or Java or any other so-called high-level programming language. Instead, computers understand a numeric language called machine code. After a program is written in a high-level language, it must be compiled, or translated, into machine language. Machine language is platform-specific; this is why your copy of Dreamweaver will run only on a PC or only on a Mac. Programs must be compiled for a certain type of computer, and then they will run only on that type of computer.

To learn more about Java, visit Sun's website at http://java.sun.com. To learn more about implementations of the Java virtual machine, visit Microsoft at www.microsoft.com/ java and Apple at www.apple.com/java.

Java is different. Java is compiled to run on a pretend computer called a virtual machine. The virtual machine is actually itself a program that is compiled to run on a specific platform. On Windows computers, the virtual machine is Microsoft VM; on Macs, it's Mac OS Run-Time for Java (MRJ). When a Java application or applet is run on a virtual machine, the virtual machine translates the code into platform-specific machine code. Thus, the Java applet itself is platform-independent.

Java Issues

The good news is, Java applets are not only platform-independent, but they're browser-independent as well. As soon as a browser encounters an applet on a web page, it launches the virtual machine and steps out of the way. You don't have any plug-ins to worry about. The only things needed to run a Java applet are a virtual machine (which most computers already have installed) and a Java-enabled browser (which almost all browsers are).

The bad news is, nothing is perfect. Some virtual machines are slower and buggier than others; it takes time to launch the virtual machine, which can seem like excessive download time to a frustrated user ; and, because the virtual machine is running through the browser, complex applets that require lots of processing power can (and do!) crash browsers.

Java also can create security problems. Some so-called hostile applets are actually designed to behave like viruses; others can cause damage to a system accidentally . It's the job of the virtual machine to protect the computer system from these dangers, but virtual machines themselves can never be completely hackproof. Consequently, many institutions that deal in sensitive information will set their firewalls not to accept any Java, and individuals might choose to disable Java in their browser preferences.

Java and Media

Java applets can contain images and sounds, which will then display in the browser without the need for additional plug-ins or system components . All images must be GIFs or JPEGs. All sounds must be AU files. Java cannot handle video; however, it can create animations or "fake video" from a series of still images. Java animations are not as smooth and do not run as quickly as those created in Flash, Shockwave, or QuickTime.

Working with Applets

Working with applets is not like working with Shockwave or QuickTime movies, or any other kind of web media, because applets are not structured like those elements. Your first encounter with an applet will probably involve the words "What are all these bits and pieces, and where's the applet?"

Class Files

A basic compiled Java applet is a file with the filename extension .classnot .java, which is used for uncompiled source code.

However, an applet often consists of more than just one file. A complex applet might have several class files, of which only one is the applet itself. The others are supporting players that the applet will call on as it works. With some applets, naming conventions make it clear which is the main fileif the program is called Tabulator and there's a class file called tabulator.class, for instance. With other applets, however, it can be a challenge knowing which class file to actually embed in the web page. (The best applets usually have documentation that spells this out for you.)

Media Files

If an applet uses any media filesimages, sounds, movies, and so onthese also will be in separate files, sometimes in separate folders. You must keep the internal folder structure of the applet the way the applet author intended, or the applet won't be capable of finding its media files.

Obtaining Java Applets

Most Java applets are written by Java programmers. There isn't one standard authoring environment for creating them. This is why every applet is so different from every other applet.

If you want to use Java on your website, you can program it yourself, hire someone else to program it, or use one of the many prewritten applets available on the web and elsewhere. Sun's own applet resource page (http://java.sun.com/applets), which has links to other major resource sites as well as a selection of applets to download, is a good place to start looking. Some applets are free, some are shareware, and some are commercial. Some are also better documented than others and allow more customization. Some work better than others do.

Working with Applets in Dreamweaver

Dreamweaver provides several features for working with Java applets, including the Applet object and the Applet Property Inspector. You can use these tools to insert an applet into an existing page or to examine and alter the sample HTML code that usually comes with commercially available Java applets.

Inserting an Applet

In Dreamweaver, use the Applet object, found with the Media objects in the Common category of the Insert bar, to place Java applets on a page. The actual process is simple. Click the Applet object. In the dialog box that follows , browse to the appropriate class file and click OK. There!

You have an applet. If you look at your code, you'll see that the applet has been inserted using the applet tag.

Figure 15.11 shows the Applet object as well as the Property Inspector for a Java applet. Because every applet is different, this is another generic inspector, with a Parameters button for adding applet-specific parameters. Aside from the standard parameters, the only two settings available are Code and Base. Code adds a class parameter and should be set to the name of the class file. Base adds the base parameter and should be set to the URL of the folder, if any, that contains the Java applet files. (It's customary to store an applet in its own folder so that all the files that comprise it can easily be kept together.) As with plug-in media, additional parameters used by specific applets are added with the generic Parameters button.

Figure 15.11. The Applet object and the Property Inspector for a Java applet.

Exercise 15.4. Inserting a Java Applet

In this exercise, you insert an applet that puts a continually changing series of quotes on the web page. (This applet is one of several freebies offered at the Sun Java site, http://java.sun.com/openstudio/guide.html. To see it in action on a web page, look on the book's website in chapter_15/java for quote_finished.html . Figure 15.12 shows a static screenshot from this file.)

Figure 15.12. Sun's Java Quotes applet as it appears in a web page.

According to the documentation, the applet takes several parameters; Table 15.5 lists them. Some of these parameters might be optional, and others might be required; the documentation doesn't specify. You'll use them all.

Table 15.5. Parameters for the Sun Quotes Java Applet, Along with Descriptions and Suggested Values




The background color of the applet in RGB hexadecimal.


The border height, in pixels.


The border width, in pixels.


The delay between frames , in milliseconds .


The name of the font to be used for the applet.


The size of the font, in points.


A URL to load if the applet is clicked.


true or false , determining whether the quotes should appear randomly or in a set order.


The number of quotes.


This parameter must appear once for each quote to be included ( N is an integer), based on the number of quotes specified. The first quote is quote0, the second quote is quote1, and so on.


For each occurrence of the parameter, the value must be a vertical bar ()delimited string in which the first item is the quote, the second item is the author, the third item is the RGB hexadecimal text color, the fourth item is the RGB hexadecimal background color, and the last item is the length of time in seconds to display the quote.


The distance in pixels between the quote and the author name.

  1. To start, find and examine the Quote folder, which contains this applet. You'll see several class files. Can you tell by their names which one is the applet?

  2. In chapter_15/java , open java_quotes.html .

    Using the Insert Applet object, insert the applet. The main class file that you should insert (you might have figured this out already) is JavaQuote.class . The codebase option should say quote/classes/ .

    Java is case-sensitive! Make sure all references to files, folders, parameters, and values are in their correct case, or your Java applets won't work.

  3. Next, set the width and height for the applet to occupy in your layout. For this applet, there is no required sizethe dimensions just determine how much space the quotes will be allotted on the page. If you want your page to match the examples shown here, set your width and height to 300 x 125.

  4. Referring to the parameters list in Table 15.5, and using the generic Parameters dialog box, set the parameters for the applet. Experiment until you get results you like.

    (You might find it difficult to see what you're doing when you try to enter the quote parameter because most quotes are fairly long and the dialog box won't show them in their entirety. If you like, you can type each quote first in a text editor, such as Notepad or Simple Text, and then paste it into the parameter's value field. You also can work directly in Dreamweaver Code view, of course, if you feel comfortable there.)

    When you're done, your Parameters dialog box will look something like the one shown in Figure 15.13.

    Figure 15.13. The Parameters dialog box, showing entered parameters and values for the Sun Quote Java applet. (Only the first few parameters are visible.)

  5. Try it in a browser. If everything got entered correctly, your virtual machine should load and show you an ever-changing set of quotes.

    You might come across a Java applet that has been packaged into one or more archive files. These files will have filename extensions of .jar, .zip, and .cab. With an applet like this, it's easiest to just follow the programmer's instructions that came with the applet and not use Dreamweaver to generate the code.

[ LiB ]

Macromedia Dreamweaver MX 2004 Demystified
Macromedia Dreamweaver MX 2004 Demystified
ISBN: 0735713847
EAN: 2147483647
Year: 2002
Pages: 188
Authors: Laura Gutman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net