What's So Hot about Java? (Page 1 of 3)




What's So Hot about Java?


IBM makes Java work for developers.

Why are so many developers adding Java to their repertoire? What is all the buzz about? Well, you might say Java takes care of all the "buzzwords" developers are looking for: robust, secure, distributed, object-oriented, network-aware, reusable, modular and so on. But perhaps the best thing about Java is its platform-independence.

Suddenly, the fragmented process of applications development and deployment is simplified. In Java, you are no longer tied to a single platform. You can write once -- and run anywhere. It's a primary reason why IBM is investing heavily in Java and working with key partners such as Sun Microsystems to accelerate the adoption of Java technology.

We encourage you to find out more about Java -- and how it can let you expand your market opportunity, while decreasing your development time.

Where to Start?

Start by browsing this paper about Java's impact on development by Lisa Amini, a team leader at IBM's AIX Systems Center.

Then visit the Education section of this site to learn more about Java and Java-related concepts.


Table of Contents


Getting Started
A Few Basic Java Concepts
Five Steps To Creating an Applet
Painting the Picture
Panels, Widgets, Events
Inter-Applet Communications
Linking Native Methods

EDITOR'S NOTE:
A PDF version of this paper is available for viewing with Adobe's Acrobat(TM) Reader. A free copy of the reader is available from Adobe.



Getting Started


Paving the road to business on the Internet, Java transforms static environments into interactive sites.

Certainly, finding information about Java is no chore. Java is portable and architecture-neutral for any system implementing the Java Virtual Machine.

Programs written in Java can be distributed and executed on any client system that has a Java-enabled Web browser. Developers are freed from having to write multiple versions for multiple platforms and even from having to recompile for each platform. Because the applets run on the client system, scalability and performance are no longer tightly tied to Web server systems. High performance requirements are also addressed through multithreading, the ability to link native code, and the Just-in-Time compiler.
Is it a neat way to do animation over the Web or the technology that could enable the "$500 network PC"? . . . Both!
Although understanding Java may come easily, true confidence is more likely to come through actually working with it. This Website will guide you through working with the Java Development Toolkit for AIX. (Because Java is platform-independent, these exercises will work with other operating systems as well as the AIX, IBM's UNIX environment.)

You will learn to run demonstrations as well as to develop your own example applications and applets. You will see how easy it is to format graphical user interfaces and handle user inputs, to build multithreaded animations, and to communicate among applets and to servers over the network.

The examples provided in this article will provide step-by-step instructions for creating two Java applets, complete with sample code and illustrations.
Is it portable and interpreted or high-performance? . . . Both!

GETTING STARTED

Before starting, we need to get the essentials out of the way. If you haven't already installed the Java Development Toolkit (JDK) and a Java-enabled Web browser, you'll need to do so.

Once you install the JDK 1.0.2, make sure everything is properly in place by invoking the JDK demos. Figure 1 includes commands to enter at a shell (ksh) command prompt to set up and run the JDK demos.

On the export DISPLAY command, replace mydisplay:0 with your display's actual name. Note that when you enter the RunDemos command, three windows (representing the three HTML files in the Animator demo) will be displayed. These demos use the appletviewer to run the applets. The JDK provides the appletviewer, so you can view applets without a Java-enabled browser.
Is Java an object-oriented programming language or a distributed run-time environment? . . . Both!
Later, when we start writing our own applets, we'll use the Navigator browser instead of the appletviewer. If you haven't already noticed, the appletviewer has an Applet menu button. Use this button to Quit an applet, so the next demo will start.

As you invoke the applets, you should notice a few things in preparation for our later examples. First, look at the Animator applet. If you've already gone to the next applet, just enter appletviewer Animator/example1.html. In Java, basic animation is performed in one of three ways:

  • A series of images (GIF, JPEG, or XDM) are displayed in succession
  • A single image is successively redrawn at different coordinates, creating the effect of movement
  • A single image is successively enlarged or reduced, creating the effect of growth or of an explosion

The Animator applet uses the first method, displaying a series of images in succession. In fact, the Animator applet was written so you can use parameters to specify the directory where images are stored, the number of images to store, the time to pause between displaying each image, the audio files to include, and more. To get a better understanding, let's take a quick look at the applet source and images. You can view a single image in your browser by creating a file (call it /tmp/showgif.html) and inserting the line <IMG SRC = / usr/ lpp/ Java/ demo/ Animator/ images/ Duke/ T1.gif>. Then, invoke the Netscape Browser (netscape &) and set the Location field on the menu to file:/tmp/showgif.html. If the netscape command fails, try entering export LANG=C and then restart netscape.

Now let's check out the applet source. Java source code is always stored in files with the postfix .javaAnimator/Animator.java and search for the field getParameterInfo. The first column of this array lists parameters that Animator accepts. To see how to set these parameters, display the Animator/example1.html file and note the param tags. By supplying your own set of image files, you can use the Animator applet to animate any Web page.

The next installment will take you through the basic concepts of creating an applet.


A Few Basic Java Concepts


Now we're ready to start our own applet. Refer to Figure 2.1 for the steps to create an applet. But don't start this procedure yet.

First, you need to understand a few basic Java concepts. Look at the helloWorld.java source file. A key feature of object technology (OT) is reusability. Even the simplest applet takes advantage of this feature. Notice the line creating the helloWorld class "extends java.applet.Applet" . The Applet class is what allows us to avoid rewriting the GUI interface already provided by the browser. Since applets by definition are Java programs invoked from a browser, all applets must extend the Applet class. By extending Applet, we need only to implement those methods and variables we want to override.

Now is a good time to view the API documentation, which is on the Sun Microsystems Inc's Website. The API documentation is very important, so I'd recommend adding it to your browser Bookmarks. Notice the list of Java Packages. In Java almost everything is either a class, an object, or a method. Java groups classes into packages. The Abstract Window Toolkit (AWT or java.awt) package provides the windowing and GUI classes. The java.applet package provides classes required to write a program that can be invoked from within a browser.

In Java, we tell the compiler which class we're referencing by giving the package and class name (for example, java.applet.Applet). If you'll be using a lot of classes from a package, you can simplify your programs by "importing" a package or class. Notice that our helloWorld applet starts with the statement, "import java.awt.*' " , to import all the classes in the AWT package. Imported classes don't need to be prefixed with their package name. For example, if we had imported java.applet.*, our helloWorld class definition could simply extend Applet instead of java.applet.Applet. The java.lang package is the only package automatically imported by the Java compiler. We'll talk more about packages and interfaces in later sections.

In the API documentation, select the java.applet link, then the Class Applet link. The Class Applet page describes the variables and methods that are part of the Applet class. (Don't worry if this looks foreign right now, but if you plan to write applets on your own later, it's helpful to start learning how to use the API documentation.) Notice the list of methods included in the Applet class. The key points are:

  • When the page containing your applet is referenced, the browser will call the init(), then the start() methods.
  • If the user leaves your page, the browser will call the stop() method.
  • If the user returns to your page, the browser will call the start() method again.
  • When the user exits the browser, the browser will call stop(), then destroy().

In our helloWorld applet, we can override any of these methods by creating our own versions. In init(), we'd do things like sizing the window, setting background colors, and creating our applet's layout. Ours is a simple applet, so we just set the window size in init() and leave the start(), stop(), and destroy() methods as provided for now.

Notice that our applet has a paint method, but paint() wasn't listed as one of the methods for the Applet class. How does it get called? Since our helloWorld applet extends the Applet class, it's a subclass of Applet. As such, our helloWorld applet inherits the methods and variables implemented in Applet.

Referring to the API documentation for Applet, notice that the declaration for the Applet class indicates that it extends Panel; therefore, it's a subclass of Panel. (Go ahead, click on Panel.) Panel is a subclass of Container, which is a subclass of Component. Component includes a paint() method called by the browser any time the window is updated (resized, redisplayed, or altered in any way). Input to paint() is a Graphics object representing the graphics area of this component (our applet). So for an applet, paint() is crucial. Our simple applet just paints the string "Hello World!" but we'll build on this later.

About now you might be thinking, "This sounds a little too abstract. I mean, how was I supposed to know to go from Applet to Panel to Container to Component just to find the paint() method?" Well, the good news is that you need to know only a few of these fundamental relationships or "flows," and we go through them in this series. The other piece of good news is that as you become familiar with the API documentation, you'll more easily pick up on when to call a method to perform a task and when to override a method.



JavaTM is a trademark of Sun Microsystems, Inc.

Other companies, products, and service names may be trademarks or service marks of others.

Copyright    Trademark



Next Page Table of Contents Java Education Java Home
IBM HomeOrderEmployment