SlideShare a Scribd company logo
Prepared by, B.Sc. Ill(VISern)
Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II)
UMT-II: Applet Programming
Introductionto Applet:
• A java program that contains mainO function is called as 'Standalone
iava application'.
• A standalonejava application is saved ina file with '.java' file extension.
• A JDK tool, iava compiler(javac) is used to compile '.java' file.
• A java compiler creates byte code i.e. file with '.class' file extension.
• The by code is executed by JVM usingJDK tool Java Interpreter!iava).
• This is about standalonejava application program.
• Java provides another feature called Applet'.
• An applet is a special java program which also saved in fdc with '.java'
file extension but it does not contain niainfj function.
• Also, applet is not executable using java interpreter. It executes or runs
within a web page.
• We can compile the applet code by using java compiler (javac) that create
executable applet (i.e. .class file) but we cannot execute this code using
iava interpreter(iava).
• Hence to run an applet we require web browser i.e. java compatible web
browser.
• Ifjava compatible web browser isnot available, thenwe can execute or run
a java applet using a JDK tool called 'appletviewer' present in 'bin' of
JDK as 'javac' and 'java' commands are present.
• In short, applet is a java program that does not contains main() function
and which executes or runs within a web page.
• To create and handle the java applet's, a separate package named
iava.applet' is provide by the java developers.
• This package contains a class whose name is Applet' in which methods
are defined to handle execution of the applet.
• Don't get confused between applet package and applet class. Package has
name with all letters in small case whereas class name begins with capital
letter 'A'
NOTE: Package name is: java.applet
Class name is : java.applet.Applet
Prepared by,
Mr.Hushare
B.Sc. Ill(VISem)
Asst. Professor Advance Java Programming (Unit-II)
Standalone java application Vs. Applet
# Standalone java application Java applet application
1 It contains main() function. It does not contains main() function.
2 JDK tool java interpreter can run
byte code of standalone java
application.
JDK tool java interpreter cannot
run byte code of java applet
application.
3 It does not require we browser to It requires web browser to execute.
execute.
4 To execute this application byte code
we have to run.
To execute applet application html
code we have run.
5 It execution is begins with main()
method.
Its execution begins with init()
method.
6 As compare to applet it is large java
program.
Java applet is small program.
Prepared by,
Mr.Hushare Y.V., Asst. Professor
Running a iava applet:
B.Sc. Ill(VISem)
Advance Java Programming (Unit-II)
Art applet is small java program without main() function which run's
within a web page.
That is, when we run/test a web page in web browser that web page
execute/run the specifiedjava applet.
Following are the steps for running a iava annlet:
Step 1: Build applet code (i.e. ".java" file)
Step 2: Create executable applet code (i.e. ".class" file)
Step 3: Create a web page (i.e. ".html" file) and add executable applet
inweb page using <APPLET> tag of HTML.
Step 4: Test/execute HTML file usingjava compatible web browser or
using JDK tool "appletviewer".
Step 1: Build Applet code:
importjava.awt.*;
importjava.applet.*;
public class Myapplet extends Applet
f
X
public void paint (Graphics g)
{
.............// Applet code
Fig. Skeleton of applet program (Architecture of applet)
• This Applet skeleton uses two essential classes i.e. Applet and Graphics.
• Applet class is defined in iava.applet package and Graphics class is
defined in java.awt package.
• The paintO method, when it is called actually it displays the result of the
applet code on the screen. It is method of Applet class.
• This applet code we should save with '.java' extension i.e. Myapplet.java.
Step 2: Creating executable applet code:
• Just compile above created Myapplet.java file usingjava compiler (javac).
• It will create executable applet code i.e. Mvapplet.class.
by.
.Hushare Y.V., Asst. Professor
Step 3: Create web page
B.Sc. Ill(VIScm)
Advance Java Programming (Unit-II)
and add executable applet inHTML file:
<HTML>
<BODY>
<APPLET CODE = Myapplet.class
HEIGHT= intcgcrvaluc
WIDTH - integervalue >
</APPLET>
</BODY>
</HTML>
• Save this file say webl.html. Here CODE attribute of <APPLET> tag is
used to embed Myapplet.class applet in web page web 1.html.
Step 4: Test/execute web page:
• When above created web page is tested injava compatible we browser,
Myapplet.class this applet run within this web page as small window
whose size is specified with HEIGHT and WIDTH in <APPLET> tag.
• If java compatible web browser is not available, then use JDK tool
"appletviewer" as follows to runweb page.
C:V> appletviewer web.html
For example, write an applet to show "Hello India" message.
importjava.applet.*;
importjava.awt.*;
public class hello
{
public void paint(Graphics g)
{
g.drawString("Hello India",10,10);
}
J_
hello,java
C:>......>javac hello.java
C:>......>appletviewer hello.html
<html>
<body>
<applet code=hello.class
width=300
height=200>
</applet>
</body>
</html>
hello.html
by,
.Hushare Y.V., Asst. Professor
Output:
B.Sc. Ill(VISem)
Advance Java Programming (Unit-II)
j> Applet Viewer: hello-classÿ
Applet started.
by,
.Hushare Y.V., Asst. Professor
Life Cycle of an applet:
B.Sc. Ill(VISem)
Advance Java Programming (Unit-II)
Running
paintQ
destroy()
Fig.Life Cycle of Applet
It is important to understand the order in which the various methods are
get called to control the applet.
The following sequence of methods are invoked, when applet begins.:
o init()
o start()
o paintQ
When an applet is terminated, the following sequence of methods
invoked sequentially:
o stopQ
o destroyQ
NOTE: All of these methods are defined in 'Applet' class which is defined in
'java.applet' package.
Prepared by, B.Sc. Ill(VISem)
Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II)
1. Born Slate :
• The init() methodis the first methodthat gets called when we runan applet
and put the applet in borne state.
• Inthis state of applet, its variables are get initialized.
2. Running State:
• The initQ methodcalls another methodstartQ automatically put the applet
in running state.
• Again startQ method invokes paintQ method and its output will be
displayed on screen.
• The startQ and paintQ methods are called in sequence every time when
applet receives focus.
3. Idle State:
• When web browser going to display another page, then stopQ method is
get called to suspend the execution of applet.
• At this time, the applet is said to be in Idle state.
• Suspended applet may resume again by calling startQ method when it
receives focus.
4. Dead State:
• When applet is not needed further, then it will be moved from the memory
and pushed into dead state.
• This is done by invoking destroyQ method.
• This is the last methodthat invoked.
by,
.Hushare Y.V., Asst. Professor
Passing Parameters to Applet:
B.Sc. Ill(VISem)
Advance Java Programming(Unit-II)
• Wc can pass parameter to applet by using <PARAM> tag.
• Two essential attributes of <PARAM> tag are :
o NAME
o VALUE
• The <PARAM> tag should be used within <APPLET> tag as follows:
<APPLET ... >
<PARAM name= text
value= stringval >
</APPLET>
• To catch the Parameters are passed to an applet gctParametcrQ method
we should define in initQ method.
• getParameterQ methodcan accept only ONE string argument i.e. name
of the parameter and returns a string i.e. value of that parameter.
• For example,
/*
<APPLET code-paramTest.class height=500 width=400>
<PARAM name-x
value-'SSSC, Amravati">
</APPLET>
*/
importjava.awt.*;
importjava.applet.*;
public class paramTest extends Applet
{
String str;
public void init()
{
str=getParameter("x");
}
public void paint(Graphics g)
{
g.drawString(str,100,100);
}
}
Prepared by, B.Sc. Ill(VISern)
Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II)
getCodeBaseO and getDocumentBaseO methods:
• In applet programming wc create THREE files i.e. ".java", ".class" and
".html".
• Wc also know that there is no role of ".java" file after compilation.
• It is also important that, two files i.e. '.class' and '.html' file must be in
same folder.
• Executable applet means '.class' file.
• Wc know that runs within a html document. Hcncc full path of html file is
referred as document base of the applet i.e. from which document it is
executed.
• Whereas the path where ".html" and ".class" files arc stored is referred as
code base of applet i.e. at which place executable applet is stored.
• That iscodebase anddocument base ofapplet isnothingbut thepathwhere
it is.
• We can retrieve both of these paths by making use of two methods:
ÿ
getDocumcBascO
ÿ
getCodeBase()
For example,
• Consider the files "test.class" and "test.html" both arc stored in folder
"D:testing".
• The following program output illustrate the use of getCodeBaseO an
getDocumentBaseO methods.
Mr.Hushare Y.V., Asst. Professor
B.Sc. Ill(VIScm)
Advance Java Programming (Unit-II)
/*
<APPLET code~tcst.class height=500 width=400>
</APPLET>
*/
importjava.awt.*;
importjava.applet.*;
public class test extends Applet
{
public void paint(Graphics g)
{
g.drawString("Document base:" + getDocumentBaseO,20,30);
g.drawString("Code base:" +getCodeBaseO,20,50);
}
Output:
mAm
4i| Applet Viewer: test.class
Applet
DocumentBase=fiIe:/D:n"estingl2.html
CodeBase=file:/D:JTestingi
Applet started.
Prepared by, B.Sc. Ill(VIScm)
Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II)
ApplicationContext:
• 'AppletContext' is anjava interface defined in 'applet' package.
• An applet runs inside a browser or applet viewer.
• An applet runningwithin a browser can ask the browser to do things for it:
o Fetch an audio clip
o Show a short message in the status line
o Show a different web page
• The browser can performthese requests or ignore them.
• To communicate with the browser, an applet calls the
java.applet.Applet.getApplctContextO method, which returns an object
that implements an interface of type java.applet.AppletContext.
• Think of AppletContext as a communication path between the applet and
the browser.
• AppletContext interface provides the following methods:
# MethodDescription MethodDescription
1 void showStatus(String message)
Shows the message inthe status line
of the browser
2 Enumeration getApplets!)
Returns an enumeration of all the
applets in the same context (same web
page)
3 Applet getApplet(Stringname)
Returns the applet in the current
context with the specified name (null
ifnone exists)
4 void showDocument(URL url)
Shows a new web page in the browser,
displacing the current page.
5
void showDocument(URL url,
String target)
Shows a new web page in the browser,
specifying the target frame
(" self", " parent", " top",
" blank", or <frame-name>)
Prepared by, B.Sc. Ill(VIScm)
Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II)
6 Image getlmage(URL url)
Returns an image object that
encapsulates the image specified by
the URL
7 AudioClip getAudioClip(URL url)
Returns an AudioClip object that
encapsulates the sound file specified
by the URL.
• The following program shows the use of Application context.
• Inthis programwe are usingapplicationcontext objet to openanother .html
file(Test.html) from the applet ACDemo.class.
o D:ACDemo.java
o D:ACDemo.class
o D:Test.html
/*<applel code— 'ACDemo" width=300 height=50> </applet>*/
importjava.awt.*;
importjava.applet.*;
importjava.net.*;
public class ACDemo extends Applet
i

public void start()
{
AppletContext ac = getAppletContext();
url = getCodeBase(); // get url of this applet(here ,url="file://D:/")
try
{
ac.showDociimentfnew URL(url+"Test.html"j);
}
catch(Exception e)
{
showStatus("URL not found");
}
}
}
13
Prepared by,
Mr.Hiishare Y.V., Asst. Professor
B.Sc. Ill(VISem)
Advance Java Programming (Unit-II)
Graphics Class:
• Java provides another feature called graphics programming.
• Graphics means art of drawing graphical shapes ( e.g. a line,a rectangle, a
ellipse, etc.)
• Java allows to draw different graphical shapes.
• The feature of graphics programming is provided in java through a class
'Graphics' defined in 'awt' package, (iava.awt.Graphics)
• The 'Graphics' class contains various methods for drawing various
shapes.
• The following are some methods of the 'Graphics' class:
Method Description
drawLine() Draws a straight line
drawString() Displays the string.
drawRect() Draws a hollow (empty) Rectangle.
drawRoundRect() Draws a hollow (empty) Rectangle with rounded corners
fillRect() Draws a filled Rectangle.
drawOval() Draws a hollow (empty) oval.
fdlOval() Draws a filled oval.
drawPolygonO Draws a hollow (empty) Polygon.
fillPlolygon() Draws a filled Polygon.
drawArc() Draws a hollow)empty) arc.
setFont() Sets font.
setColor() Sets the drawing color.
:parcd by, B.Sc. Ill(VISem)
Mr.Hushare Y.V., Asst. Professor Advance Java Programming(Unit-II)
Draw Lines:
• The drawLineO method is used to draw straight line.
• This method is defined in Graphics class of awt package.
• Syntax:
Graphics g;
g.drawLine(x1,yl,x2,y2);
• For example.
/*
<applet code="lineTest.class"
height=700
width=800>
</applet>
#/
importjava.awt.*;
importjava.applet.*;
public class lineTest extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.orange);
g.drawLine(100,100,500,100);
g.setColor(Color.green);
g.drawLine) 100,100,100,500);
15
Prepared by,
Mr.Hushare Y.V., Asst. Professor
B.Sc. Ill(VISern)
Advance Java Programming (Unit-II)
Output:
IIIApplet Viewer: lineTest.class | ÿ |B !•—£3—1
Applet
Applet started.
Drawing Rectangle:
• The drawRectO and fillRectO methods are used to draw
hollow(empty) and filled rectangle respectively.
• Ittakes coordinates left-topcomer (x,y),width andheight as arguments.
(X, y) Width
Height
Syntax:
Graphics g;
g.drawRect(x, y, width, Height);
g.fillRect(x, y, width, Height);
Prepared by,
Mr.Hushare
B.Sc. Ill(VIScm)
Asst. Professor Advance Java Programming (Unit-II)
/*
<applet code="rectTest.class"
width=500
height=400>
</applct>
*/
importjava.awt.*;
importjava.applet.*;
public class rectTest extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawRcct(l0,10,100,70);
g.setColor(Color.green);
g.fillRect(150,10,100,70);
}
}
Output:
=.l Applet Viewer rectTest.class
Applet
Applet started.
17
Prepared by,
Mr.Hiishare Y.V., Asst. Professor
B.Sc. Ill(VISem)
Advance Java Programming (Unit-II)
Drawing Ellipses:
• The drawOval() and fillOval() methods are used to draw hollow(empty)
and filled ellipse/circle respectively.
• It draws a oval within specified rectangle.
• Hence it takes coordinates left-top comer (x,y), width and height of
rectangle as arguments.
• The oval whose width and height is equal is referred as circle.
(x,y)
• Syntax:
Graphics g;
g.drawOval(x, y, width, Height);
g.fillOval(x, y, width, Height);
For example.
by,
.Hushare Y.V.,
B.Sc. Ill(VIScm)
Asst. Professor Advance Java Programming(Unit-II)
/*<applet code="ovalTest.class"
width=500
height=400>
</applet>*/
importjava.awt.*;
importjava.applet.*;
public class ovalTest extends Applet
i
t
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawOval(l0,10,150,70); // Ellipse
g.fillOval(200,l0,150,70); // Ellipse
g.setColor(Color.magenta);
g.fill0val(10,100,150,150);
}
I
/
Output:
Applet Viewer: ovalTest.class
Applet
Applet started.

More Related Content

What's hot

Java basic introduction
Java basic introductionJava basic introduction
Java basic introduction
Ideal Eyes Business College
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
Ajay Sharma
 
JAVA INTRODUCTION
JAVA INTRODUCTIONJAVA INTRODUCTION
JAVA INTRODUCTION
Prof Ansari
 
Core Java
Core JavaCore Java
Chapter 1 introduction to java technology
Chapter 1 introduction to java technologyChapter 1 introduction to java technology
Chapter 1 introduction to java technology
sshhzap
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
sotlsoc
 
Basic java part_ii
Basic java part_iiBasic java part_ii
Basic java part_ii
Khaled AlGhazaly
 
I pad uicatalog_lesson02
I pad uicatalog_lesson02I pad uicatalog_lesson02
I pad uicatalog_lesson02
Rich Helton
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
Java Lover
 
Java notes
Java notesJava notes
Java notes
Debasish Biswas
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
jayc8586
 
Programming in Java
Programming in JavaProgramming in Java
Programming in Java
Abhilash Nair
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
Sandeep Rawat
 
Java introduction
Java introductionJava introduction
Java introduction
The icfai university jaipur
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed Answers
Whizlabs
 
Java features
Java featuresJava features
Java features
Prashant Gajendra
 
Core java online training
Core java online trainingCore java online training
Core java online training
Glory IT Technologies Pvt. Ltd.
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
poonguzhali1826
 
Core java course syllabus
Core java course syllabusCore java course syllabus
Core java course syllabus
Papitha Velumani
 

What's hot (19)

Java basic introduction
Java basic introductionJava basic introduction
Java basic introduction
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
JAVA INTRODUCTION
JAVA INTRODUCTIONJAVA INTRODUCTION
JAVA INTRODUCTION
 
Core Java
Core JavaCore Java
Core Java
 
Chapter 1 introduction to java technology
Chapter 1 introduction to java technologyChapter 1 introduction to java technology
Chapter 1 introduction to java technology
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
 
Basic java part_ii
Basic java part_iiBasic java part_ii
Basic java part_ii
 
I pad uicatalog_lesson02
I pad uicatalog_lesson02I pad uicatalog_lesson02
I pad uicatalog_lesson02
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
 
Java notes
Java notesJava notes
Java notes
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Programming in Java
Programming in JavaProgramming in Java
Programming in Java
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
 
Java introduction
Java introductionJava introduction
Java introduction
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed Answers
 
Java features
Java featuresJava features
Java features
 
Core java online training
Core java online trainingCore java online training
Core java online training
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
Core java course syllabus
Core java course syllabusCore java course syllabus
Core java course syllabus
 

Similar to B.Sc. III(VI Sem) Advance Java Unit2: Appet

How to run java program without IDE
How to run java program without IDEHow to run java program without IDE
How to run java program without IDE
Shweta Oza
 
Core java introduction
Core java introduction Core java introduction
Core java introduction
Som Prakash Rai
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
mrinalbhutani
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
Danairat Thanabodithammachari
 
How to write a simple java program in 10 steps
How to write a simple java program in 10 stepsHow to write a simple java program in 10 steps
How to write a simple java program in 10 steps
Ishara Amarasekera
 
JAVA Program Examples
JAVA Program ExamplesJAVA Program Examples
JAVA Program Examples
Prof Chethan Raj C
 
java basic for begginers
java basic for begginersjava basic for begginers
java basic for begginers
divaskrgupta007
 
Unit of competency
Unit of competencyUnit of competency
Unit of competency
loidasacueza
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
sanchi Sharma
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
Nexus
 
Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)
Daroko blog(www.professionalbloggertricks.com)
 
JAVA ALL 5 MODULE NOTES.pptx
JAVA ALL 5 MODULE NOTES.pptxJAVA ALL 5 MODULE NOTES.pptx
JAVA ALL 5 MODULE NOTES.pptx
DrPreethiD1
 
Java lab-manual
Java lab-manualJava lab-manual
Java lab-manual
Khurshid Asghar
 
APPLET.pptx
APPLET.pptxAPPLET.pptx
APPLET.pptx
SHANMUGARAJA K
 
Java lab1 manual
Java lab1 manualJava lab1 manual
Java lab1 manual
nahalomar
 
Java Intro: Unit1. Hello World
Java Intro: Unit1. Hello WorldJava Intro: Unit1. Hello World
Java Intro: Unit1. Hello World
Yakov Fain
 
Applet1 (1).pptx
Applet1 (1).pptxApplet1 (1).pptx
Applet1 (1).pptx
FahanaAbdulVahab
 
Java introduction
Java introductionJava introduction
Java introduction
logeswarisaravanan
 
Applet.pptx
Applet.pptxApplet.pptx
Applet.pptx
LakachewYezihalem
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
Gomathi Gomu
 

Similar to B.Sc. III(VI Sem) Advance Java Unit2: Appet (20)

How to run java program without IDE
How to run java program without IDEHow to run java program without IDE
How to run java program without IDE
 
Core java introduction
Core java introduction Core java introduction
Core java introduction
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
How to write a simple java program in 10 steps
How to write a simple java program in 10 stepsHow to write a simple java program in 10 steps
How to write a simple java program in 10 steps
 
JAVA Program Examples
JAVA Program ExamplesJAVA Program Examples
JAVA Program Examples
 
java basic for begginers
java basic for begginersjava basic for begginers
java basic for begginers
 
Unit of competency
Unit of competencyUnit of competency
Unit of competency
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)
 
JAVA ALL 5 MODULE NOTES.pptx
JAVA ALL 5 MODULE NOTES.pptxJAVA ALL 5 MODULE NOTES.pptx
JAVA ALL 5 MODULE NOTES.pptx
 
Java lab-manual
Java lab-manualJava lab-manual
Java lab-manual
 
APPLET.pptx
APPLET.pptxAPPLET.pptx
APPLET.pptx
 
Java lab1 manual
Java lab1 manualJava lab1 manual
Java lab1 manual
 
Java Intro: Unit1. Hello World
Java Intro: Unit1. Hello WorldJava Intro: Unit1. Hello World
Java Intro: Unit1. Hello World
 
Applet1 (1).pptx
Applet1 (1).pptxApplet1 (1).pptx
Applet1 (1).pptx
 
Java introduction
Java introductionJava introduction
Java introduction
 
Applet.pptx
Applet.pptxApplet.pptx
Applet.pptx
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 

More from Assistant Professor, Shri Shivaji Science College, Amravati

Unit-IV_Introduction to Java.pdf
Unit-IV_Introduction to Java.pdfUnit-IV_Introduction to Java.pdf
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event HandlingB.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
Assistant Professor, Shri Shivaji Science College, Amravati
 
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & MultithreadingB.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
Assistant Professor, Shri Shivaji Science College, Amravati
 
B.Sc. III(VI Sem) Question Bank Advance Java(Unit:1,2, and 5)
B.Sc. III(VI Sem) Question Bank Advance Java(Unit:1,2, and 5)B.Sc. III(VI Sem) Question Bank Advance Java(Unit:1,2, and 5)
B.Sc. III(VI Sem) Question Bank Advance Java(Unit:1,2, and 5)
Assistant Professor, Shri Shivaji Science College, Amravati
 
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-5 PL/SQL, Cursor and Trigger
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-5 PL/SQL, Cursor and TriggerB.Sc. II (IV Sem) RDBMS & PL/SQL Unit-5 PL/SQL, Cursor and Trigger
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-5 PL/SQL, Cursor and Trigger
Assistant Professor, Shri Shivaji Science College, Amravati
 
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-2 Relational Model
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-2 Relational ModelB.Sc. II (IV Sem) RDBMS & PL/SQL Unit-2 Relational Model
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-2 Relational Model
Assistant Professor, Shri Shivaji Science College, Amravati
 
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-1 Fundamentals of DBMS
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-1 Fundamentals of DBMSB.Sc. II (IV Sem) RDBMS & PL/SQL Unit-1 Fundamentals of DBMS
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-1 Fundamentals of DBMS
Assistant Professor, Shri Shivaji Science College, Amravati
 

More from Assistant Professor, Shri Shivaji Science College, Amravati (7)

Unit-IV_Introduction to Java.pdf
Unit-IV_Introduction to Java.pdfUnit-IV_Introduction to Java.pdf
Unit-IV_Introduction to Java.pdf
 
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event HandlingB.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
 
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & MultithreadingB.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
 
B.Sc. III(VI Sem) Question Bank Advance Java(Unit:1,2, and 5)
B.Sc. III(VI Sem) Question Bank Advance Java(Unit:1,2, and 5)B.Sc. III(VI Sem) Question Bank Advance Java(Unit:1,2, and 5)
B.Sc. III(VI Sem) Question Bank Advance Java(Unit:1,2, and 5)
 
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-5 PL/SQL, Cursor and Trigger
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-5 PL/SQL, Cursor and TriggerB.Sc. II (IV Sem) RDBMS & PL/SQL Unit-5 PL/SQL, Cursor and Trigger
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-5 PL/SQL, Cursor and Trigger
 
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-2 Relational Model
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-2 Relational ModelB.Sc. II (IV Sem) RDBMS & PL/SQL Unit-2 Relational Model
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-2 Relational Model
 
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-1 Fundamentals of DBMS
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-1 Fundamentals of DBMSB.Sc. II (IV Sem) RDBMS & PL/SQL Unit-1 Fundamentals of DBMS
B.Sc. II (IV Sem) RDBMS & PL/SQL Unit-1 Fundamentals of DBMS
 

Recently uploaded

The debris of the ‘last major merger’ is dynamically young
The debris of the ‘last major merger’ is dynamically youngThe debris of the ‘last major merger’ is dynamically young
The debris of the ‘last major merger’ is dynamically young
Sérgio Sacani
 
GBSN - Biochemistry (Unit 6) Chemistry of Proteins
GBSN - Biochemistry (Unit 6) Chemistry of ProteinsGBSN - Biochemistry (Unit 6) Chemistry of Proteins
GBSN - Biochemistry (Unit 6) Chemistry of Proteins
Areesha Ahmad
 
Direct Seeded Rice - Climate Smart Agriculture
Direct Seeded Rice - Climate Smart AgricultureDirect Seeded Rice - Climate Smart Agriculture
Direct Seeded Rice - Climate Smart Agriculture
International Food Policy Research Institute- South Asia Office
 
HOW DO ORGANISMS REPRODUCE?reproduction part 1
HOW DO ORGANISMS REPRODUCE?reproduction part 1HOW DO ORGANISMS REPRODUCE?reproduction part 1
HOW DO ORGANISMS REPRODUCE?reproduction part 1
Shashank Shekhar Pandey
 
Immersive Learning That Works: Research Grounding and Paths Forward
Immersive Learning That Works: Research Grounding and Paths ForwardImmersive Learning That Works: Research Grounding and Paths Forward
Immersive Learning That Works: Research Grounding and Paths Forward
Leonel Morgado
 
The cost of acquiring information by natural selection
The cost of acquiring information by natural selectionThe cost of acquiring information by natural selection
The cost of acquiring information by natural selection
Carl Bergstrom
 
The binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defectsThe binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defects
Sérgio Sacani
 
ESA/ACT Science Coffee: Diego Blas - Gravitational wave detection with orbita...
ESA/ACT Science Coffee: Diego Blas - Gravitational wave detection with orbita...ESA/ACT Science Coffee: Diego Blas - Gravitational wave detection with orbita...
ESA/ACT Science Coffee: Diego Blas - Gravitational wave detection with orbita...
Advanced-Concepts-Team
 
Applied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdfApplied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdf
University of Hertfordshire
 
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdfwaterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
LengamoLAppostilic
 
Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.
Aditi Bajpai
 
Sciences of Europe journal No 142 (2024)
Sciences of Europe journal No 142 (2024)Sciences of Europe journal No 142 (2024)
Sciences of Europe journal No 142 (2024)
Sciences of Europe
 
Authoring a personal GPT for your research and practice: How we created the Q...
Authoring a personal GPT for your research and practice: How we created the Q...Authoring a personal GPT for your research and practice: How we created the Q...
Authoring a personal GPT for your research and practice: How we created the Q...
Leonel Morgado
 
23PH301 - Optics - Optical Lenses.pptx
23PH301 - Optics  -  Optical Lenses.pptx23PH301 - Optics  -  Optical Lenses.pptx
23PH301 - Optics - Optical Lenses.pptx
RDhivya6
 
Pests of Storage_Identification_Dr.UPR.pdf
Pests of Storage_Identification_Dr.UPR.pdfPests of Storage_Identification_Dr.UPR.pdf
Pests of Storage_Identification_Dr.UPR.pdf
PirithiRaju
 
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Leonel Morgado
 
AJAY KUMAR NIET GreNo Guava Project File.pdf
AJAY KUMAR NIET GreNo Guava Project File.pdfAJAY KUMAR NIET GreNo Guava Project File.pdf
AJAY KUMAR NIET GreNo Guava Project File.pdf
AJAY KUMAR
 
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
Scintica Instrumentation
 
ESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptxESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptx
PRIYANKA PATEL
 
Sexuality - Issues, Attitude and Behaviour - Applied Social Psychology - Psyc...
Sexuality - Issues, Attitude and Behaviour - Applied Social Psychology - Psyc...Sexuality - Issues, Attitude and Behaviour - Applied Social Psychology - Psyc...
Sexuality - Issues, Attitude and Behaviour - Applied Social Psychology - Psyc...
PsychoTech Services
 

Recently uploaded (20)

The debris of the ‘last major merger’ is dynamically young
The debris of the ‘last major merger’ is dynamically youngThe debris of the ‘last major merger’ is dynamically young
The debris of the ‘last major merger’ is dynamically young
 
GBSN - Biochemistry (Unit 6) Chemistry of Proteins
GBSN - Biochemistry (Unit 6) Chemistry of ProteinsGBSN - Biochemistry (Unit 6) Chemistry of Proteins
GBSN - Biochemistry (Unit 6) Chemistry of Proteins
 
Direct Seeded Rice - Climate Smart Agriculture
Direct Seeded Rice - Climate Smart AgricultureDirect Seeded Rice - Climate Smart Agriculture
Direct Seeded Rice - Climate Smart Agriculture
 
HOW DO ORGANISMS REPRODUCE?reproduction part 1
HOW DO ORGANISMS REPRODUCE?reproduction part 1HOW DO ORGANISMS REPRODUCE?reproduction part 1
HOW DO ORGANISMS REPRODUCE?reproduction part 1
 
Immersive Learning That Works: Research Grounding and Paths Forward
Immersive Learning That Works: Research Grounding and Paths ForwardImmersive Learning That Works: Research Grounding and Paths Forward
Immersive Learning That Works: Research Grounding and Paths Forward
 
The cost of acquiring information by natural selection
The cost of acquiring information by natural selectionThe cost of acquiring information by natural selection
The cost of acquiring information by natural selection
 
The binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defectsThe binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defects
 
ESA/ACT Science Coffee: Diego Blas - Gravitational wave detection with orbita...
ESA/ACT Science Coffee: Diego Blas - Gravitational wave detection with orbita...ESA/ACT Science Coffee: Diego Blas - Gravitational wave detection with orbita...
ESA/ACT Science Coffee: Diego Blas - Gravitational wave detection with orbita...
 
Applied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdfApplied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdf
 
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdfwaterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
 
Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.
 
Sciences of Europe journal No 142 (2024)
Sciences of Europe journal No 142 (2024)Sciences of Europe journal No 142 (2024)
Sciences of Europe journal No 142 (2024)
 
Authoring a personal GPT for your research and practice: How we created the Q...
Authoring a personal GPT for your research and practice: How we created the Q...Authoring a personal GPT for your research and practice: How we created the Q...
Authoring a personal GPT for your research and practice: How we created the Q...
 
23PH301 - Optics - Optical Lenses.pptx
23PH301 - Optics  -  Optical Lenses.pptx23PH301 - Optics  -  Optical Lenses.pptx
23PH301 - Optics - Optical Lenses.pptx
 
Pests of Storage_Identification_Dr.UPR.pdf
Pests of Storage_Identification_Dr.UPR.pdfPests of Storage_Identification_Dr.UPR.pdf
Pests of Storage_Identification_Dr.UPR.pdf
 
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
 
AJAY KUMAR NIET GreNo Guava Project File.pdf
AJAY KUMAR NIET GreNo Guava Project File.pdfAJAY KUMAR NIET GreNo Guava Project File.pdf
AJAY KUMAR NIET GreNo Guava Project File.pdf
 
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
 
ESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptxESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptx
 
Sexuality - Issues, Attitude and Behaviour - Applied Social Psychology - Psyc...
Sexuality - Issues, Attitude and Behaviour - Applied Social Psychology - Psyc...Sexuality - Issues, Attitude and Behaviour - Applied Social Psychology - Psyc...
Sexuality - Issues, Attitude and Behaviour - Applied Social Psychology - Psyc...
 

B.Sc. III(VI Sem) Advance Java Unit2: Appet

  • 1. Prepared by, B.Sc. Ill(VISern) Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II) UMT-II: Applet Programming Introductionto Applet: • A java program that contains mainO function is called as 'Standalone iava application'. • A standalonejava application is saved ina file with '.java' file extension. • A JDK tool, iava compiler(javac) is used to compile '.java' file. • A java compiler creates byte code i.e. file with '.class' file extension. • The by code is executed by JVM usingJDK tool Java Interpreter!iava). • This is about standalonejava application program. • Java provides another feature called Applet'. • An applet is a special java program which also saved in fdc with '.java' file extension but it does not contain niainfj function. • Also, applet is not executable using java interpreter. It executes or runs within a web page. • We can compile the applet code by using java compiler (javac) that create executable applet (i.e. .class file) but we cannot execute this code using iava interpreter(iava). • Hence to run an applet we require web browser i.e. java compatible web browser. • Ifjava compatible web browser isnot available, thenwe can execute or run a java applet using a JDK tool called 'appletviewer' present in 'bin' of JDK as 'javac' and 'java' commands are present. • In short, applet is a java program that does not contains main() function and which executes or runs within a web page. • To create and handle the java applet's, a separate package named iava.applet' is provide by the java developers. • This package contains a class whose name is Applet' in which methods are defined to handle execution of the applet. • Don't get confused between applet package and applet class. Package has name with all letters in small case whereas class name begins with capital letter 'A' NOTE: Package name is: java.applet Class name is : java.applet.Applet Prepared by, Mr.Hushare B.Sc. Ill(VISem) Asst. Professor Advance Java Programming (Unit-II) Standalone java application Vs. Applet # Standalone java application Java applet application 1 It contains main() function. It does not contains main() function. 2 JDK tool java interpreter can run byte code of standalone java application. JDK tool java interpreter cannot run byte code of java applet application. 3 It does not require we browser to It requires web browser to execute. execute. 4 To execute this application byte code we have to run. To execute applet application html code we have run. 5 It execution is begins with main() method. Its execution begins with init() method. 6 As compare to applet it is large java program. Java applet is small program.
  • 2. Prepared by, Mr.Hushare Y.V., Asst. Professor Running a iava applet: B.Sc. Ill(VISem) Advance Java Programming (Unit-II) Art applet is small java program without main() function which run's within a web page. That is, when we run/test a web page in web browser that web page execute/run the specifiedjava applet. Following are the steps for running a iava annlet: Step 1: Build applet code (i.e. ".java" file) Step 2: Create executable applet code (i.e. ".class" file) Step 3: Create a web page (i.e. ".html" file) and add executable applet inweb page using <APPLET> tag of HTML. Step 4: Test/execute HTML file usingjava compatible web browser or using JDK tool "appletviewer". Step 1: Build Applet code: importjava.awt.*; importjava.applet.*; public class Myapplet extends Applet f X public void paint (Graphics g) { .............// Applet code Fig. Skeleton of applet program (Architecture of applet) • This Applet skeleton uses two essential classes i.e. Applet and Graphics. • Applet class is defined in iava.applet package and Graphics class is defined in java.awt package. • The paintO method, when it is called actually it displays the result of the applet code on the screen. It is method of Applet class. • This applet code we should save with '.java' extension i.e. Myapplet.java. Step 2: Creating executable applet code: • Just compile above created Myapplet.java file usingjava compiler (javac). • It will create executable applet code i.e. Mvapplet.class. by. .Hushare Y.V., Asst. Professor Step 3: Create web page B.Sc. Ill(VIScm) Advance Java Programming (Unit-II) and add executable applet inHTML file: <HTML> <BODY> <APPLET CODE = Myapplet.class HEIGHT= intcgcrvaluc WIDTH - integervalue > </APPLET> </BODY> </HTML> • Save this file say webl.html. Here CODE attribute of <APPLET> tag is used to embed Myapplet.class applet in web page web 1.html. Step 4: Test/execute web page: • When above created web page is tested injava compatible we browser, Myapplet.class this applet run within this web page as small window whose size is specified with HEIGHT and WIDTH in <APPLET> tag. • If java compatible web browser is not available, then use JDK tool "appletviewer" as follows to runweb page. C:V> appletviewer web.html For example, write an applet to show "Hello India" message. importjava.applet.*; importjava.awt.*; public class hello { public void paint(Graphics g) { g.drawString("Hello India",10,10); } J_ hello,java C:>......>javac hello.java C:>......>appletviewer hello.html <html> <body> <applet code=hello.class width=300 height=200> </applet> </body> </html> hello.html
  • 3. by, .Hushare Y.V., Asst. Professor Output: B.Sc. Ill(VISem) Advance Java Programming (Unit-II) j> Applet Viewer: hello-classÿ Applet started. by, .Hushare Y.V., Asst. Professor Life Cycle of an applet: B.Sc. Ill(VISem) Advance Java Programming (Unit-II) Running paintQ destroy() Fig.Life Cycle of Applet It is important to understand the order in which the various methods are get called to control the applet. The following sequence of methods are invoked, when applet begins.: o init() o start() o paintQ When an applet is terminated, the following sequence of methods invoked sequentially: o stopQ o destroyQ NOTE: All of these methods are defined in 'Applet' class which is defined in 'java.applet' package.
  • 4. Prepared by, B.Sc. Ill(VISem) Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II) 1. Born Slate : • The init() methodis the first methodthat gets called when we runan applet and put the applet in borne state. • Inthis state of applet, its variables are get initialized. 2. Running State: • The initQ methodcalls another methodstartQ automatically put the applet in running state. • Again startQ method invokes paintQ method and its output will be displayed on screen. • The startQ and paintQ methods are called in sequence every time when applet receives focus. 3. Idle State: • When web browser going to display another page, then stopQ method is get called to suspend the execution of applet. • At this time, the applet is said to be in Idle state. • Suspended applet may resume again by calling startQ method when it receives focus. 4. Dead State: • When applet is not needed further, then it will be moved from the memory and pushed into dead state. • This is done by invoking destroyQ method. • This is the last methodthat invoked. by, .Hushare Y.V., Asst. Professor Passing Parameters to Applet: B.Sc. Ill(VISem) Advance Java Programming(Unit-II) • Wc can pass parameter to applet by using <PARAM> tag. • Two essential attributes of <PARAM> tag are : o NAME o VALUE • The <PARAM> tag should be used within <APPLET> tag as follows: <APPLET ... > <PARAM name= text value= stringval > </APPLET> • To catch the Parameters are passed to an applet gctParametcrQ method we should define in initQ method. • getParameterQ methodcan accept only ONE string argument i.e. name of the parameter and returns a string i.e. value of that parameter. • For example, /* <APPLET code-paramTest.class height=500 width=400> <PARAM name-x value-'SSSC, Amravati"> </APPLET> */ importjava.awt.*; importjava.applet.*; public class paramTest extends Applet { String str; public void init() { str=getParameter("x"); } public void paint(Graphics g) { g.drawString(str,100,100); } }
  • 5. Prepared by, B.Sc. Ill(VISern) Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II) getCodeBaseO and getDocumentBaseO methods: • In applet programming wc create THREE files i.e. ".java", ".class" and ".html". • Wc also know that there is no role of ".java" file after compilation. • It is also important that, two files i.e. '.class' and '.html' file must be in same folder. • Executable applet means '.class' file. • Wc know that runs within a html document. Hcncc full path of html file is referred as document base of the applet i.e. from which document it is executed. • Whereas the path where ".html" and ".class" files arc stored is referred as code base of applet i.e. at which place executable applet is stored. • That iscodebase anddocument base ofapplet isnothingbut thepathwhere it is. • We can retrieve both of these paths by making use of two methods: ÿ getDocumcBascO ÿ getCodeBase() For example, • Consider the files "test.class" and "test.html" both arc stored in folder "D:testing". • The following program output illustrate the use of getCodeBaseO an getDocumentBaseO methods. Mr.Hushare Y.V., Asst. Professor B.Sc. Ill(VIScm) Advance Java Programming (Unit-II) /* <APPLET code~tcst.class height=500 width=400> </APPLET> */ importjava.awt.*; importjava.applet.*; public class test extends Applet { public void paint(Graphics g) { g.drawString("Document base:" + getDocumentBaseO,20,30); g.drawString("Code base:" +getCodeBaseO,20,50); } Output: mAm 4i| Applet Viewer: test.class Applet DocumentBase=fiIe:/D:n"estingl2.html CodeBase=file:/D:JTestingi Applet started.
  • 6. Prepared by, B.Sc. Ill(VIScm) Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II) ApplicationContext: • 'AppletContext' is anjava interface defined in 'applet' package. • An applet runs inside a browser or applet viewer. • An applet runningwithin a browser can ask the browser to do things for it: o Fetch an audio clip o Show a short message in the status line o Show a different web page • The browser can performthese requests or ignore them. • To communicate with the browser, an applet calls the java.applet.Applet.getApplctContextO method, which returns an object that implements an interface of type java.applet.AppletContext. • Think of AppletContext as a communication path between the applet and the browser. • AppletContext interface provides the following methods: # MethodDescription MethodDescription 1 void showStatus(String message) Shows the message inthe status line of the browser 2 Enumeration getApplets!) Returns an enumeration of all the applets in the same context (same web page) 3 Applet getApplet(Stringname) Returns the applet in the current context with the specified name (null ifnone exists) 4 void showDocument(URL url) Shows a new web page in the browser, displacing the current page. 5 void showDocument(URL url, String target) Shows a new web page in the browser, specifying the target frame (" self", " parent", " top", " blank", or <frame-name>) Prepared by, B.Sc. Ill(VIScm) Mr.Hushare Y.V., Asst. Professor Advance Java Programming (Unit-II) 6 Image getlmage(URL url) Returns an image object that encapsulates the image specified by the URL 7 AudioClip getAudioClip(URL url) Returns an AudioClip object that encapsulates the sound file specified by the URL. • The following program shows the use of Application context. • Inthis programwe are usingapplicationcontext objet to openanother .html file(Test.html) from the applet ACDemo.class. o D:ACDemo.java o D:ACDemo.class o D:Test.html /*<applel code— 'ACDemo" width=300 height=50> </applet>*/ importjava.awt.*; importjava.applet.*; importjava.net.*; public class ACDemo extends Applet i public void start() { AppletContext ac = getAppletContext(); url = getCodeBase(); // get url of this applet(here ,url="file://D:/") try { ac.showDociimentfnew URL(url+"Test.html"j); } catch(Exception e) { showStatus("URL not found"); } } }
  • 7. 13 Prepared by, Mr.Hiishare Y.V., Asst. Professor B.Sc. Ill(VISem) Advance Java Programming (Unit-II) Graphics Class: • Java provides another feature called graphics programming. • Graphics means art of drawing graphical shapes ( e.g. a line,a rectangle, a ellipse, etc.) • Java allows to draw different graphical shapes. • The feature of graphics programming is provided in java through a class 'Graphics' defined in 'awt' package, (iava.awt.Graphics) • The 'Graphics' class contains various methods for drawing various shapes. • The following are some methods of the 'Graphics' class: Method Description drawLine() Draws a straight line drawString() Displays the string. drawRect() Draws a hollow (empty) Rectangle. drawRoundRect() Draws a hollow (empty) Rectangle with rounded corners fillRect() Draws a filled Rectangle. drawOval() Draws a hollow (empty) oval. fdlOval() Draws a filled oval. drawPolygonO Draws a hollow (empty) Polygon. fillPlolygon() Draws a filled Polygon. drawArc() Draws a hollow)empty) arc. setFont() Sets font. setColor() Sets the drawing color. :parcd by, B.Sc. Ill(VISem) Mr.Hushare Y.V., Asst. Professor Advance Java Programming(Unit-II) Draw Lines: • The drawLineO method is used to draw straight line. • This method is defined in Graphics class of awt package. • Syntax: Graphics g; g.drawLine(x1,yl,x2,y2); • For example. /* <applet code="lineTest.class" height=700 width=800> </applet> #/ importjava.awt.*; importjava.applet.*; public class lineTest extends Applet { public void paint(Graphics g) { g.setColor(Color.orange); g.drawLine(100,100,500,100); g.setColor(Color.green); g.drawLine) 100,100,100,500);
  • 8. 15 Prepared by, Mr.Hushare Y.V., Asst. Professor B.Sc. Ill(VISern) Advance Java Programming (Unit-II) Output: IIIApplet Viewer: lineTest.class | ÿ |B !•—£3—1 Applet Applet started. Drawing Rectangle: • The drawRectO and fillRectO methods are used to draw hollow(empty) and filled rectangle respectively. • Ittakes coordinates left-topcomer (x,y),width andheight as arguments. (X, y) Width Height Syntax: Graphics g; g.drawRect(x, y, width, Height); g.fillRect(x, y, width, Height); Prepared by, Mr.Hushare B.Sc. Ill(VIScm) Asst. Professor Advance Java Programming (Unit-II) /* <applet code="rectTest.class" width=500 height=400> </applct> */ importjava.awt.*; importjava.applet.*; public class rectTest extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawRcct(l0,10,100,70); g.setColor(Color.green); g.fillRect(150,10,100,70); } } Output: =.l Applet Viewer rectTest.class Applet Applet started.
  • 9. 17 Prepared by, Mr.Hiishare Y.V., Asst. Professor B.Sc. Ill(VISem) Advance Java Programming (Unit-II) Drawing Ellipses: • The drawOval() and fillOval() methods are used to draw hollow(empty) and filled ellipse/circle respectively. • It draws a oval within specified rectangle. • Hence it takes coordinates left-top comer (x,y), width and height of rectangle as arguments. • The oval whose width and height is equal is referred as circle. (x,y) • Syntax: Graphics g; g.drawOval(x, y, width, Height); g.fillOval(x, y, width, Height); For example. by, .Hushare Y.V., B.Sc. Ill(VIScm) Asst. Professor Advance Java Programming(Unit-II) /*<applet code="ovalTest.class" width=500 height=400> </applet>*/ importjava.awt.*; importjava.applet.*; public class ovalTest extends Applet i t public void paint(Graphics g) { g.setColor(Color.red); g.drawOval(l0,10,150,70); // Ellipse g.fillOval(200,l0,150,70); // Ellipse g.setColor(Color.magenta); g.fill0val(10,100,150,150); } I / Output: Applet Viewer: ovalTest.class Applet Applet started.