Sunday, February 17, 2008

Coffee With JAVA-Part One



Here I am to start a series of sessions on JAVA, the programming language of the future! In this first session we will see the most fascinating feature of JAVA the "Applets". Applets are applications that are transported across the Internet, accessed from a Web Server and installed automatically on a client system and run as a web document. The really good aspect of applets is that they access only a limited part of the client's memory and thus reduces the risk of virus attacks and ensures data integrity.


A SAMPLE JAVA APPLET

import java.awt.*;
import java.applet.*;
"angle brackets"applet code="App1" width="200" height="60""angle brackets"
public class App1 extends Applet{
public void paint(Graphics g){
g.drawString("My First Applet",20,20);
}
}

The Explanation for this code is given below:
  • The "awt" class in JAVA called as Abstract Windows Toolkit must be imported. JAVA applets communicate with the user using awt tools only not using the conventional console input and output methods.
  • The "Applet" class must be imported for every applet in JAVA and the applet class must be an extension of the Applet main class.
  • The paint() method belongs to the "awt" class in JAVA and takes a graphics device context as an argument. It is defined in awt and must be overridden in the extended class.
  • Inside the "App1" class you have another method "drawString()" which is the member of "Graphics" class. It takes three arguements, the text to be displayed, the x and y coordinates on the screen.
  • The JAVA browser window starts at (0,0) on the left top of the window as the origin.
  • The HTML code at the top specifies the size of the window, in the place of "angle brackets" type "<".
Compiling and Running the JAVA applet:
  • Run "cmd" from your start menu. The DOS interface opens. Using "cd" command move to the appropriate folder where your JAVA compiler is stored. Usually its better to copy the JAVA compiler so that it has the path C:>\jdk1.3\bin>.
  • Type edit filename.java at this instant, in our case, App1.java and type the program shown above in the editor window that opens.
  • Save the program and you will be at the command prompt again.
  • To compile and run:C:>\jdk1.3\bin>appletviewer App1.java.
  • The output looks like this

Features:

  • Applets do not have main() method.
  • Applets must be run only under java compatible browsers
  • I/O consoles dont work with Applets.
...............The Aroma will continue.

2 comments:

Unknown said...

hey nice work da.. carry on.. i'll run those programs and check after i install java..:-)

Chandran said...

Thanks da!! Wish everyone learns JAVA easily this way..

Powered By Blogger