Showing posts with label applet. Show all posts
Showing posts with label applet. Show all posts

Sunday, August 16, 2009

Using Javac

The Javac compiler works the same on applets as it does on Java applications:

javac FilledBox.java

Here are a few tips that may help you get started. First, applet classes must always be declared public or they will not get compiled. Also, remember that Java is case-sensitive; Filledbox.java is not the same as FilledBox.java and will not be compiled.

If the Java code is acceptable to the compiler, the only message you will see is about a deprecated API:

Note: FilledBox.java uses a deprecated API. Recompile with "-deprecation" for details. 1 warning.

For now ignore the warning. As long as there were no error messages, the file FilledBox.class will be created. If there were error messages, you need to go back and fix your code. There are many different types of error messages that the compiler may generate when given a source file. The simplest to fix are syntax errors, such as a missing semicolon or closing brace. Other messages will highlight incorrect use of variable types, invalid expressions, or violation access restrictions.

Getting your source code to compile is only the first part of the debugging process; error-free compilation does not guarantee that your program will do what you want.

Creating an HTML file to host your applet

<HTML> <HEAD> <TITLE>Sample HTML Document With Filled Box</TITLE> </HEAD> <BODY> <H1>FilledBox Demo</H1> <P> <APPLET CODE="FilledBox.class" WIDTH=50 HEIGHT=50> <PARAM NAME=color VALUE="red"></APPLET> </BODY> </HTML> You can create this file by simply typing it into a text editor. Save the file as FilledBox.html. HTML files can be named anything you like, although it is common practice to name them after the applets they host.

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.

Adding Java applet to HTML

There is an <APPLET> tag that specifies the location of the class file and the display area allocated to the applet. Suppose you want to add a clock applet that will display the current time in hours, minutes, and seconds. Suppose we have a compiled clock applet called Clock.class. A simple example of an <APPLET> tag that loads the Clock applet:

< APPLET = "Clock.class" width="200" height="60">< / A P P L E T >

When a browser encounters these tagsm it will start the VM and ask it to load Clock.class. It also tells the VM that the applet may draw in a region that is 200 x 60 pixels. The location of the <APPLET> tag in the document determines the coordinate of the top left of the applet's display area.

< 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>

<P>

< APPLET = "Clock.class" width="200" height="60">< / A P P L E T >

</ body>

</ html>

As you can see, embedding applets into Web pages is simple. Java is able to create plug-in components that can be used by novices as well as experts. For this component strategy to work, the HTML author must be able to customize the properties and behaviour of the applet via HTML. The Java programmer decides which parameters will have meaning for the applet, and the HTML author uses <PARAM> tags to pass initial parameters to the applet. The clock applet needs no parameters, telling the time is a universal function.

Monday, August 3, 2009

JAVA Libraries

There are two types of libraries: Core library: Core library means JAVA Development Kit (JDK). Optional Library: Optional Libraries means Programmer defined Libraries. The Core Libraries must be present in every implementation of JAVA. JDK version 1.2 has 12 core libraries with a total of 1391 classes. Every library has different classes, and every class has different functions. Core JAVA Libraries in JDK 1.2 are Name: JAVA.Lanugage Class: 93 Description: Basic rushed support for JAVA, Threads reflection and exception Name: JAVA.applet Class: 4 Description: Applet (Web Page) support. Name: JAVA.awt Class: 298 Description: Windows and GUI Buttons, Hex, Bit, etc (Abstract windows tools) Name: JAVA.swing Class: 500 Description: Supports JAVA for GUI development. Name: JAVA.IO Class: 75 Description: Supports input and output. Name: JAVA.util Class: 77 Description: Utility data structure. Name: JAVA.rmi Class: 65 Description: Remote method calls. Name: JAVA.sql Class: 26 Description: Supports database connectivity Name: JAVA.security Class: 106 Description: Supports secure data coding . Name: JAVA.net Class: 38 Description: TCP/IP, IPX/SPX and other network protocols. Name: JAVA.beans Class: 43 Description: Competent software support to remark replied application development by re-use of existing coded fragments. Name: JAVA.text Class: 50 Description: Supports for localized text elements such as date, time and currency. Name: JAVA.math Class: 2 Description: Supports decimal and numeric types in SQL database. Name: JAVAX.accessibility Class: 14 Description: Supports large text size for the visually impaired.

Search Techno Talk

Google Groups
Subscribe to Techno Talk
Email:
Visit this group