Monday, October 18, 2010

Learning Applet In Java


Learning How Applets Work
Now that Java is making the transition from a child prodigy to an established language, it is being used for all kinds of large-scale business software and other applications. However, the core of interest in the language remains in a new type of program that Java made possible: the applet. Applets are programs designed to run as part of a World Wide Web page. When a Java applet is encountered on a page, it is downloaded to the user's computer and begins running.
During this hour you'll be introduced to applet programming. Programming applets with Java is much different from creating applications with Java. Because applets must be downloaded off a page each time they are run, applets are smaller than most applications to reduce download time. Also, because applets run on the computer of the person using the applet, they have numerous security restrictions in place to prevent malicious or damaging code from being run.
The following topics will be covered:
·         Setting up an applet
·         Displaying information in an applet
·         Stopping and starting an applet
·         Putting an applet on a Web page
·         Using applet HTML tags and attributes
Standard Applet Methods
All applets are subclasses of the Applet subclass, which is part of the java.applet package of classes. Being part of this hierarchy enables the applets that you write to use all the behavior and attributes they need to be run off of a World Wide Web page. Before you begin writing any other statements in your applets, they will be able to interact with a Web browser, load and unload themselves, redraw their window in response to changes in the browser window, and other functions.
In applications, programs begin running with the first statement of the main() block statement and end with the last } that closes out the block. There is no main() method in a Java applet, so there is no set starting place for the program. Instead, an applet has a group of standard methods that are handled in response to specific events as the applet runs.
The following are the events that could prompt one of the applet methods to be handled:
·         The program is loaded for the first time
·         Something happens that requires the applet window to be redisplayed
·         The program stops at a specific point
·         The program restarts after a stop
·         The program is unloaded as it finishes running
The following is an example of a bare-bones applet:
public class Skeleton extends java.applet.Applet {
    // program will go here
}
Note that unlike applications, applet class files must be public in order to work. (However, if your applet uses other class files of your own creation, they do not have to be declared public.) This class inherits all of the methods that are handled automatically when needed: init(), paint(), start(), stop(), and destroy(). However, none of these methods do anything. If you want something to happen in an applet, you have to override these methods with new versions in your applet program. The two methods you will override most often are paint() and init().
The paint() Method
The paint()method should be a part of almost every applet that you write because you can't display anything without it. Whenever something needs to be displayed or redisplayed on the applet window, the paint() method handles the task. You also can force paint() to be handled with the following statement:
repaint();
Otherwise, the main reason paint() occurs is when something is changed in the browser or the operating system running the browser. For example, if a Windows 95 user minimizes a Web page containing an applet, the paint() method will be called to redisplay everything that was on-screen in the applet when the applet is later restored to full-size.
Unlike the other methods that you will be learning about during this hour, paint() takes an argument. The following is an example of a simple paint() method:
public class paint(Graphics screen) {
    // display statements go here
}
The argument is a Graphics object. The Graphics class of objects is used to handle all attributes and behavior that are needed to display text, graphics, and other information on-screen. (You'll learn about drawString(), one of the methods of the Graphics class, later this hour.) If you are using a Graphics object in your applet, you have to add the following import statement before the class statement at the beginning of the source file:
import java.awt.Graphics;
If you are using several classes that are a part of the java.awt package of classes, use the statement import java.awt.*; instead. It makes all of these classes available for use in your program.
The init() Method
The init() method is handled once--and only once--when the applet is run. As a result, it's an ideal place to set up values for any objects and variables that are needed for the applet to run successfully. This method is also a good place to set up fonts, colors, and the screen's background color.

Caution: Variables and objects should not be created inside an init() method because they will only exist within the scope of that method. For example, if you create an integer variable called displayRate inside the init() method and try to use it in the paint() method, you'll get an error when you attempt to compile the program. Create any variables that you need to use throughout a class as object variables right after the class statement and before any methods.

The start() and stop() Methods
At any point when the applet program starts running, the start() method will be handled. When a program first begins, the init() method is followed by the start() method. After that, in many instances there will never be a cause for the start() method to be handled again. In order for start() to be handled a second time or more, the applet has to stop execution at some point.
The stop()method is called when an applet stops execution. This event can occur when a user leaves the Web page containing the applet and continues to another page. It also can occur when the stop() method is called directly in a program.
In the programs that you'll write as you're starting out with the Java language, start() and stop() will have the most use in animation. You'll learn more about this use during Hour 18, "Creating Animation."
The destroy() Method
The destroy() method is an opposite of sorts to the init() method. It is handled just before an applet completely closes down and completes running. This method is used in rare instances when something has been changed during a program and should be restored to its original state. It's another method that you'll use more often with animation than with other types of programs.

No comments:

Post a Comment