Java Applets Are Objects

 <  Day Day Up  >  

Java Applets provide a great example of what an object actually is because they are totally self-contained and have both attributes and methods . I use this example quite a bit to explain the concept of an object.

Defining an Applet

First, let's define an applet . Applets were actually on the leading edge of the Internet boom. My first exposure to Java was a demo that Sun provided with one of the first beta releases of Java. The demo was that of a bouncing head. Applets were introduced to provide dynamic content to Web pages. Prior to the mid-1990s, most Web content was static: basically text and hyperlinks . As the Web became more widespread, people understandably wanted flashier Web sites.


What makes applets really interesting with regard to this book is that they are self-contained objects. Up to this point, all the Java code listings contained in this book are applications. In Java, you can create two kinds of programs: applications and applets. Applications are standalone programs that can be executed independently. Applets, on the other hand, cannot be executed independently ”they must be embedded inside an HTML file. In this way, applets are similar to JavaScript. The following code is an example of a very simple applet that displays the words "Hello World Applet" in the browser.

 
 // Java Applet import java.applet.*; import java.awt.*; public class HelloWorld extends java.applet.Applet {         public void paint (java.awt.Graphics g) {             g.drawString("Hello World Applet", 50, 25);         } } 

Both JavaScript code and applets are literally embedded in HTML code. We have already seen some JavaScript code. Now take a look at the following HTML code, which executes a Java applet.

 
 <HTML> <HEAD> <TITLE>Java Example</Title> </HEAD> <BODY> This is my page<br> Below you see an applet<br> <br> <APPLET Code="HelloWorld.class" width=200 Height=100> </APPLET> </BODY> </HTML> 

An applet can only be executed within a browser.

The Object Tag

Although the applet tag <APPLET> was created to work with Java applets, a newer tag called the object tag ( <object>) , is now used to embed various types of objects in Web pages, including Java applets.


To illustrate the object properties of an applet, consider an applet that provides a banner, which displays several messages that fade in and out one at a time. This applet has all the properties of a valid object ”attributes and behaviors. The attributes are things such as color , height, width, and so on. In this case, the behavior is the ability to fade in and out. So, we have met the definition of an object, as the applet has both attributes and behaviors.

Applet Inheritance

A Java applet actually inherits many of its attributes and behaviors from the Java Applet class. This is yet another way that an applet illustrates what an object actually is.


One of the more interesting things about an applet is that it provides a great example of the distributed nature of an object. Consider that an applet is an object that travels across a network. When a Web page is loaded into a browser and an applet tag is encountered , the browser (client) attempts to load the applet by making a request to the server ”the same server that contained the original HTML page. This applet is a self-contained object, and is sent across the wire into the browser as attributes and behavior. At this point, a Java virtual machine, which is part of the browser, is launched to execute the applet.

Consider the example of an applet that displays a flashing banner on the Web page. The code that makes the banner flash is actually part of the object. The attributes that represent the color and so on are also part of the object. Even though the color attribute is part of the object, you can actually change the default value by providing a parameter in the HTML code. In this way, each HTML document that uses the applet can be customized. You can actually send parameters to the Java applets by using the following code.

 
 <param Name=NameOfParameter Value="ValueOfParameter"> 

Because Java is actually a programming language, it is much better suited to sophisticated programming tasks than JavaScript, or any of the scripting languages. For example, Java can handle the Dynamic Object Model (DOM) object hierarchy described in Chapter 12, "Objects and XML: Portable Data." Java can also create sophisticated user interfaces using its Swing capabilities.

Swing

Swing is the Java package that provides robust user interface capabilities for applets as well as applications. Information on Swing can be found at the Sun Microsystems Web site at http://www.javasoft.com.


 <  Day Day Up  >  


Object-Oriented Thought Process
Object-Oriented Thought Process, The (3rd Edition)
ISBN: 0672330164
EAN: 2147483647
Year: 2003
Pages: 164
Authors: Matt Weisfeld

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