Module IV Applets
Module IV Applets
Local Applets
Remote Applets.
An applet can be a fully functional Java application because it has the entire
Java API at its disposal. There are some important differences between an
applet and a standalone Java application, including the following:
To build the applet code two classes of java library are essential Applet and
Graphics.The Applet class is contained in java.applet package.The Applet class
maintains the life cycle of the applets with its four methods
init(),start(),stop() and destroy().Another paint() is defined by the AWT
Component class which requires a Graphics object as an argument.The
output may be text, graphics or sound.The syntax of the paint() method is:
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
start(): This method is automatically called after the browser calls the init
method. It is also called whenever the user returns to the page containing the
applet after having gone off to other pages.
stop():This method is automatically called when the user moves off the page on
which the applet sits. It can, therefore, be called repeatedly in the same applet.
destroy():This method is only called when the browser shuts down normally.
Because applets are meant to live on an HTML page, you should not normally
leave resources behind after a user leaves the page that contains the applet.
paint():Invoked immediately after the start() method, and also any time the
applet needs to repaint itself in the browser. The paint() method is actually
inherited from the java.awt.
About HTML:
HTML stands for Hyper Text Markup Language, which is the most widely used
language on Web to develop web pages.
HTML was created by Berners-Lee in late 1991 but "HTML 2.0" was the first
standard HTML specification which was published in 1995. HTML 4.01 was a
major version of HTML and it was published in late 1999. Though HTML 4.01
version is widely used but currently we are having HTML-5 version which is an
extension to HTML 4.01, and this version was published in 2012.
<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
HTML is a markup language and makes use of various tags to format the
content. These tags are enclosed within angle braces <Tag Name>. Except few
tags, most of the tags have their corresponding closing tags. For
example <html>has its closing tag </html> and <body> tag has its closing
tag</body> tag etc.
Tag Description
<! This tag defines the document type and HTML version.
DOCTYPE...>
<title> The <title> tag is used inside the <head> tag to mention
the document title.
<html>
<head>
</head>
<body>
</body>
</html>
Any document starts with a heading. You can use different sizes for your
headings. HTML also has six levels of headings, which use the elements <h1>,
<h2>, <h3>, <h4>, <h5>, and <h6>. While displaying any heading, browser
adds one line before and one line after that heading.
<!DOCTYPE html>
<html>
<head>
<title>Heading Example</title>
</head>
<body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Paragraph Example</title>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Hello<br />
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<center>
</center>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<hr />
</body>
</html>
Step1:Write the applet code .(eg public class HelloJava Extends Applet{ })
Step 3: Compile the above java file (javac HelloJava.java) to get HelloJava.class
file.
Step 4: Design a web page using HTML tags and embed the .class
file(HelloJava.class) created in step 3 and save the file with xxx.html extension
Example:
<html>
<body>
</applet>
</body>
</html>
To execute the applet by html file, create an applet and compile it. After that
create an html file and place the applet code(.class file) in html file. Now click
the html file.
//HelloJava.java
import java.applet.Applet;
import java.awt.Graphics;
g.drawString("welcome",150,150);
Note: class must be public because its object is created by Java Plugin software
that resides on the browser.
<body>
</applet>
</body>
</html>
To execute the applet by appletviewer tool, create an applet that contains applet
tag in comment and compile it. After that run it by: appletviewer First.java.
Now Html file is not required but it is for testing purpose only.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
/*
</applet>
*/
{
public void paint(Graphics g)
g.drawString("welcome to applet",150,150);
c:\>javac First.java
c:\>appletviewer First.java
import java.awt.*;
import java.applet.*;
setForeground(Color.red);
}
<html>
<head>
<title>applets program</title>
</head>
<body bgcolor=pink>
<center>
</center>
<applet code=HelloJava.class width=200 height=150 align=right>
</applet>
</body>
</html>
Note: HelloJava.java
HelloJava.class
HelloJava.html
Outputs
import java.applet.Applet;
import java.awt.Graphics;
String str;
if(str ==null)
str="JAVA";
str="hello"+str;
{
g.drawString(str,10, 50);
<html>
<body>
</applet>
</body>
</html>
Output
Getting input from the user
Next step is to retrieve the items from the fields for display of calculations, if
any.Text fileds contains items in string form. They need to be converted to the
right form, before they are used in computations. The results are then
converted back to strings for display.
import java.awt.*;
import java.applet.*;
character length
text2=new TextField(8);
text3=new TextField(8);
add(text2);
add(text3);
text2.setText("0");
text3.setText("0");
try
as strings
x=Integer.parseInt(s1); //for computation convert s1 string to integer
s2=text2.getText();
y=Integer.parseInt(s2);
s3=text3.getText();
z=Integer.parseInt(s3);
catch(Exception e){ }
max=x;
if(y>max)max=y;
if(z>max)max=z;
g.drawString(s,120,75);
repaint();
return true;
}
//UserInput.html file
<html>
<applet code=UserInput.class
width=400
height=200>
</applet>
</html>
Output