0% found this document useful (0 votes)
15 views31 pages

Unit No.1

The document provides an overview of applets in Java programming, distinguishing them from standard applications and outlining their lifecycle and basic methods. It details the steps to create, embed, and test applets within web pages, along with examples of applet code. Additionally, it introduces JApplet, a Swing-based applet that extends the functionality of traditional applets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views31 pages

Unit No.1

The document provides an overview of applets in Java programming, distinguishing them from standard applications and outlining their lifecycle and basic methods. It details the steps to create, embed, and test applets within web pages, along with examples of applet code. Additionally, it introduces JApplet, a Swing-based applet that extends the functionality of traditional applets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Advanced JAVA Programming

UNIT :- I
Applet
Reference Book
E. Balagurusamy, “Programming With JAVA” , Tata McGraw
Hill, 6th Edition

Herbert Schildt, “ JAVA:- The Complete Reference, Tata


McGraw Hill, 11th Edition

Pune Institute of Computer Technology E&TC Department


Applet

Java Programs are available in two flavours


Application :- It is similar to all other kind of programs like in C , C++
etc. to solve a real time problem
Applet :- Applet is small Java programs that are primarily used in
internet computing
They can be transported over the Internet from one computer to
another and run using the Applet viewer or any web browser that
supports Java.
An applet , like any application program, can do many things for us. It
can perform arithmetic operations, display graphics, play sounds,
accept user input, create animation and play interactive games.
A web page can now contain not only a simple text or a static image
but also a Java applet which run graphics, sounds and moving images.
Local and Remote Applet

We can embed applets into web pages in two ways


One , we can write our own applet and embed them into web
pages
Second, we can download an applet from a remote computer
system and then embed into a web page.
How Applets Differ from Applications
Applets and stand alone applications are Java programs
Significant difference between them:- Applets are not full featured
application programs.
They are usually written to complete a small task or a component of a
task.
They are usually designed for use on the internet

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

1. Building an applet code (.java file)


2. Creating an executable applet (.class file)
3. Designing a web page using HTML tag
4. Preparing <APPLET>tag
5. Incorporating <APPLET> tag into the web page
6. Creating HTML file
7. Testing the applet code
Building Applet Code
 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
 Process-1) Import Applet and import java.applet.Applet;
Graphics class from applet and awt import java.awt.Graphics;
package respectively
 Process -2) Define applet class from public class AppletClassName
extending the properties of Applet extends Applet
class {
 Process-3) Override the method -----------------
paint that shows the result of applet public void paint(Graphics g)
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
</BODY> =“300” Height=“300”>
</applet>
</HTML>
Save as .html and use appletviewer filename.html
Applet Life Cycle
Basic Methods of Applet

public void init() – To initialize or pass input to an applet

public void start()- start() called after the init() method and it
starts an applet

public void stop()- To stop running applet

public void paint(Graphics G)- To draw something within an applet

Public void destroy() – To remove an applet from memory


completely
Example :- Print the Hello Java on Applet Window

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.*; g.drawString(msg,10,30);
import java. awt.*; }
}
public class Sample extends Applet
{
String msg; /*
public void init() <html>
{ <body>
setBackground(Color.red); // set background <applet code="Sample.class" width="300"
and forground color height="300">
of applet </applet>
window </body>
setForeground(Color.black); </html>
msg="Hello Friends .... "; // initialize the */
string to dispaly
}
public void start()
{
msg += "How are you?....";
}
public void paint(Graphics g)
{
msg+= "Bye…….";
import java. applet.*; {
import java. awt.*; msg+= "Bye…….";
g.setFont(f1);
public class Sample extends Applet g.drawString(msg,10,30);
{ }
String msg; }
Font f1;
public void init()
{ /*
setBackground(Color.red); // set background <html>
and forground color <body>
of applet <applet code="Sample.class" width="300"
window height="300">
setForeground(Color.black); </applet>
msg="Hello Friends .... "; // initialize the </body>
string to display </html>
f1 =new Font (“Aerial”,Font.BOLD, 18); */
}
public void start()
{
msg += "How are you?....";
}
public void paint(Graphics g)
Passing a Parameter to Applet
import java.applet.Applet; /*
import java.awt.Graphics; <html>
<body>
public class Test extends Applet <applet code="Test.class"
{ width="300" height="300">
String str; <param name="string"
public void init() value="Applet">
{ </applet>
</body>
str=getParameter("string"); </html>
if (str ==null) */
str= "Java";
str="Hello"+str;
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
Passing a Parameter to Applet
import java.awt.Graphics;
import java.applet.Applet; public void paint(Graphics g)
{
public class Rectangle extends Applet g.drawRect(x,y,w,h);
{ }
int x,y,w,h; }
public void init() /*
{ <html>
<body>
x=Integer.parseInt(getParameter("xValue" <applet code="Rectangle.class"
)); width="300" height="300">
<param name="xValue" value="20">
y=Integer.parseInt(getParameter("yValue" <param name="yValue" value="40">
)); <param name="wValue"
value="100">
w=Integer.parseInt(getParameter("wValue <param name="hValue"
")); value="120">
</applet>
h=Integer.parseInt(getParameter("hValue" </body>
)); </html>
Passing a Parameter to Applet
import java.awt.*; String s1,s2,s;
import java.applet.*; g.drawString("Input a }
number in each box",10,50); /*
public class UserIn extends <html>
Applet try <body>
{ { <applet code
TextField text1, text2; s1=text1.getText(); ="UserIn.class" width="300"
public void init() x= Integer.parseInt(s1); height="300">
{ s2=text2.getText(); </applet.
text1=new TextField(8); y=Integer.parseInt(s2); </body>
text2=new TextField(8); } <html>
add(text1); catch(Exception e) */
add(text2); {}
text1.setText("0"); z=x+y;
text2.setText("0"); s= String.valueOf(z);
} g.drawString("The sum
public void paint(Graphics g) is:",10,75);
{ g.drawString(s,100,75);
int x=0,y=0,z=0; }
Passing a Parameter to Applet
getCodeBase() and getDocumentBase()

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

 Called as Swing Applet


 Second type of program that commonly uses swing is
applet
 Swing based applets are similar to AWT based applet but
with an important difference
 Swing applet extends the JApplet rather than Applet
 JApplet includes all of the functionality founds in Applet
and adds support for Swing.
 Swing applets use the same four life cycle methods as
init(), start(), stop(), destroy()

You might also like