Any Web browser that's going to have a life on the Internet must be able to play Java appletssmall applications designed to run directly in a browser. There's no real trick to running Java applets. In any Java-enabled Web browser, you simply
click
on a link for a Java applet, and it starts running. It doesn't matter whether you're running Windows 3.1, Windows 95, Mac OS, or UNIX; the application starts. Many Web pages even contain embedded Java scripts that automatically play when you visit the
page. These scripts enable Web page developers to add spice to their pages with small animations and moving text.
So, this is going to be a short chapter, right? Well, not exactly. To demonstrate the power of Java and give you a glimpse of what it will offer in the future, this chapter shows you several Java applets in action. In addition, you'll get a glimpse of
Java source code (so you can adapt existing Java applets for your own use), and a list of places to go to learn more about this amazing new technology.
Before Java, the Internet was pretty much a one-way street into your computer. Graphics, sound, and video could pour into your computer, but you couldn't respond to it or interact in any way. Your computer was just a slow TV set that offered video and
sound on demand. Sure, you could fill out forms and request information from a server, but you couldn't interact as you can with a typical application. Java makes the Internet more of a two-way street, enabling you to interact and receive immediate
feedback.
Java, developed by Sun Microsystems, is a programming language that enables programmers to develop applications that can run on any platforma PC running Windows, Mac OS, all versions of UNIX, you name it.
To run Java applets, you need a Java-enabled Web browser, such as Internet Explorer 3.0. Such a Web browser comes with a built-in Java interpreter that converts the raw Java source code into an application that can run on your computer. When you
click on a link for a Java applet, Internet Explorer 3.0 downloads the Java code and interprets it. The Java applet then runs inside the Internet Explorer 3.0 window or inside its own window. You don't need a special helper application or add-in to run
Java appletsthe interpreter is an integral part of Internet Explorer 3.0.
Currently, Java is a nifty programmer's toy. Java applets are typically small and unsophisticated. You might find a Java applet that displays a tiny animation on a Web page, an interactive game of tic-tac-toe, or even a calculator that can determine
your monthly payments on a loan. But you won't find many Java applets that can help you create a newsletter or manage a database. . .at least not yet.
Java's future is much more exciting. As developers determine ways to break the speed barriers inherent in modem transfers, and in the Java technology itself, as programmers gain experience with Java, and as the Java code develops, remote computing
over
the Internet is fast transforming from dream into reality. One day, you'll be able to run your word processor and spreadsheet programs directly from the Internet, instead of having them clutter up your hard drive.
TIP
You shouldn't run out and buy a $500 Internet computer just yet. Many people who have chosen to rely solely on the Internet for all their computing needs find that these computers fall far short of their expectations. Without local storage space (a hard drive) and memory, these computers aren't very practical for most people right now, but their future looks rosy.
The Internet is no place for the paranoid. Any file (even a harmless-looking document file) might have some sort of virus hiding in it that can play havoc with your computer. Not so long ago, somebody developed a virus for Word documents that executed
as a macro. You just can't trust anything anymore.
Java is billed as a safe programming language. According to Sun Microsystems, Java has built-in safeguards to prevent programming vandals from writing viruses into applets and scripts. But such claims only motivate the vandals to try harder. Netscape's
boast that its Internet security features were safe only succeeded in inspiring the formation of a Web site called Hack Netscape, where fun-loving hackers attempt to break the code and prove Netscape Corporation wrong.
To completely avoid viruses, you have two options. The first option is to cut the cable that connects your computer to the Internet, never run noncommercial programs, and prevent your friends, colleagues, and relatives from touching your computer.
Yeah,
right. The second, more practical option is to back up your hard drive and run an anti-virus program on a regular basis, avoid downloading files from questionable sites, and try not to worry so much.
Playing Java applets is a no-brainer. If the applet is embedded in a Web page, all you have to do is load that page and watch. As with any inline graphic or video clip, or background audio recording, Internet Explorer 3.0 downloads the information and
displays (or plays) the result. Figure 17.1 shows an embedded Java applet, although this picture doesn't give you the complete effectthe angels are flapping their wings.
Figure 17.1. Embedded Java scripts can provide animation or moving text.
To play some Java applets, you must click on a link, and then wait. If you have a slow modem connection and the Java applet is fairly large, you have to wait some more as Internet Explorer 3.0 downloads and interprets the Java code. When the
downloading
and interpreting are complete, the applet runs. Figure 17.2 shows a relatively complex applet that lets you play a crossword puzzle.
Figure 17.2. Many Java applets enable you to play games on the Internet.
TIP
After you've downloaded an applet, it's on your machine until you close the window in which the applet is running. If you plan on playing awhile, you can disconnect from the Internet to avoid using up your connect time (if that's an issue).
With the preliminaries out of the way, you can now skip around the Web and take a look at some Java applets in action. The following sections take you on a tour of some popular Java applets to give you a taste of what's out there and a peek into the
future of computing on the Internet.
These sections focus on various types of applets. First, you will take a look at simple Java scripts and applets that enhance Web pages. You will then take a short break and have some fun with interactive Java games. Following the break, you will
travel
to some sites that host Java multimedia and educational applets. And finally, you will take a look at Java applets that perform useful tasks, such as helping you determine loan payments.
Web page creators are always looking for new ways to grab the attention of passers by. They play with fonts, add graphics and maps, plop a table here or there, and slap in a few decorative lines. Without a hook, a Web page is doomed to function only as
a stepping stone to the next page. And no Web page creator wants that.
Java gives Web page developers more tools to work with. With Java, pages can contain background audio, lively text, animations, and even guided tours.
Figure 17.3 shows a sample enhancement called CrazyText, which Patrick Taylor boldly ripped off and modified from Daniel Wyszynski's NervousText applet. (Borrowing and adapting existing Java code is common and acceptable, as you'll see later.)
CrazyText
bounces around on the screen, making you think you have some weird screen flicker thing going.
Another cool example of text enhancement is the LED sign at
http://www.cs.hope.edu/~dbrown/java/LEDSign/WWW/LED.html
This sign provides an LED display that scrolls a message across your screen.
Figure 17.3. CrazyText animates plain text.
Text isn't the only boring Web page element that you can jazz up with Java. Visit the HotButtons applet at the following address to see what I mean:
http://www.brainlink.com/~cauldron/plugNPlay/hotButton.html
Instead of sitting still on a page, these buttons take on various incarnations and emit sounds when you click them. (See Figure 17.4.)
Figure 17.4. HotButtons are animated control panel buttons that sound off.
One of my personal favorites is Bob Cat the Java Cat, shown in Figure 17.5. Here, you are greeted by the creator's cat, a sassy feline who sticks its tongue out at you and makes a tiny, obnoxious sound. To visit Bob, go to
http://www.conveyor.com/face/face.html.
Figure 17.5. Bob Cat shows how a simple animation can catch your attention.
TIP
The examples shown here are a small sampling of Java applets and scripts. If you're interested in taking a look at additional samples, visit Gamelan (pronounced GAMma-lohn) at
http://www.gamelan.com/
This page has hundreds of links to interesting sites that have incorporated Java applets in their Web pages.
The simple text and graphics enhancements you just saw are fairly unsophisticated. They make nice Web page decorations, and they're fairly easy to program. To see more sophisticated uses of Java, play a few Java games. You can find several games on the
Web, including memory, tic-tac-toe, interactive crossword puzzles, and even a few rudimentary arcade games.
One such arcade game is Alien Landing (see Figure 17.6). The goal is to land the Alien spaceship on a flat piece of ground. . .softly. You use the Left and Right Thruster buttons to move the ship left and right, and use the Down Thruster button to
soften the landing. It's not as easy as it sounds.
Figure 17.6. In Alien Landing, you help the alien touch down softly on flat ground.
If you like Alien Landing, try Star Wars with the Gipper at
http://www.galcit.caltech.edu/~doug/computer/java/sdi/sdi.html
At this site, a hold-out from the cold war, you're greeted by the face (and voice) of Ronald Reagan. Click on the hammer and sickle dots, and watch Ronnie toast the Russkies with his superhuman glare.
If you've worn out your mouse playing Solitaire and FreeCell, and you're looking for a card game to break in your new mouse, you'll find plenty of Java card games on the Web.
If you like to gamble, try the Black Jack game at
http://www.well.com/user/yimmit/BlackJack.html
Here, the house graciously starts you out with $100. You place your wager ($1, $2, $5, $10, or $25), and try to score as close to 21 as possible without going over. If you lose your money, just click on Reset Bank to start another game (see Figure
17.7).
Figure 17.7. Play Black Jack against the computer.
If you're into more intellectual games, try WebChess at
http://www.delorie.com/game-room/chess/
Or try out a glorified version of tic-tac-toe, Connect Four, at
http://server.snni.com/~mfo/java/connect4/index.html
If you didn't get your fill of multimedia in Chapter 16, "Using Graphics, Sound, and Video," you can experience multimedia through Java. Java applets commonly have pointers to audio and image files, and to animations that can keep you
entertained.
If you like to watch other people's slide shows, visit Suresh Srinivasan's slide show from his 1988 trek to the north at
http://www.thomtech.com/~suresh/java/alaska/
Here, you can click your way through the slides one by one, as if your computer were transformed into a slide projector. To listen to the sound attached to a particular slide, click the Audio button (see Figure 17.8). If you have a modem connection,
you'll have to be patient; Internet Explorer 3.0 downloads all the graphics before you can start playing the slide show.
Figure 17.8. This slide show applet enables you to click through a series of electronic slides.
The Real Graffiti page at the following address displays a brick wall that you can decorate with your own graffiti:
http://militzer.me.tuns.ca/graffiti/graffiti.html
When you connect to this site, you're given a virtual can of spray paint, a color selection palette, an eraser (for detail work), and a sandblaster (see Figure 17.9). If you think that your creation is worthy, you can submit it for permanent
application
to The Wall. Only the best 25 submissions make The Wall of Fame.
Figure 17.9. Real Graffiti lets you spray paint your way to stardom on the Net.
One of the most exciting ways in which Java is being applied is in the educational field. With interactive Java applets, students can take online tests and receive immediate feedback, visit interactive labs, increase their vocabulary, take foreign
language lessons, and much more. Of course, the electronic classroom hasn't entirely replaced the neighborhood school, but you can get a taste of what's to come by taking a look at some Java applets designed for students.
One slightly morbid and not very educational site is the CyberShark Feeding Zone at
http://www.cse.ucsc.edu/~raptor/
Here, you watch as a shark chases his next meal across the screen (see Figure 17.10). He never seems to catch them, although maybe I just didn't stick around long enough to find out. Click the Start Simulation button at the bottom of the page to view
population models.
Figure 17.10. Students can watch a virtual shark chase his virtual meal.
If you're interested in Physics, check out the Virtual Laboratory at
http://jersey.uoregon.edu/vlab/
In this Physics lab, any student can pull his nose out of a text book and see conceptual models of the various laws and equations of physics (see Figure 17.11). Here, students can see examples illustrating potential and kinetic energy; the
relationships
between volume, temperature, and pressure; and the diffusion process. They can even peer through a virtual telescope!
Figure 17.11. The virtual lab shows how Java can make texts more interactive.
As with most technologies, Java is also being applied to business and finance. You'll find Java applets that can help you figure your taxes, determine the monthly payment on a loan, and even shop for mutual funds.
Karl's Mortgage Calculator at
http://www.broadcom.ie/~kj/java/mortgage.html
is one of the best examples of a financial Java applet. The calculator is a simple spreadsheet. You enter the principal (amount you are borrowing), the term (number of years), and the interest percentage (see Figure 17.12). Karl's Mortgage Calculator
determines your monthly payment and the total amount of interest you will pay over the term.
Figure 17.12. You don't have to rely on what your real estate agent tells you.
If you plan on investing money rather than borrowing it, check out WSRN's Mutual Fund Lookup applet at
http://www.wsrn.com/java/WSRNLookup.html
If you know the symbol of a mutual fund or stock you are interested in, simply type the symbol and click Search. If you don't know the symbol, click on Symbol Lookup and enter the name of the stock or mutual fund.
TIP
For additional Java applets, try the JavaSoft Applets page at
http://www.javasoft.com/java.sun.com/applets/applets/
The Applet Warehouse at
http://www.appletware.com/findus/
or Café del Sol at
http://www.xm.com/cafe/
These sites provide a wide selection of demos, betas, and completed applets that you will no doubt find amusing, if not useful.
If you create your own Web pages and you'd like to spice them up with Java applets, you don't have to be a master programmer. Many of the Java applets covered in this chapter are available for you to customize and use.
Java developers commonly post their source code on the same page (or a connected page), along with a list of parameters. The source code is the Java code that makes up the applet. Parameters are items that you can replace to customize the
applet. You can download the Java code, replace the parameters, and stick the applet in your Web page.
For example, the Bob Cat animation you visited earlier contains parameters that define the size of the graphics and which graphics to use. The Bob Cat animation uses two graphics: a full-face view of Bob the Cat and a small, cropped image of his
tongue.
It also contains a pointer to a sound file (see Figure 17.13).
Figure 17.13. The source code for Bob Cat the Java Cat.
You might want to replace the Bob Cat image with an image of yourself, and replace the tongue image with pictures of your ears (to show them wiggling, for example). Of course, you would also have to change the parameters that control the location of
the
ears. However, changing these parameters is much easier than writing a Java applet from scratch.
If you are interested in writing your own Java scripts and creating Java applets, see Part VII, "Web Scripting and Active X," for details. Also, refer to Part VI, "Building a Web Site with MS FrontPage," for Web sites that provide
the latest information about Java.
If you're interested in writing your own Java applets, there's no better place to study than on the Web. Sun Microsystems and other companies and individuals are eager to teach what they know and keep everyone informed of the latest standards. Use the
following list of sites to start your journey into Java programming:
NOTE
Another great resource for learning Java is Teach Yourself Java in 21 Days by Laura Lemay (Sams.net Publishing, ISBN 1-57521-030-4). You can find it wherever fine computer books are sold.
In this chapter, you saw how Java applets make the Web a more interactive environment. In the next chapter, you will look at another tool that enables Web page developers to create interactive, multimedia applications for the Web. And, you'll learn
where to get the helper applications and add-ons you need to play them.