Nov 26, 2024
Nov 26, 2024
In the last session we saw how an application is made. Lets now check out how to make an applet. Applet make your experience more alive than an application. This is because applets can easily be downloaded onto your browser in a short time and execution time is quiet less.
Creating applets is different from creating a simple application, because Java applets run and are displayed inside a Web page with other page elements and as such have special rules for how they behave. Because of these special rules for applets in many cases (particularly the simple ones), creating an applet may be more complex than creating an application.
For example, to do a simple applet, instead of merely being able to print a message, you have to create an applet to make space for your message and then use graphics operations to paint the message to the screen.
Note: If you run the last sessions simple java application as an applet, the "This is a simple Java program" message prints to a special window or to a log file, depending on how the browser has screen messages set up. It will not appear on the screen unless you write your applet to put it there.
In the following example, you create that simple "This is a simple Java program"
Applet , place it inside a Web page, and view the result.
First, you set up an environment so that your Java-capable browser can find your HTML files and your applets. Much of the time, you'll keep your HTML files and your applet code in the same directory. Although this isn't required, it makes it easier to keep track of each element. In this example, you use a directory called HTML that contains all the files you'll need.
mkdir HTML
Now, open up that text editor and enter Listing b)
Listing b) The Simple Java Program applet.
1: import java.awt.Graphics;
2:
3: public class SimpleJavaApplet extends java.applet.Applet {
4:
5: public void paint(Graphics g) {
6: g.drawString("This is a simple java applet!", 5, 25);
7: }
8:}
Save that file inside your HTML directory. Just like with Java applications, give your file a name that has the same name as the class. In this case, the filename would be SimpleJavaApplet.java.
Now I believe the first question to hit your mind would be what should be the essential features to note about applets? There are a couple I'd like to point out:
The import line at the top of the file is somewhat analogous to an #include statement in C; it enables this applet to get access to the JDK classes for creating applets and for drawing graphics on the screen. The paint() method displays the content of the applet onto the screen. Here, the string Simple Java gets drawn. Applets use several standard methods to take the place of main(), which include init() to initialize the applet, start() to start it running, and paint() to display it to the screen. You'll learn about all of these in the future.
Now, compile the applet just as you did the application, using javac, the Java compiler.
javac SimpleJavaApplet.java
Again, just as for applications, you should now have a file called SimpleJava.class in your HTML directory.
To include an applet in a Web page, you refer to that applet in the HTML code for that Web page. Here, you create a very simple HTML file in the HTML directory (see Listing c).
Listing c) The HTML with the applet in it.
1:
2:
3:
4:
5:
My Java applet says:
6: <APPLET CODE="SimpleJavaApplet.class" WIDTH=150 HEIGHT=25><APPLET>
7: </BODY>
8: </HTML>
You refer to an applet in your HTML files with the <APPLET> tag. You'll learn more about <APPLET> later on, but here are two things to note:
Use the CODE attribute to indicate the name of the class that contains your applet.
Use the WIDTH and HEIGHT attributes to indicate the size of the applet. The browser uses these values to know how big a chunk of space to leave for the applet on the page. Here, a box 150 pixels wide and 25 pixels high is created.
Save the HTML file in your HTML directory, with a descriptive name (for example, you might name your HTML file the same name as your applet – SimpleJavaApplet.html). And now, you're ready for the final test – actually viewing the result of your applet. To view the applet, you need one of the following:
browser that supports Java applets, such as Netscape 2.0 and above.
The appletviewer application, which is part of the JDK. The appletviewer is not a Web browser and won't enable you to see the entire Web page, but it's acceptable for testing to see how an applet will look and behave if there is nothing else available.
If you don't have a Web browser with Java capabilities built into it, you can use the appletviewer program to view your Java applet. To run appletviewer, just indicate the path to the HTML file on the command line:
appletviewer HTML/SimpleJavaApplet.html
Tip: Although you can start appletviewer from the same directory as your HTML and class files, you may not be able to reload that applet without quitting appletviewer first. If you start appletviewer from some other directory (as in the previous command line), you can modify and recompile your Java applets and then just use the Reload menu item to view the newer version.
Java's strengths lie in its portability—both at the source and at the binary level, in its object-oriented design—and in its simplicity. Each of these features help make applets possible, but they also make Java an excellent language for writing more general-purpose programs that do not require HotJava or other Java-capable browser to run. I guess from here, you now have the foundation to create more complex applications and applets.
Next: Statements & Expresssions
09-Jun-2001
More by : Deepak Chandrasekaran