Saturday, February 19, 2011

EVERY ONE UNDERSTANDABLE JAVA SIMPLE STEP BY STEP APPROACH

http://www.tripod.lycos.com/

A First Java Program

Writing and compiling the program
How does the program work?
Writing and compiling the program

Here is a simple Java program (about the simplest one you can get that actually produces a result you can see on the screen) for you to type in to the computer. Type it into a text file, exactly as it stands including the correct case for the letters, and save it using the name first.java.

import java.awt.*;
import java.applet.*;

public class first extends Applet
{
public void paint (Graphics g)
{ g.drawString("Hello",100,100);
}
}
This program is designed to display a simple "Hello" message on the screen - what a lot of commands simply to display one stupid little word! When you save it on the disc, it is important that you give the file the same name as the word that appears before extends Applet in the program. In this case, the name of both the program and the file is first, so the file must be called first.java.

Having entered the program, compile it using the following instruction:

javac first.java
If you are presented with a message to the effect of "I can't find the program javac", or "I can't find the file first.java" for that matter, then you should alter the path variable on your computer so that it recognises both the place where the Java compiler is stored and the place where you have placed the first.java file. If that sounds a little daunting, then copy the first.java file to the same subdirectory on the disc where the Java compiler is sitting. Then, having moved to that subdirectory, run the compile instruction again.

Assuming that you have entered the program correctly, the compiler should finish with a message to the effect that the file has been compiled correctly. What's more likely is that there will be errors, in which case you must edit the file to correct them.

When the file compiles correctly, you will find that an extra file, first.class, has been created on the disc. This contains the compiled Java code. What we have created is the code for an applet, and it should be run using a web page. Create a simple text file containing the following code:


The text file can have any name, but it must have the extension .htm or .html This file extension is what tells your operating system that the file contains code for a web page.
The file must be saved in the same folder as the .class file. Otherwise, when you run it as a web page, it will try to call the applet and not be able to find it!
You can view the web page (thereby running the applet inside it) either by activating your browser and opening the file specifically, or by using a program such as My Computer and double clicking on the file icon on the screen.
If you like, you can add other HTML tags to the file, to turn it into an all-singing all-dancing web page. However, most of the time, you will just want to see the Java applet without any distractions, so you will probably want to restrict to the and tags.
The applet tag contains a reference to the class file where the code is to be found, and also specifies the width and height of the rectangle that appears on the screen. The applet runs within this rectangle, and any output produced by the program that would appear outside this rectangle simply doesn't appear!

If everything has gone to plan, you should see a rectangle on the screen similar to the one you saw on the previous page, except the message will be slightly different and there won't be a black rectangle on it. The applet window background may be either grey or white. If you don't see the applet, then you will probably see the "Can't find the item" icon (a red cross on my computer) - either your browser is not Java enabled (get a more up-to-date version!) or the HTML file is stored in a different position to your class file.

How does the program work?

Let's go through the program step by step to understand it fully. The first two lines are

import java.awt.*;
import java.applet.*;
These refer to two files which contain various pre-compiled routines. They are library files, and our program can use any of the routines within them if it needs them. The 'applet' file contains various routines that allow applets to work. The 'awt' file contains the Abstract Windows Toolkit routines. You may well write programs that require other library files to be included, in which case, feel free to add more import statements.

public class first extends Applet
{
}
The main part of the program is included within a class definition. Classes can contain variables, methods (i.e. subroutines), structures - all sorts of things, and a Java program is always defined as a class. This class is public, which means that other programs (our web browser, specifically) can access and run it, and the class is of the Applet type (we say it extends the Applet type), i.e. the class represents an Applet program.

The statements making up the applet - and there may be thousands of them - are enclosed within the curly brackets after the first line of the class definition. Within those curly brackets there is only one method definition, defining a method called paint. This is a subroutine which tells the applet what the screen should look like:

public void paint (Graphics g)
{ g.drawString("Hello",100,100);
}
The method name paint, with a single parameter of type Graphics is a special one. It doesn't have to be called explicitly by the program. You will notice that there isn't a statement in the file that calls this routine paint.

The parameter g is defined to be of type Graphics. This means that it is a graphics environment variable. The type Graphics is in fact another class, which contains methods for drawing things on the screen. Since g is defined to be of that class, it inherits all these methods, and we call one of them here.

The call g.drawString("Hello",100,100) (which, like all statements in Java, must be followed by a semicolon) calls the drawString function within the variable g. A String is a sequence of letters and punctuation, and this method displays them on the screen. The three things passed to the method are the string itself (the message Hello within the double quotation marks), and the x- and y-coordinates where you want it to appear.

N.B. The y-coordinate is not the position where the top-left corner of the message will appear. It is the position of a line on which your message sits. In this case, this means that it indicates the co-ordinates of the bottom-left of the message. Some messages will contain letters such as 'g', 'p' or 'y' that would dangle down




************************************plz check the below link for further continue pagesssss*******
http://richardbowles.tripod.com/java/guide/first.htm

No comments:

Post a Comment