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.

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.