Saturday, May 16, 2009

Tutorial 1 - Objects, Applications & Applets

NOTE : -  THIS IS NOT THE ORIGINAL CONTENT


Tutorial 1 - Objects, Applications & Applets

The first Java tutorial covers the topic of objects/classes as well as outlining the basic structure of Java applications and applets.

What Are Objects

Java is an object-oriented programming language. But what are objects? An object is a self-contained entity which has its own private collection of properties (ie. data) and methods (ie. operations) that encapsulate functionality into a reusable and dynamically loaded structure. After a class definition has been created as a prototype, it can be used as a template for creating new classes that add functionality. Objects are programing units of a particular class. Dynamic loading implies that applications can request new objects of a particular class to be supplied on an 'as needed' basis. Objects provide the extremely useful benefit of reusable code that minimizes development time.

The Basic Structure of an Application

A Java application resembles a program in most compiled languages. Compiled bytecode resides on the user's machine and is executed by a run-time interpreter that has to be user installed.

Previously you wrote a simple 'hello world!' application to test the development environment. Now comes the explanation of the basic structure of a Java application using it as an example. Applications are stand alone programs and are executed by using a Java interpreter.

/* The HelloWorldApp class implements an application * that displays "Hello World!" to the standard output. */ public class HelloWorldApp { public static void main(String args[]) { // Display Hello World! now System.out.println("Hello World!"); } }


The first two lines is a comment statement about the application's function. Comments are not required but are extremely useful as documentation within the source. Other notes and doc files may get lost but these stay right where they are most useful. A long comment starts with a /* or /** and ends with a */ Short one line comments begin with // and end with the .

The third line starts with the reserved word public. This indicates that objects outside the object can invoke (ie call or use) it. The reserved word class indicates that we are building a new object specification (aka template) with the name that follows. HelloWorldApp is the class name (your choice) and is case sensitive. Java 'style' capitalizes only the first letter of each class name. Line four is an opening curly bracket that marks the start of the class definition.

Line five is the specification of a method (aka procedure in older languages). static indicates that it calls a class method and not an 'instance' method. The method's name is main and the reserved word void indicates that no result is returned. main has parameters in round brackets. String args[] indicates that any command line argument values given are being passed to main as an array of String type in the variable args. Line six has an opening bracket for the main() method.

Line eight invokes the println() method of the system.out object. The data to be printed is passed in the argument as a string datatype. Note that each Java statement concludes with a semicolon. Finally, closing curly brackets are used for the main and for the object definition.

The Basic Structure of an Applet

A Java applet produces object code which can be interpreted within the user's browser. This means that naive users do not have to fuss with program installation. Applets also provide security by restricting local user resource access. Applets can be useful, user friendly programs as demoed by ChemJava or a lot of fun as seen on Jigzone.

Here is the explanation of the basic structure of a Java applet using the one that you wrote to test your working environment. Applets are placed in XHTML documents and are executed from within a Java aware browser.


/* The HelloWorld class implements an applet that displays "Hello World!" within an XHTML document. */ import java.awt.*; import java.applet.*; public class HelloWorld extends Applet { public void init() // Initialize the canvas { Color lightgray=new Color(211,211,211); // customize setBackground(lightgray); // make mellow background resize(150,10); } public void paint(Graphics g) // Display Hello World! { g.drawString("Hello World!",50,25); } }


Line three uses the reserved word import which indicates that objects from external libraries awt and applet are going to be used. Line four uses the reserved word extends to indicate that the class being created is a subclass of the Applet class. This demonstrates how Java code is reused and extended.
Note: The JApplet class is used instead of Applet whenever Swing GUI is used.

Line six starts the override (change) of the java.applet.Applet class init() method. Line eleven invokes the resize()method of the Applet object and sets the window dimensions. Note the all important statement ending semicolon.

Line 12 is another method override declaration. This time it is the paint() method of the Applet object. It is being passed an object of the Graphics class called g. Line 14 tells the Graphics object g to invoke its method drawString()using the string "Hello World! and position it at point (50,25) in the previously assigned window. A later tutorial gives details on graphics programming within applets.

Viewing Applets in XHTML Docs

Applet elements are placed inside the body element of an XHTML document. An applet skeleton is as follows:

Browser does not support Java applets!



code points to the previously compiled applet class that is contained in a separate file. codebase points to the folder that the class file is in. A good choice is place the class folder under the html document. Be sure to explicitly point at the codebase starting at the html document. If you need to start at a local root, use file:///C:/. Anything else fails in FireFox! height and width are also required parameters and cannot be moved into style rules. Nested paramelement(s) are optional and can be used to feed data to the applet from the XHTML document. An example would be The applet would use varColor=getParam("var_color"); to access the value. Always include a message for non-java browsers between the applet element tags. The resulting display is:

Note 1: Remember that filenames are case sensitive in Java. Some browsers may allow helloworld.class but Opera obeys the rule! Always test any applet on as many browsers as you can.

Note 2: Although styling attributes such as align, hspace and vspace are mentioned in many texts, they are archaic and should be replaced by CSS style rules. And of course all XHTML documents should have a doctype and validate to current w3.org recommendations.

Note 3: The applet element has been deprecated and may only be used in Transitional documents. To use a Strictdoctype you must convert applet elements to object elements.The equivalent object element syntax is more complex as FireFox/Opera have native support for Java while MSIE/Safari use plugins. Refer to object elements for a demonstration and source code.

Life Cycle of an Applet

Each Java applet goes through a sequence of stages. They are created (initialized), displayed (painted), paused while off screen (stopped, started) and finally removed (destroyed) when over.

Initialization occurs when the applet is first loaded. Tasks performed here are creating objects, setting initial state, loading images, fonts, etc. and setting parameters. The method used is init()

Display is how things are drawn on screen whether it is text, graphics or background. Redisplay occurs many time through the life of an applet. The method used is paint(Graphics g).

Stopping occurs when one leaves a page that contains a running applet. Threads normally continue running but can be manually stopped. The method used is stop().

Starting occurs after initialization and after stops occur. Tasks include starting threads, sending messages to helper objects, or to tell the applet to start running. The method used is start().

Destruction cleans up an applet before the browser exits. Tasks include thread stopping. Normally the methoddestroy() is not overridden.

Cautions About Applet Use

The greatest problem with using applets is there is no defined standard implementation method. Some browsers support Java natively (ie. self contained) but this leads to version compatibility issues. Other browsers use a Java plug-in module. But this sometimes requires that the viewer download and install Java. And there are some primitive browsers that offer no Java support at all. Most browsers also open a box that will overwrite any text that the box is scrolled to (ie a high z-index value). At this point JavaScript looks like a more consistent method of utility delivery!

For security purposes, Java applets normally allow NO access to user resources! Local files can not be accessed. Only the current website server can be communicated with. Printers can't be used. If your project can live with those restrictions then an applet may be more appropriate than an application.

Note: Digitally signed applets revealing the author can be accepted by a user and run with full application privileges.


BIBLIOGRAPHY / REFERENCE : -

FOR REFERENCE....PLEASE VISIT :


P.S. : THIS CONTENT IS TAKEN FROM THE ABOVE MENTIONED SITE

No comments:

Post a Comment