Showing posts with label Java Virtual Machine. Show all posts
Showing posts with label Java Virtual Machine. Show all posts

Saturday, August 15, 2009

Java Applet Source Code

Java applet source code is written in the same way as Java application source code, with a text editor. The difference is that Java applets do not have a main method. Instead, they have several other methods that are called by the VM when requested by the browser. Here is the source code for the simple FilledBox applet:
import java.awt.*;
import java.applet.Applet;
/**FilledBox displays a filled, colored box in the browser window*/
public class FilledBox extends Applet {
// This variable stores the color specified in the HTML document Color boxColor;
/** Get the box color from the host HTML file*/
public void init() {
String s;
s= getParameter("color");
//The default color is gray
boxColor = Color.gray;
//We expect a parameter called color, which will have
//the value red, white, or blue. If the parameter
//is missing, s will be null
if (s !=null) {
if (s.equals("red")) boxColor=Color.red;
if (s.equals("white")) boxColor=Color.white;
if (s.equals("blue")) boxColor=Color.blue;
}
}
/**Paint the box in region assigned to the applet.
* Use the color specified in the HTML document
*/
public voic paint(Graphics g) {
g.setColor (boxColor);
g.fillRect (0, 0, size().width, size().height);
}
}
A main method is required by all Java applications; it is conspicuously absent in this applet. Java applets do not have any required methods at all. However there are five methods that the VM may call when requested by the Web browser (or appletviewer):

public void init ()
Initializes the applet Called only once.

public void start ()
Called when the browser is ready to start executing the initialized applet. Can be called multiple times if user keeps leaving and returning to the Web page. Also called when browser deiconified.

public void stop ()
Called when the browser wishes to stop executing the applet. Called whenever the user leaves the Web page. Also called when browser iconified.

public void destroy ()
Called when the browser clears the applet out of memory.

public void paint(Graphics g)
Called whenever the browser needs to redraw the applet.

If the applet does not implement any of these methods, the applet will have no functionality for the specific method not implemented. In the example, init and paint are implemented. The init function obtains the desired box color from a parameter in the host document. The paint method draws the filled box in the browser window.
Save this Java applet source as FilledBox.java.

Monday, August 10, 2009

Web Browser Applet processing

A Java-enabled Web Browser follows a specific series of steps when it encounters an <APPLET> tag in an HTML document.

1. The browser reserves space in the document for displaying the applet. The WIDTH and HEIGHT parameters of the <APPLET> tag determine the amount of space used by the applet.

2. The browser reads the parameters from the <PARAM> tags.

3. The VM starts and is asked to load and initialize the applet. The applet has access to the names and values in the <PARAM> tags.

4. The VM creates a running copy of the applet based on the class file.

5. The browser calls the applet's init method, so the applet will initialize itself.

6. The VM calls the start method of the applet when it is ready for the applet to start processing. It also calls paint to draw the applet in the browser window.

7. Whenever the applet need to be redrawn (for example, when the user scrolls the applet into view), the browser calls the applet's paint method.

8. The browser calls the stop method when the user moves onto another HTML document.

9. The broswer calls the destroy method when it clears the applet out of memory.

HTML for Java Applets

HTML files are text files with special character sequences that specify the document formatting characteristics. The special character sequences are called tags, and they consist of symbols places between left and right angle brackets, as shown in the following excerpt: Here is some normal text. < I> Here is some italic text.< / I > The output will be as follows, Here is some normal text. Here is some italic text. Most HTML tags use the < tag > and < / tag > sequences to set and unset their relevant properties. For example, < B>turns on bold, and < / B > turns it off. Other tags, such as the paragraph tags may not require an end tag. A complete HTML file has both formatting and structure tags: < html> < head> < title> Sample HTML Document</title> < /head> < body> < h1>HTML Demo< /h1> This document is a sample of HTML. < /body> < /html> The < H T M L > tag indicates that the file is an H T M L document. The < H E A D > tag marks the start of an invisible header section normally used for recording the title and author of the document. Some programs will only look at the header section of a document. The phrase between the < T I T L E> and < / T I T L E > tags is the name of this document. The body section of the document, marked by the < BODY> tag, contains all the display information; in this case, a level-one heading (signified by < H 1 > and < / H 1 > ) and a line of normal text. To include an image in an HTML file, use the < I M G > tag and specify the name and location of the image you want to load. You can use the full URL of the image; a simpler relative reference can be used if the graphic is located on the same server as the HTML file itself: < html> < head> < title> Sample HTML Document< /title> < head/> < body> < img src="Twinkle.gif" /> < h1>HTML Demo</ h1> This document is a sample of HTML. < /body> </ html> If you want to connect this page to another document via a hypertext link, you must insert an anchor tag (< a>). Everything between the anchor tag and the end anchor tag will be highlighted, so the user knows that the lighlighted test or graphics can be clicked on. The following will build a hypertext link to Techno Talk's group on google in the sample document. < html> < head> < title> Sample HTML Document< /title> < /head> < body> < img src="Twinkle.gif" /> < h1>HTML Demo</ h1> This document is a sample of HTML. < p>Share your expertise< a href=">http://groups.google.com/group/nanonagle-tech">Join Techno Talk Group< /a> </ body> </ html> The paragraph tag (< P > ) makes the test easier to read. A Web browser ignores excess spaces and new lines when displaying a document, so if you need to break a line or begin a new paragraph, you must insert < P > or < B R > tags as necessary.

How Java works

As with many other programming languages, java uses a compiler to convert human-readable source code into executable programs. The java compiler generates architecture-independent bytecodes. The bytecodes can be executed only by a Java Virtual Machine (VM), which is an idealized java architecture, usually implemented in software rather than hardware. Java bytecode files are called class files because they contain a single Java class. The vast majority of Java programs will be composed of more than one class file.
To execute Java bytecodes, the VM uses a class loader to fetch the bytecodes from a disk or a network. Each class file is fed to a bytecode verifier that ensures the class is formatted correctly and will not corrupt memory when it is executed. The bytecode verification phase adds to the time it takes to load a class, but it actually allows the program to run faster because the class verification is performed only once, not continuously as the program runs.
The execution unit of the VM carried out the instruction specified in the bytecodes. The simplest execution unit is an interpreter, which is a program that reads the bytecodes, interprets their meaning, and then performs the associated function. Interpreters are generally much slower than native code compilers because they continuously need to look up the meaning of each bytecode during execution.
Fortunately there is an elegant alternative to interpreting code, called Just-in-Time (JIT) compilation. The JIT compilers convert the bytecodes to native code instructions on the user's machine immediately before execution. Traditional native code compilers run on the developer's machine, are used by programmers, and produce non portable executables. JIT compilers run on the user's machine and are transparent to the user; the resulting native code instructions do not need to be ported because they are already at their destination.

Search Techno Talk

Google Groups
Subscribe to Techno Talk
Email:
Visit this group