|
Scripting languages such as JavaScript can enhance the functionality of your Web pages, but for all its capabilities, JavaScript is still very much bound by the existing features of your Web browser. As the name suggests, JavaScript is designed not as a general-purpose programming language, but as a scripting language for extending the capabilities of the browser and for controlling elements on your Web pages.
If, on the other hand, you're looking for a means to add new functionality to the World Wide Web, what you need to do is turn to Java-the language on which JavaScript is based. You learned some about Java applets in Chapter 9, "External Files, Multimedia, and Animation," with the pocket-watch animation we created there. In this chapter, you'll learn lots more about about Java by examining the following topics:
Java was originally developed by a small advanced-projects team at Sun Microsystems. In its early days, Java-originally named OAK- was designed as the programming language for an interactive controller called a Portable Data Assistant (PDA).
What made this device unique was the fact that the technology it encompassed could be embedded into nearly any type of electronic consumer product, and that product could be programmed to perform any operation desired.
After several years of being moved from project to project (from consumer electronic devices to video-on-demand set-top boxes), Bill Joy-one of Sun's co-founders-realized that Java was an ideal language for the Internet and the World Wide Web.
The original proving ground for the use of Java on the Internet was the HotJava browser, a Web browser with most of the common Web browser features-and one major new feature. HotJava, which was itself written in the Java language, had the capability to download and execute small Java programs which then ran inside a Web page, providing animations or interactive tools seamlessly with other HTML features on the page. It was this capability that got many people in the industry of the World Wide Web very, very excited.
But enough history. What exactly is Java, and why would you want to use it?
Java is an object-orientated programming language similar to C++. Unlike C++, however, Java was designed with one unique capability. In the Internet world, there are various computer platforms, all of which use different operating systems and require programs written in languages such as C++ to be specially crafted to suit their individual needs. As a result, you cannot simply take a C++ program written for a Macintosh computer and run it on your Windows 95-based PC.
Java, on the other hand, was designed so that you can do just that-write a program once and have it run on many different computer platforms. To achieve this goal, Java programs are compiled into a special form (called bytecodes), which create cross-plaform executable files. Basically, this means is that Java programs can be run on any computer platform that supports the Java system.
Note |
If you're new to programming languages, the concept of compiling may be new to you. Unlike HTML or JavaScript, Java programs cannot just be read into a browser or other program and run. You need to first run a program called a Java program, which converts, or compiles, the raw Java program into its special cross-platform form. |
The second major feature of Java is the one that makes so much fun for use with Web pages. Using the same Web server and Web browser communications that let you download and view HTML pages, Java programs can be transferred from computer system to computer system without any intervention by the user and, because of its cross-platform technology, without any concern about the type of computer system it's being transferred to. These Java programs are called applets.
New Term |
A Java applet is a Java program that can be included inside an HTML page. When that page is downloaded by a browser that supports Java, the applet is also downloaded and run inside the Web page. |
To run Java applets, you need a browser that supports Java. Netscape was the first browser to sign up to license Java, and Netscape 2.0 was the first browser to include Java applet capabilities. Java has since been licensed to other browser manufacturers and will be appearing in more and more browsers as time goes on.
Basically, there is very little limitation to the possible applications for Java applets and Java-based applications. To this extent, Sun has even created a Web browser called HotJava that was written entirely using the Java language.
Note |
Java programs generally fall into one of two specific categories: Java applets, which are designed to be embedded inside a Web page, and Java applications, which are standalone Java programs. These programs are fully self-contained and don't run within a Web browser at all. You'll learn only about applets in this chapter. |
In fact, the only real limitation imposed by Java is the in imaginations of Web developers. If the crop of Java applets that have sprung up is any indication, some very imaginative minds are at play on the World Wide Web.
In this section, we'll explore some Java applets so that you can get an idea of what you can do using Java.
Note |
To view Java applets, you'll need a browser that supports Java. Netscape 2.0 supports Java on all platforms except Macintosh and Windows 3.1; the pre-release Netscape 3.0 supports it on the Macintosh as well. If you're using Windows 3.1, I'm afraid you're out of luck; there isn't any Java support for your system yet. |
Take for example, the Blue Skies Weather Underground, operated by the University of Michigan (see Figure 25.1). This site represents one of the best examples of the incredible interactive capabilities that Java brings to the World Wide Web. The Weather maps and the various gadgets surrounding it in Figure 25.1 are all part of a single Java applet, which enables you to view the current weather report for major cities by highlighting them with the cursor. In addition, by clicking various regions of the map, you can zoom in for a close-up look at individual weather patterns, or alternatively, you can view a movie of the weather pattern for the past 24 hours.
Figure 25.1 : Blue Skies Weather Underground.
What makes this service so amazing is that it all happens within one easy-to-use screen. Without Java, you would probably need many hundreds of separate Web pages to create a similar service, and even with all these pages, you would still not be able to easily duplicate some features, including the line-drawn United States maps over the satellite images that are created on-the-fly by Java.
To experiment with the features offered by the Blue Skies service, point your Web browser to http://cirrus.sprl.umich.edu/javaweather.
To give yourself an even better idea of the possibilities offered by Java, point your Web browser to http://www.gamelan.com/, as shown in Figure 25.2. This site contains directory of sites currently using Java. It also includes a large collection of applets that demonstrate the variety of reasons why people are starting to incorporate Java into their Web pages-reasons such as these:
Both Netscape and Sun also operate their own directories of Java applets along with a variety of related information. To visit the Netscape directory shown in Figure 25.3, use http://home.netscape.com/comprod/products/navigator/version_2.0/java_applets/index.html. This index contains pointers to all the latest Netscape-related Java information, along with some of the more popular Java applets.
Figure 25.3 : Netscape's Java resources.
To provide even more information about Java, Sun has set up a Web site at http://www.javasoft.com/ devoted just to the subject, as shown in Figure 25.4. This site contains up-to-the-minute details covering all aspects of Java development and usage, and it is also the primary source for Java development tools and documentation.
Due to the size and complexity of the issues involved in using a programming language such as Java to its fullest advantage, dealing with all the intricacies of Java programming is beyond the scope of this book. Therefore, instead of dealing with the actual programming techniques involved, in this section you'll work through the creation of a simple Java applet-a ticker tape display similar to the marquees in Internet Explorer that you learned about in Chapter 9. In this way, you'll get a better idea of what Java is all about.
For those of you who are keen to learn more about the internals of Java, a copy of the complete Java documentation written by Sun Microsystems has been included on the CD-ROM accompanying this book. To view this documentation, you'll need to install a copy of the Adobe Acrobat reader-also available on the CD-ROM-in both Macintosh and Windows versions.
Note |
For a full discussion of Java programming, you might want to check out Teach Yourself Java in 21 Days, also from Sams.net, and written by the same author of this book. |
Before you begin creating your own Java applets, you must get a Java development kit such as Sun's own JDK, Symantec's Cafe Lite, or Natural Intelligence's Roaster. These Java development kits will usually contain all the tools required to compile Java applets, the most up-to-date libraries (called classes), a stand-alone applet viewer to test your applets without the need for Navigator 2.0, and a debugging utility to help locate problems in your Java code.
Note |
In object-oriented terms, the class is the basic structural framework for all program design. A class is a bit like a library of prebuilt instructions, or a template that you can enhance to create new classes and entire applications. Although you don't need to understand all the concepts surrounding object-orientated design and development as you start to use Java, you might find it handy to get a good book on the subject. The previously-mentioned Teach Yourself Java in 21 Days contains a basic introduction to object-oriented programming. |
You'll have to buy most Java development kits, but if you're low on cash, you can download and unpack Sun's JDK (it's not the easiest program to use, but it's still the definitive kit for compatibility). To get the JDK, point your Web browser to http://www.javasoft.com/JDK-1.0/index.html, as shown in Figure 25.5. Sun currently provides JDKs for Windows 95, Windows NT, Macintosh and SPARC/Solaris 2.3, 2.4, or 2.5 systems.
Figure 25.5 : The Java Develpers Kit.
Note |
An alternative method for compiling Java applets is provided by the Black Star Public Java Compiler-http://mars.blackstar.com/. This site operates a Web page where you can submit Java programs for compilation. For any serious use, however, you really do need to install the JDK on your local computer. |
After you've gotten a Java development kit of some sort on your system, you can start writing Java applets. You can write Java programs by using a simple text editor, or you might want to use a program editor of some sort. It really doesn't matter what you use because Java source code is plain text, just as your HTML files are. After choosing your editor, create a new directory to hold your Java applets. Start your editor, and create a new file called Ticker.java. (Be sure to include the capital T; remember that Java is case sensitive.)
Because Java is an object-oriented language, when you're working with Java applets, you really are adding functionality to the basic applet framework defined in the JDK. For this reason, the basic structure of all Java applets looks very similar.
The basic framework for the Ticker Tape applet looks like this:
/* Exercise - Ticker.class */
import java.applet.*;
import java.awt.* ;
public class Ticker extends Applet implements Runnable {
Additional functionality goes in here.
}
The first line is simply a comment line that describes the name of the Java applet. Any text enclosed between the /* characters and corresponding */ characters is treated as a comment and is ignored by the compiler.
The next two lines of code-the ones starting with import-are used to tell the compiler about any additional class libraries that will be used by your applet. All applets need to use routines from the java.applet.* library, and to display information on-screen you also need routines from the Advanced Windows Toolkit, as defined by java.awt.*.
The fourth line of text is the one that does all the work of defining your new class as an applet. In this line, you declare the name for your new class, public class Ticker. You tell the system which existing class it's based on, extends Applet. And because the Ticker Tape applet will need to run continually, you define a special package called Runnable by using implements Runnable.
Warning |
Make sure that you type the word Ticker using the same uppercase and lowercase characters as you did when naming the Ticker.java text file. If the two names are not identical (case-wise), Java will report an error when you attempt to compile the program. |
After the class definition, you need to define some variables for the applet to use. Like most object-oriented languages, Java is a typed language, which means that you must declare the type of information that a variable will hold before it can be used.
To declare all the variables that will be accessible to the entire class, add the following code after the class declaration:
public class Ticker extends Applet implements Runnable {
Thread tkthread = null; /* Thread handle needed for multitasking */
String tktext = "Exercise - ticker tape"; /* Default text */
int tkspd = 1; /* The default scroll speed (slowest is 1 ) */
String tkfname = "TimesRoman"; /* The default font name */
int tkfsz = 12; /* The default font size */
Font tkfont = null; /* Font handle for graphics library */
String tkdirection = "Left"; /* The default scroll direction */
Dimension tksize = null; /* Window dimension handle */
int tktextwth = 0; /* Text width value */
int tktexthgt = 0; /* Text height value */
int tkpos = -63000; /* Scroll position */
Inside every class, there can be any number of different routines called methods. These methods are used to control specific actions that can be taken by a class.
For every class, the first method called when the class is run (or instantiated) is the init() method. This method is used to set up default information for the class and to load variables like those you defined with working values earlier in this exercise. In the base applet class, an init() method is declared already; however, because you want to add additional functionality to the applet class, you need to override the base init() method with a new one of your own.
To define the init() method for Ticker and set up all the control variables, you add the following code.
Note |
The comment lines included in this code are not required to make Ticker operate. They simply explain what each line does. If you are following along with this exercise, you don't need to include the comments. Also, you might find it easier to refer to the completed example at the end of the exercise, which has all the comments removed. |
/* Declare the init() method */
public void init() {
/* Declare a working variable for this method only */
String getval = null;
/* Retrieve the text to be displayed by Ticker */
/* as defined in the HTML document */
getval = getParameter("tktext");
/* If no text is defined, revert to the default message */
tktext = (getval == null ) ? tktext : getval;
/* Retrieve the scroll speed for Ticker */
/*as defined in the HTML document */
getval = getParameter("tkspd");
/* If no speed is defined, revert to the default speed */
tkspd = (getval == null ) ? tkspd : (Integer.valueOf(getval).intValue());
/* Retrieve the font for Ticker */
/* as defined in the HTML document */
getval = getParameter("tkfname");
/* If no font is defined, revert to the default font */
tkfname = (getval == null) ? tkfname : getval ;
/* Retrieve the font size for Ticker */
/* as defined in the HTML document */
getval = getParameter("tkfsz");
/* If no font size is defined, revert to the default size */
tkfsz = (getval == null ) ? tkfsz : (Integer.valueOf(getval).intValue());
/* Create a font class based on the font name and font size */
tkfont = new java.awt.Font( tkfname, Font.PLAIN, tkfsz ) ;
/* Check to see if the Reverse parameter has been set. */
/* If not, set tkdirection to Left */
/* and tkpos to a large negative number */
/* Otherwise, set tkdirection to Right */
/* and tkpos to a large positive number. */
getval = getParameter("tkreverse");
if (getval==null) {
tkdirection = "Left";
tkpos = -63000 ;
}
else {
tkdirection = "Right";
tkpos = 63000;
}
/* Set the background color for the applet window to white */
this.setBackground(Color.white);
}
Warning |
Be sure to include all the opening ({) and closing (}) brackets where listed. These curly brackets, or braces, are used by Java to indicate the start and finish of blocks of code, and without them, Java will get very confused indeed. |
The start() and stop() methods are called when a class is first started and when the class is stopped, respectively.
In this exercise, the start() method needs to be overridden to define Ticker as a self-contained task, one that operates independently of all other activities on your computer. This action allows your operating system to better share its resources among all the programs that are currently running. If this is not done, there is a danger that a routine like Ticker could have a serious impact on the performance of other programs.
However, after you do define Ticker as a task, or thread of its own, you need a way to stop it from running when the applet is no longer needed. To do this, in the stop() method, you include a specific call to the thread to halt its execution.
The code required to perform the start and stop tasks is shown here:
/* Declare the start() method   ; */
public void start() {
/* Define a new Thread for this task */
tkthread = new Thread(this);
/* start Ticker running as an independent task */
tkthread.start();
}
/* Declare the stop() method   ; */
public void stop() {
/* stop the Ticker thread running */
tkthread.stop();
}
In the class definition at the start this exercise, there was a statement that said implements Runnable. This statement defines a template for a special method that gets called after the applet has been loaded, and after the init() and start() methods have been executed. If you have any computer programming experience, you'll find that the run() method is a bit like a main() subroutine.
If you don't have any programming experience, don't worry. All you just need to understand that this method contains a loop of code that causes the Java screen to be continually redrawn. Each time it gets redrawn, the text in the Ticker window is moved a step to either the left or the right.
The code for the run() method is as follows:
/* Declate the run() method   ; */
public void run() {
/* Set the multitasking priority of Ticker to the lowest value */
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
/* Create an infinite loop that continually repaints the Java screen */
while (true) {
/* Send Ticker to sleep so that other programs can get some work done */
try {Thread.sleep( 10 ); } catch (InterruptedException e) {}
/* Whenever Ticker wakes up, repaint the contents of the Java applet window */
repaint();
}
}
The final method for this exercise is the paint() method. Whenever the repaint() statement in the run() method is reached-each pass through the while loop-the paint() method is the main method that gets run. The paint() method is where all the tricky stuff happens to make the text scroll across the screen.
In Java terms, the paint() method is where you draw information onto the Java canvas, which is a fancy name for the drawing area of a Java applet. The paint() method for Ticker is as follows:
/* Declare the paint method   ; */
/* Unlike the other methods, this one receives some information from the */
/* calling routine. This information is assigned to a graphics */
/* class called tk. &n bsp; */
public void paint(Graphics tk) {
/* Get the size of the Java canvas */
/* and assign it to a dimension class called tksize. */
tksize = size();
/* Set the font to use to the one defined in the init() method, */
/* and then get its specs */
tk.setFont(tkfont);
FontMetrics tkfm = tk.getFontMetrics();
/* Calculate the height in pixels of the text, */
/* the first time through the paint method */
/* After this, use the previously calculated value. */
tktexthgt = ( tktexthgt==0 ) ? tkfm.getHeight() : tktexthgt;
/* Calculate the width in pixels of the text message */
/* the first time through the paint method */
/* After this, use the previously calculated value */
tktextwth = ( tktextwth==0 ) ? tkfm.stringWidth( tktext ) : tktextwth;
/* If the scroll direction is set to Left, */
/* use the first set of calculations to determine the */
/* new location for the text in this pass through paint(). */
/* Otherwise, use the set of calculations following the else statement. */
if (tkdirection=="Left") {
tkpos = ( tkpos <= tktextwth * -1 ) ? tksize.width : tkpos - tkspd;
}
else{
tkpos = ( tkpos > tktextwth ) ? 0 - tksize.width : tkpos + tkspd;
}
/* Set the text color to black */
tk.setColor(Color.black);
/* Draw the message in its new position on the Java canvas */
tk.drawString( tktext, tkpos, ( tksize.height + tktexthgt ) / 2 );
}
As promised earlier, here is the completed Ticker applet, ready to be compiled. All the comments except the one on the first line have been removed, and any unnecessary line spacing is gone as well. The indentations, however, have been retained as a guide to how the various components are related. When you write Java code, using indentation to indicate the separate blocks of text is a very good way of cross-checking that no { or } symbols have been left out.
/* Exercise - Ticker.class */
import java.applet.*;
import java.awt.* ;
public class Ticker extends Applet implements Runnable {
Thread tkthread = null;
String tktext = "Exercise - ticker tape";
int tkspd = 1;
String tkfname = "TimesRoman";
int tkfsz = 12;
Font tkfont = null;
String tkdirection = "Left";
Dimension tksize = null;
int tktextwth = 0;
int tktexthgt = 0;
int tkpos = -63000;
public void init() {
String getval = null;
getval = getParameter("tktext");
tktext = (getval == null ) ? tktext : getval;
getval = getParameter("tkspd");
tkspd = (getval == null ) ? tkspd : (Integer.valueOf(getval).intValue());
getval = getParameter("tkfname");
tkfname = (getval == null) ? tkfname : getval ;
getval = getParameter("tkfsz");
tkfsz = (getval == null ) ? tkfsz : (Integer.valueOf(getval).intValue());
tkfont = new java.awt.Font( tkfname, Font.PLAIN, tkfsz ) ;
getval = getParameter("tkreverse");
if (getval==null) {
tkdirection = "Left";
tkpos = -63000 ;
}
else {
tkdirection = "Right";
tkpos = 63000;
}
this.setBackground(Color.white);
}
public void start() {
tkthread = new Thread(this);
tkthread.start();
}
public void stop() {
tkthread.stop();
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
try {Thread.sleep( 10 ); } catch (InterruptedException e){}
repaint();
}
}
public void paint(Graphics tk) {
tksize = size();
tk.setFont(tkfont);
FontMetrics tkfm = tk.getFontMetrics();
tktexthgt = ( tktexthgt==0 ) ? tkfm.getHeight() : tktexthgt;
tktextwth = ( tktextwth==0 ) ? tkfm.stringWidth( tktext ) : tktextwth;
if (tkdirection=="Left") {
tkpos = ( tkpos <= tktextwth * -1 ) ? tksize.width : tkpos - tkspd;
}
else{
tkpos = ( tkpos > tktextwth ) ? 0 - tksize.width : tkpos + tkspd;
}
tk.setColor(Color.black);
tk.drawString( tktext, tkpos, ( tksize.height + tktexthgt ) / 2 );
}
}
After you've entered the code for Ticker.java into your text editor and saved a copy onto your hard drive, the next step is to compile it into Java bytecodes so that it can be run. If you've got a Java development evironment, see the documentation that came with that kit for information on how to compile your Java applets. If you're using the JDK, you'll use a program that comes with the JDK called javac.
To use javac from from either a DOS prompt or the UNIX command line, enter this:
javac Ticker.java
Note |
This command assumes that javac is located somewhere in your execution PATH (javac is in the java/bin directory that comes with the JDK) and that Ticker.java is located in the current directory. In addition, the CLASSPATH variable also needs to be defined to include the main Java classes (usually java/lib/classes.zip) and the current directory "." (the "dot" directory). For more informtion on setting up the JDK, see Sun's Java Freqently Asked Questions files at http://java.sun.com/faqIndex.html for more information. If you're using Windows 95, the Win95/Java FAQ at http://www-net.com/java/faq/faq-java-win95.txt will also be useful. |
If everything goes as planned, after a few seconds-or minutes, depending on the speed of your computer-your cursor will return to the command line, and a new file called Ticker.class will have been created in the current directory.
If the javac compiler detects any errors, you'll see something that looks a bit like this:
C:\samsgold\java>javac Ticker.java
Ticker.java:15: ';' expected.
Dimension tksize = null
^
Ticker.java:49: ';' expected.
this.setBackground( Color.white )
^
2 errors
The number following the colon indicates the line where the problem occurred, and the message after the number indicates the reason for the error. On the next line, the source for the problem line is displayed with a caret (^) indicating the fault's position in the line.
If you received any errors, go back and edit Ticker.java to fix the problems, and then try recompiling the applet. When you have a "good" compile of Ticker.class, you're ready to add the applet to your Web pages.
After your new applet is compiled, you need to include it on a Web page to test it. This section shows you how to include the Ticker Tape applet on a Web page and how to include prebuilt applets written by other people as well.
To include an applet on a Web page, you use the <APPLET> tag, which looks something like this:
<APPLET CODE="name.class" WIDTH=pixels HEIGHT=pixels></APPLET>
In the CODE attribute, you place the name of the Java class to be run (that class file should be in the same directory as your HTML file), and in the WIDTH and HEIGHT attributes, you must declare the width and height of the drawing area (or canvas) to be used by the applet. If you do not include the WIDTH and HEIGHT attributes, the applet will not appear on the page.
Based on this information, you could include the Ticker Tape applet in a Web page by writing this:
<APPLET CODE="Ticker.class" WIDTH=400 HEIGHT=75></APPLET>
In this basic form, when you load the Web page, the Ticker applet is displayed by using the default values set in the init() method discussed in Exercise 25.1.
Note |
So that your Web browser can locate the applet code, place the Ticker.class file in the same directory as the Web page. |
The <PARAM> tag is used inside the <APPLET> tag to define parameters for the applet-for example, in the ticker tape applet, for the text to scroll by on the window, or for the Animator applet you learned about in Chapter 9, to indicate the names of the individual frames of the animation or the speed at which to play them.
Parameters passed to the applet are usually queried in an applet's init() method. In the init() method of Ticker.class, for example, several calls were made to a method called getParameter(). This call interrogates the parameters contained in the <APPLET> tag, looking for parameters that match the name declared in the getParameter() call, as shown here:
getval = getParameter("tktext");
tktext = (getval == null ) ? tktext : getval;
In this example, getParameter("tktext") tells Java to look for a parameter called tktext between the <APPLET> and </APPLET> tags. If such a value is located, the text associated with the parameter, rather than the default message text, is scrolled through the Ticker Tape window.
To define tktext as a parameter inside the <APPLET> tags, you use the <PARAM> tag, which takes the following form:
<PARAM NAME="tktext" VALUE="Exercise - Scroll this text in the Ticker
Tape window">
When used inside the <PARAM> tag, the NAME attribute is assigned the parameter name, and the VALUE attribute is assigned the information to be passed to the applet.
If you take a closer look at the init() code, you'll see four other parameters that can also be set for Ticker.class:
NAME="tkspd" | Sets the scroll speed; 1 is the slowest value. |
NAME="tkfname" | Sets the font name; Times Roman is the default. |
NAME="tkfsz" | Sets the font size; 12 point is the default. |
NAME="tkreverse" | Reverses the scroll direction. |
By combining these attributes, you can tailor the appearance of the Ticker Tape applet in various ways. For example, when they're used as shown in the following HTML source, the result is a Web page like the one shown in Figure 25.6.
<HTML>
<HEAD>
<TITLE>Exercise - Ticker.class</TITLE>
</HEAD>
<BODY>
<H1>Ticker Tape Java Exercise</H1>
<HR>
<P ALIGN=CENTER>
<APPLET CODE="Ticker.class" width=400 height=50>
<PARAM NAME="tktext"
VALUE="Exercise - Scroll this text in the Ticker Tape window">
<PARAM NAME="tkspd" VALUE="1">
<PARAM NAME="tkfname" VALUE="Arial">
<PARAM NAME="tkfsz" VALUE="28">
<PARAM NAME="tkreverse" VALUE="Yes">
</APPLET>
</P>
<HR>
</BODY>
</HTML>
Figure 25.6: A scrolling ticker tape window.
You may have noticed that the <APPLET> tag has an opening and closing side. Although you can include as many <PARAM> tags as you need inside the <APPLET> tags, you can include other bits of HTML or text in between those tags as well.
The text and HTML between the <APPLET> and </APPLET> tags is displayed by browsers that do not understand the <APPLET> tag (which includes most browsers that now support Java). Because your page may be viewed in many different kinds of browsers, it's a very good idea to include alternate text here so that readers of your page who don't have Java will see something other than a blank line: an image, perhaps, or a bit of HTML to replace the applet.
With a little extra work, you can add many other features to Ticker.class if you want to. You could include parameters to control the color of the text or background, the capability to display text from a separate HTML document in the Ticker Tape window, or even fancy borders.
You might be surprised to hear that some good examples of enhanced Ticker Tape classes are already available to download from sites on the World Wide Web-saving you the hassle of coding all these features yourself. To locate most of these sites, take a look at the Gamelan directory-http://www.gamelan.com/.
Note |
Many of the Java classes currently available include source code you can freely use in your own applets. Before using anyone else's code, however, check the copyright requirements the author expects you to meet. Some authors ask for mention and possibly a hyperlink to their site, whereas others expect nothing. |
Because Java applets can be contained anywhere on the Web and run on any platform, you can incorporate Java applets that have been developed by other people into your Web pages. In some cases, you don't even need a copy of the Java class on your own computer; you only need to know where it is located. If you're starting to feel as though this Java thing is a bit beyond you, or you feel as though you just don't have the time to spend learning all of Java's intricacies, this may be the ideal way of using Java. You don't have to do any coding-you just have to configure someone else's applet to do what you want in your own HTML code.
For example, an enhanced version of the Ticker Tape class you just learned about is contained on the CD for this book and stored at http://www.lne.com/Web/Examples/Professional/chap25/TickerT.class. To use this class in your own Web pages, you have two options. You can install the class at your own Web site, or you can simply include the location of the class as part of your <APPLET> tag. If used in this second form, the <APPLET> tag will look something like this:
<APPLET CODE="TickerT.class" HEIGHT="30" WIDTH="400"
CODEBASE="http://www.lne.com/Web/Examples/Professional/chap25/">
The difference between this example and the one you used for the basic Ticker.class file is in the inclusion of the CODEBASE attribute, which contains a URL that describes the location of the directory where the class file is located.
Note |
To find out about the latest features of TickerT.class, point your Web browser to http://www.webcom.com/taketwo/Ticker.shtml. This page contains information about the supported parameters and describes how you can download the file yourself. The author of the ticker tape applet (and of this chapter), Wes Tatters, requests that if you do decide to use this applet on your Web pages, please include a link to his home page at http://www.webcom.com/taketwo/. |
A quick exploration of the Gamelan site will reveal other sites that also offer classes you can incorporate into your own Web pages. Take, for example, the J_tools site shown in Figure 25.7. This collection includes applets that display animated bullets, multicolored wavy text, and different types of horizontal rules. To find out more about how you can use these applets in your own pages, take a look at http://www.crl.com/~integris/j_tools.htm.
A copy of the J_tools applet class has been included on the CD-ROM that accompanies this book, along with a collection of sample applets your you to experiment with. The following list indicates who created each applet and describes what they do:
As you discovered in this chapter, Java has the potential to change forever the face of Web publishing, but at the same time, its capabilities require some effort to come to grips with. At the same time, however, Java applets are remarkably easy to incorporate into your Web page. All it takes is an <APPLET> tag and a few corresponding <PARAM> tags.
To learn more about what Java has to offer, point your Web browser to http://java.sun.com/, and join the journey into the next generation of Web publishing.
Q | I keep getting errors when I try to test an applet locally by using Navigator 2.0, yet it compiled correctly and works fine across the Internet. What am I doing wrong? |
A | Some versions of Netscape 2.0 contain a bug that prevents them from reloading applets. The only way to fix this problem is by exiting Netscape 2.0 and restarting it. |
Q | People keep telling me that I should not use Java because it's supported only by Navigator 2.0 and HotJava. It this true? |
A | Currently, yes, this is indeed the case, and as I write this, HotJava supports only older versions of Java applets that don't work with the version supported by Netscape. However, many other browser manufacturers have signed up in support of Java, and so Java will most likely become more popular in the future. In the meantime, because you can use alternatives to Java inside the <APPLET> tags, you can use Java applets without penalty to other browsers (assuming, of course, that you do provide alternatives to the applet inside the <APPLET> tags. |