Unit No.1
Unit No.1
UNIT :- I
Applet
Applet
1. Applets do not use the main() method for initiating the execution of the code
2. Unlike stand alone application, applets can not be run independently
3. Applets can not read form or write to the files in the local computer
4. Applets can not communicate with other servers on the network
5. Applets can not run any program from the local computer
Preparing to Write Applets
Before we try to write applets, we must make sure that Java is installed properly, and also
ensure that either the Java appletviewer or a Java enabled browser is available
Steps involved in developing and testing in applet are
Our applet code use the services of two classes, namely, Applet and Graphics from the
Java class library.
Applet class which is contained in the java.applet package provides life and behaviour of
applet through its methods such as init(), start(), paint(), close().
Therefore Applet class is the life cycle of applet.
paint () method of the Applet class, when it is called, actually displays the result of the
applet code on the screen. Output may be text, graphics, sound etc.
paint() method, which requires a Graphics object as an argument is defined as
public void paint(Graphics g)
This requires that the applet code import the java.awt package that contain the Graphics
class.
All output operation of an applet are performed using the methods defined in the Graphics
class.
Building an Applet Code
//File Name is :- AppletClassName.java
import java.applet.Applet;
Process-1) Import Applet and Graphics import java.awt.Graphics;
class from applet and awt package
respectively public class AppletClassName extends
Process -2) Define applet class from
Applet
{
extending the properties of Applet class
-----------------
Process-3) Override the method paint public void paint(Graphics g)
that shows the result of applet code {
Process -4) By using object of Graphics -----------
class access the methods that perform -----------
the all operation of applet }
• Compile this file to creating a .class file ---------------
---------------
for execution of applet code
}
Testing a Applet Code
We know the applet are programs that reside on web pages. In order to run a
Java applet, it is first necessary to have a web page that references that applet.
Basically web page made up of text and HTML tags that can be interpreted by
web browser or an applet viewer.
A web page also called as HTML page
A web page is marked by an opening HTML tag <HTML> and a closing
HTML tag </HTML> and its divided into three major sections:
1. Comment Section (Optional)
2. Head Section ( Optional)
3. Body Section
Testing a Applet Code
<HTML>
Comment Section
<!
---- -------
-------------
>
Head Section
<HEAD>
Title Tag
</HEAD>
Body Section
<BODY>
Applet Tag <applet code = “AppletClassName.class” width =“300”
</BODY> Height=“300”>
</applet>
</HTML>
Save as .html and use appletviewer filename.html commaned to
Applet Life Cycle
Basic Methods of Applet
public void start()- start() called after the init() method and it starts an applet
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello Java", 100,100);
}
}
/*
<html>
<body>
<applet code= "HelloJava.class" width ="300" height="300"
>
</applet>
</body>
</html>
*/
*/
Example
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void init()
{
resize(200,200);
setBackground(Color.cyan);
}
public void paint(Graphics g)
{
g.drawString("Hello Java", 100,100);
}
}
/*
<html>
<body>
<applet code= "HelloJava.class" width ="300" height="300" >
</applet>
</body>
</html>
*/
import java. applet.*; }
import java. awt.*; }
1) getCodeBase() :- JAVA will allow the applet to load the directory from
which the applet’s class file was loaded
Get the base url
2) getDocumentBase():- JAVA will allow the applet to load the data from the
directory holding the HTML file that started the applet (document base)
Get URL of the document in which the applet is embedded.
JApplet
Create an applet with three text fields and four buttons add, substract, multiply and
divide. User will enter two values in the Text Fields. When any button is pressed, the
corresponding operation is performed and the results is displayed in the third text fields.
Assignment Deadline :-