You are on page 1of 19

APPLET Applet is java program that can be embedded into HTML pages.

Java applets runs on the java enables web browsers such as mozila and internet explorer. Applet is designed to run remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic and entertaining.

Advantages of Applet:

Applets are cross platform and can run on Windows, Mac OS and Linux platform Applets can work all the version of Java Plugin Applets runs in a sandbox, so the user does not need to trust the code, so it can work without security approval Applets are supported by most web browsers Applets are cached in most web browsers, so will be quick to load when returning to a web page User can also have full access to the machine if user allows

Disadvantages of Java Applet:

Java plug-in is required to run applet Java applet requires JVM so first time it takes significant startup time If applet is not already cached in the machine, it will be downloaded from internet and will take time Its difficult to desing and build good user interface in applets compared to HTML technology

Applet versus Application

1.Applets as previously described, are the small programs while applications are larger programs. Applets don't have the main method while in an application execution starts with the main method. Applets can run in our browser's window or in an appletviewer. To run the applet in an appletviewer will be an advantage for debugging. Applets are designed for the client site programming purpose while the applications don't have such type of criteria. 2.Applets are designed just for handling the client site problems. while the java applications are designed to work with the client as well as server. Applications are designed to exists in a secure area. while the applets are typically used. 3.Applets are not capable of reading and writing the user's file system. This means that the applet neither can access nor place anything locally. To illustrate this lets take an example.. Many Window based C applications uses the .INF file as the initialization file to store the information about the application and any user preferences in 16-bit Windows or the Registry in 32bit Windows. While in case of current applet it is not possible.

Local and Remote Applet

We can embed applets into webpages in two ways. One, we can write our own applets an embed them into webpages. Second. We can download an applet from a remote computer system and them embed it into a web page. An applet developed locally and stored in a local system is known as a local applet. When a webpage is trying to find a local applet, it does not need to use the Internet and therefore the local system does not require any Internet connection.

A remote applet is that which is developed by someone else and stored on a remote computer connected to Internet. In order to locate and load remote applet, we must know the applets address on the web. The address is known as Uniform Resource Locator.

The Life cycle of An Applet Applet runs in the browser and its lifecycle method are called by JVM when it is loaded and destroyed. Here are the lifecycle methods of an Applet:

init(): This method is called to initialized an applet

start(): This method is called after the initialization of the applet.

stop(): This method can be called multiple times in the life cycle of an Applet.

destroy(): This method is called only once in the life cycle of the applet when applet is destroyed.

init () method: The life cycle of an applet is begin on that time when the applet is first loaded into the browser and called the init() method. The init() method is called only one time in the life cycle on an applet. The init() method is basically called to read the PARAM tag in the html file. The init () method retrieve the passed parameter through the PARAM tag of html file using get Parameter() method All the initialization such as initialization of variables and the objects like image, sound file are loaded in the init () method .After the initialization of the init() method user can interact with the Applet and mostly applet contains the init() method.

Start () method: The start method of an applet is called after the initialization method init(). This method may be called multiples time when the Applet needs to be started or restarted. For Example if the user wants to return to the Applet, in this situation the start Method() of an Applet will be called by the web browser and the user will be back on the applet. In the start method user can interact within the applet.

Stop () method: The stop() method can be called multiple times in the life cycle of applet like the start () method. Or should be called at least one time. There is only miner difference between the start() method and stop () method. For example the stop() method is called by the web browser on that time When the user leaves one applet to go another applet and the start() method is called on that time when the user wants to go back into the first program or Applet.

destroy() method: The destroy() method is called only one time in the life cycle of Applet like init() method. This method is called only on that time when the browser needs to Shut down.

WEB DESIGN AND HTML

It is a type of data file which is transferred to the client machine. The HTML file gets translated and displayed on the screen if the client is using a Web browser like Netscape Navigator, Microsoft Internet Explorer etc HTML stands for Hyper Text Markup Language. An HTML file is a text file containing small markup tags. The markup tags tell the Web browser how to display the page. An HTML file must have an htm or html file extension. An HTML file can be created using a simple text editor. Example: 1. <html> <head><title>Title of page</title></head>

<body> This is my first homepage. <b>This text is bold</b> </body> </html>

What Exactly is HTML? Posted on: June 14, 2007 at 12:00 AM Now lets see what exactly is HTML (Hyper Text Markup Language). It is a type of data file which is transferred to the client machine. What Exactly is HTML?

Now lets see what exactly is HTML (Hyper Text Markup Language). It is a type of data file which is transferred to the client machine. The HTML file gets translated and displayed on the screen if the client is using a Web browser like Netscape Navigator, Microsoft Internet Explorer etc. Let me give you a brief description of HTML.

What is an HTML File?

HTML stands for Hyper Text Markup Language. An HTML file is a text file containing small markup tags. The markup tags tell the Web browser how to display the page. An HTML file must have an htm or html file extension. An HTML file can be created using a simple text editor.

Go ahead and try it! If you are running Windows, start Notepad. If you are using Mac, start SimpleText. Start TextEdit in OSX and change the following preferences: Open the the "Format" menu and select "Plain text" instead of "Rich text". Then open the "Preferences" window under the "Text Edit" menu and select "Ignore rich text commands in HTML files". If you do not change the preferences above, your HTML code will probably not work.

Type in the following text:

<html> <head><title>Title of page</title></head>

<body> This is my first homepage. <b>This text is bold</b> </body> </html><html> <head><title>Title of page</title></head>

<body> This is my first homepage. <b>This text is bold</b>

</body> </html>

Description of the above Example.

We have started the HTML document by using the <html> tag and ended the same by using </html> tag. These two tags tell the browser about the starting and the ending of the document. Points to note:

Although header information doesn't not get displayed in the browser window, we have given the header information by using <html> and </html> tags. Similarly we can give the title of the document by using the <title>, </title> tags which gets displayed in the browser's caption. The <body> tag is used to give a body of the document to be displayed in the browser. The text between the <b> and </b> tags is used to display the text in a bold font.

Remember, use either the .htm or the .html extension while saving an HTML file. Well, it is preferable to use .html extension.

<APPLET> TAG The <applet> tag defines an embedded applet. <APPLET ...> puts an applet in your page. In its simplest use, <APPLET ...> requires the use of CODE (to tell what applet file to load), WIDTH (to tell how wide the applet should be), and HEIGHT (to tell how tall the applet should be). example: <APPLET CODE="MyApplet.class" WIDTH=100 HEIGHT=100>

Passing Parameters To Applets These parameters allow the user to customize the applet's operation. APPLET parameters stored in the PARAM tag actually have little directly to do with HTML. It is the responsibility of the applet to check the parameter values and

respond accordingly. We can increase the applet's flexibility, making the applet work in multiple situations without recoding and recompiling it, by defining the parameters.

The two steps of passing parameters to applets are:

First, add the PARAM tag (with values) to the HTML source file. Second, add necessary code to the applet to retrieve these parameter values.

example: <PARAM NAME=param_name VALUE=param_value>

We use the above syntax to pass the parameters to the applet. In this syntax, param_name and param_value are the values passed to the Java applet. Retrieving Parameters Within the Applet These parameter values are retrieved by Java applets using the getParameter() method. This function is commonly called in the applet's init() method. The method syntax is the following: String getParameter(String name); Don't forget that the parameter names are case-sensitive when passing parameters to applets. Moreover all parameter values are converted to strings.

HTML BASIC TAGS HTML Hyper Text Markup Language

Commands in HTML are called TAGS

Opening Tag and Closing Tag

In HTML tags can be classified as

* Basic Tags * Formatting Tags * List related Tags * Table related Tags * Misc Tags * Forms in HTML * Frames

Basic Tags

1. <HTML> ....</HTML> Marks the beginning and end of HTML document.

HTML document is divided in 2 parts. head and body 2. <HEAD> ...</HEAD> Defined Head part 3. <BODY> .... </BODY> Defines HTML body 4. <TITLE> </TITLE> Specifies the title to be displayed on Title bar. This TAG is written withing HEAD

Steps to Create an HTML document. 1. Open Notepad 2. Type the HTML document

<html> <head><title> Page One</title> </head>

<body> This is HTML body </body> </html> 3. Save it : Extension should be .html First.html 4. Open Browser. File Open | Browse the .html file

* Formatting Tags

Html supports 6 levels of heading starting from H1 to H6

<h1> This is for Heading 1 </h1> <h2 align="Center"> This is for Heading 1 </h2> <h3 align="Right"> This is for Heading 1 </h3> <h4> This is for Heading 1 </h4> <h5> This is for Heading 1 </h5> <h6> This is for Heading 1 </h6>

<b> This is for Bold </b> <i> This is for Italic</i> <u> This is for Underline </u> <b><i><u> This is for Bold, Italic and Underline </u></i> </b>

<code> This is used for wrting coding within document </code> <Address> This is for wirting web addresses</Address>

<BR> this is for break

<hr> This is for horizontal Ruler

<hr width=50%> This is for horizontal Ruler

H<Sub>2 </Sub> O This is for Subscript 2 <Sup> 2 </Sup> This is for Superscript

For changing font name, size and color Font tag is used

<Font color="red" > This is for Red </font> <Font color="red" size=4> This is for Red Size 4 </font> <Font color="red" size=4 face="Arial,Algerian"> This is for Red </font>

Java - Drawing Shapes Example in java:

Java - Drawing Shapes Example in java Posted on: April 14, 2007 at 12:00 AM Applet is a program to run on the browser and it is embedded on the web page. This program is not system level program but it is a network level program. Java - Drawing Shapes Example in java

Introduction

Applet is a program to run on the browser and it is embedded on the web page. This program is not system level program but it is a network level program. The Applet class is a super class of any applet. Applet viewer is used to view or test the applet whether the applet is running properly or not.

In this program we will see how to draw the different types of shapes like line, circle and rectangle. There are different types of methods for the Graphics class of the java.awt.*; package have been used to draw the appropriate shape. Explanation of the methods used in the program is given just ahead :

Graphics.drawLine() : The drawLine() method has been used in the program to draw the line in the applet. Here is the syntax for the drawLine() method :

drawLine(int X_from_coordinate, int Y_from_coordinate, int X_to_coordinate, int Y_to_coordinate);

Graphics.drawString() : The drawSring() method draws the given string as the parameter. Here is the syntax of the drawString() method :

drawString(String string, int X_coordinate, int Y_coordinate);

Graphics.drawOval() : The drawOval() method draws the circle. Here is the syntax of the drawOval() method :

g.drawOval(int X_coordinate, int Y_coordinate, int Wdth, int height);

Graphics.drawRect() : The drawRect() method draws the rectangle. Here is the syntax of the drawRect() method :

g.drawRect(int X_coordinate, int Y_coordinate, int Wdth, int height)

EXAMPLE:import java.applet.*; import java.awt.*;

public class CircleLine extends Applet{ int x=300,y=100,r=50;

public void paint(Graphics g){ g.drawLine(3,300,200,10); g.drawString("Line",100,100); g.drawOval(x-r,y-r,100,100); g.drawString("Circle",275,100); g.drawRect(400,50,200,100); g.drawString("Rectangel",450,100); } } HTML FILE FOR THE PROGRAM <HTML> <HEAD> </HEAD> <BODY> <div align="center"> <APPLET CODE="CircleLine.class" WIDTH="800" HEIGHT="500"></APPLET> </div> </BODY> </HTML>

Java - Drawing Shapes Example using color in java n this program there are several types of methods have been used to fill or set the color of line, text, circle background and rectangle background. These are given just ahead :

Graphics.setColor() : This is the setColor() method which is the Graphics class method imported by the java.awt.*; package. This method sets the color for the object by specified color. Here is the syntax of the setColor() method :

g.setColor(Color.color_name);

Graphics.fillOval() : This is the fillOval() method used to fill the color inside the oval by specified color. Here is the syntax of the fillColor() method :

g.fillColor(Color.color_name);

Graphics.fillRect() : This is the fillRect() method used to fill the color inside the rectangle by specified color. Here is the syntax of the fillRect() method :

g.fillRect(int X_coordinate, int Y_coordinate, int Wdth, int height)

Example:import java.applet.*; import java.awt.*;

public class ShapColor extends Applet{ int x=300,y=100,r=50;

public void paint(Graphics g){ g.setColor(Color.red); //Drawing line color is red g.drawLine(3,300,200,10); g.setColor(Color.magenta); g.drawString("Line",100,100);

g.drawOval(x-r,y-r,100,100); g.setColor(Color.yellow); //Fill the yellow color in circle g.fillOval( x-r,y-r, 100, 100 ); g.setColor(Color.magenta); g.drawString("Circle",275,100);

g.drawRect(400,50,200,100); g.setColor(Color.yellow); //Fill the yellow color in rectangel g.fillRect( 400, 50, 200, 100 ); g.setColor(Color.magenta); g.drawString("Rectangel",450,100); } } Html file for the Code above: <HTML> <HEAD> </HEAD> <BODY> <div align="center"> <APPLET ALIGN = "CENTER" CODE = "ShapColor.class" WIDTH = "800"> HEIGHT = "500"></APPLET> </div>

</BODY> </HTML>

JAVA -Abstract Window Toolkit (AWT). A class library is provided by the Java programming language which is known as Abstract Window Toolkit (AWT). The Abstract Window Toolkit (AWT) contains several graphical widgets which can be added and positioned to the display area with a layout manager. As the Java programming language, the AWT is also platform-independent. A common set of tools is provided by the AWT for graphical user interface design. The implementation of the user interface elements provided by the AWT is done using every platform's native GUI toolkit. One of the AWT's significance is that the look and feel of each platform can be preserved. The GUI application or applet is created in three steps. These are:

Add components to Container objects to make your GUI. Then you need to setup event handlers for the user interaction with GUI. Explicitly display the GUI for application.

TAKING INPUT IN APPLETS For taking input in Applets AWT(Abstract Window Toolkit ) is used. textField which is a GUI component from AWT is used to get values entered from user. Also , button is used to submit the values entered in textField. Steps to take input from user: 1.Create an applet. 2.Import java.awt package to use all gui containers. 3.Now add a textField on the applet and name it as per need. 4.Add a button to the applet. this button will contain the logic about the value of textfield.

Example:- Applet for addition of two Numbers. package javaapplication1;

public class NewApplet extends java.applet.Applet { public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } }

private void initComponents() { textField1 = new java.awt.TextField(); textField2 = new java.awt.TextField(); button1 = new java.awt.Button(); textField3 = new java.awt.TextField(); label1 = new java.awt.Label(); label2 = new java.awt.Label(); label3 = new java.awt.Label(); button1.setLabel("Add"); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { button1ActionPerformed(evt); } }); label1.setText("Number One"); label2.setText("Number Two"); label3.setText("Result"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Al ignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(79, 79, 79) .addGroup(layout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup() .addGap(63, 63, 63) .addGroup(layout.createParallelGroup(java x.swing.GroupLayout.Alignment.TRAILING) .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(textField3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle. ComponentPlacement.RELATED, 154, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.T RAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING, false) .addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle. ComponentPlacement.RELATED, 121, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING) .addComponent(label2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(textField2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(95, 95, 95)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Al ignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(49, 49, 49) .addGroup(layout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING)

.addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing. GroupLayout.Alignment.TRAILING) .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(54, 54, 54) .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(32, 32, 32) .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.Componen tPlacement.RELATED) .addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(65, Short.MAX_VALUE)) ); } private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_button1ActionPerformed float n1 = Integer.parseInt(textField1.getText()); float n2 = Integer.parseInt(textField2.getText()); float res = n1+n2; textField3.setText(String.valueOf(res)); } private java.awt.Button button1; private java.awt.Label label1; private java.awt.Label label2;

private private private private }

java.awt.Label label3; java.awt.TextField textField1; java.awt.TextField textField2; java.awt.TextField textField3;

Html file for the applet: <HTML> <HEAD> <TITLE>Applet HTML Page</TITLE> </HEAD> <BODY> <H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3> <P> <APPLET codebase="classes" code="javaapplication1/NewApplet.class" width=350 height=200></APPLET> </P> <HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT> </BODY> </HTML>

You might also like