APPLET PROGRAMMING Shiv Ahire
WHAT IS APPLET?DIFFRENT TYPES OF APPLET.
• Applet is a small intelligent program which can be run under a web page.
• Applet is a Java programs that are primarily used in Internet .
• An applet is like application program which can perform arithmetic operations, display graphics,
play sounds accept user input, create animation and play interactive games.
• Java applets can be transported over the internet from one computer to another and run using the
Applet viewer or any Web browser that supports Java.
• Program that runs in
• appletviewer (test utility for applets)
• Web browser (IE, Communicator)
• An applet developed locally and stored in a local system is know as a
local applet.
when a Web page is trying to find a local applet ,it does not need to use
the internet and therefore the local system does not require internet
connection. It simply searches the directories in the local system and
locates and loads the specified applet.
• A remote applet is that which is developed by someone else
and stored on aremote computer connected to Internet.
• In order to locate and load a remote applet,
we must know applet address on the web.
This address is known as Uniform resource Locater(URL).
Contact no:9323638139 Vag’s Page 1
APPLET PROGRAMMING Shiv Ahire
How applet different from application:
APPET APPLICATION
ITS NOT A FULLFEATURED JAVA PROGRAM. ITS A FULLFEATURED JAVA PROGRAM.
TO RUN JAVA APPLET MAIN() IS NOT REQUIRED. TO RUN JAVA APPLICATION MAIN() IS REQUIRED.
APPLET IS COMPARED TO SMALL PROGRAM. ITS NORMALLY WRITTEN TO ACHIEVED BIG OR
SMALL TASK.
APPLETS ARE NOT ALLOWED TO SHARED LIBRARIS APPLICATION HAS ALLOWED TO SHARED
OF OTHER LANGUAGES. LIBRARIS OF OTHER LANGUAGES C,C++ .
NORMALLY GRAPHICS PROGRAMS ARE WRITTEN NORMALLY NON GRAPHICS PROGRAMS ARE
AS AN APPLET PROGRAM. WRITTEN AS AN APPLICATION PROGRAM.
IT IS RUN BY JAVA APPLICATION CAN BE RUN BY “JAVA
“APPLETVIEWER”,”NETSCAPE”,”HOTJAVA INTERPRETER”.
BROWSER” ETC.
IT CAN NOT ACCESS RESOURCE ,FILES IT CAN ACCESS RESOURCE,FILES
When we need to use applet?
Applet are useful when we need something dynamic to be included in the displayed of web
page (daily sensitivity index of share prices using barchart).
When we required some flash o/p.
for eg: that produces sound,animation or some sppecial effect at the time of displaying pages.
when we want to create a program and make it available on the internet.
EXPLAIN APPLET LIFE CYCLE
• Java applet inherits a set of default behaviour from Applet class. So when an applet is loaded,
it undergoes a series of changes in its state.
The applet states include:
o Born or initialization state
o Running state
o Idle state
o Dead or destroyed state
• INIT method:
• Applet enters the initialization state when it is first loaded.
• This is achieved by calling the init() method of Applet class.
• The applet is born.
• At this stage ,we may do the following:
• create objects needed by applet.
• Set up initial value and colors.
Contact no:9323638139 Vag’s Page 2
APPLET PROGRAMMING Shiv Ahire
• Load image or fonts.
• Running State
• Applet entes the running state when the calls start() method of
Applet class.
• This occurs automatically after the applet initialized.
• Starting can also occur if the applet is in “stopped”state.
• Idle state
• An applet becomes idle when it is stopped from running.
• Stopping occurs automatically when we leave the page
containing the currently running applet.
• We can also do so by calling the stop() method explicitly.
• Dead State
• An applet is said to be dead when it is removed from memory.
• This occurs automatically by invoking the destroy() method
when we quit the browser.
• This stage occurs only once in applet’s life cycle.
• If the applet has created any resources,like threads,we may
override the destroy() method to clean up these resources.
• Display state
• Applet moves to the display stae whenever it has to perform
some output operations on the screen.
• This happens after the applets enters into the running state.
• The paint() method is called to accomplish this task.
Contact no:9323638139 Vag’s Page 3
APPLET PROGRAMMING Shiv Ahire
Explain object hierarchy for an Applet
Java’s co-ordinate system
The origin (0,0) in the upper-left corner, positive x
values are to be right, and positive y values are to the
bottom. The values of co-ordinates x and y are in
pixels.
METHOD DESCRIPTION
clearRect() Erases a rectangular area of the canvas.
copyArea() Copies a rectangular area of the canvas to another
area.
drawArc() Draws a hollow arc.
drawLine() Draws a straight line.
drawOval() Draws a hollow oval.
drawPolygon() Draws a hollow polygon.
drawRect() Draws a hollow rectangle.
drawRoundRect() Draws a hollow rectangle with rounded corners.
drawString() Displays a text string.
fillArc() Draws a filled arc.
fillOval() Draws a filled oval.
drawOval() Draws a hollow oval.
Contact no:9323638139 Vag’s Page 4
APPLET PROGRAMMING Shiv Ahire
drawPolygon() Draws a hollow polygon.
drawRect() Draws a hollow rectangle.
drawRoundRect() Draws a hollow rectangle with rounded corners.
drawString() Displays a text string.
fillArc() Draws a filled arc.
fillOval() Draws a filled oval.
fillPolygon() Draws a filled polygon.
fillRect() Draws a filled rectangle.
fillRoundRect() Draws a filled rectangle with rounded corner.
getColor() Retrieves the current drawing colour.
getFont() Retrieves the currently used font.
getFontMetrics() Retrieves information about the current font.
setFont() sets fonts.
fillPolygon() Draws a filled polygon.
fillRect() Draws a filled rectangle.
fillRoundRect() Draws a filled rectangle with rounded corner.
getColor() Retrieves the current drawing colour.
getFont() Retrieves the currently used font.
getFontMetrics() Retrieves information about the current font.
setFont() sets fonts.
Contact no:9323638139 Vag’s Page 5
APPLET PROGRAMMING Shiv Ahire
WAP to demonstrate an applet.
WaP to Write a message “Welcome to Java Programming”
1
2 // A first applet in Java.
3
4 // Java packages
5 import java.awt.Graphics; // import class Graphics
6 import java.applet.*; // import class Applet
7
8 public class Welcome extends Applet
9 {
10 // draw text on applet’s background
11 public void paint( Graphics g )
12 {
13
14
15 // draw a String at x-coordinate 25 and y-coordinate 25
17 g.drawString( "Welcome to Java Programming!", 25, 25 );
18
19 } // end method paint
20
21 } // end class welcome
– To Compile
• javac Welcome.java
• If no errors, bytecodes stored in Welcome.class
– Create an HTML file
• Loads the applet into appletviewer or a browser
• Ends in .htm or .html
– To execute an applet
• Create an HTML file indicating which applet the browser (or appletviewer)
should load and execute
Contact no:9323638139 Vag’s Page 6
APPLET PROGRAMMING Shiv Ahire
(Welcome.html)
-Simple HTML file
• Usually in same directory as .class file
• Remember, .class file created after compilation
– HTML codes (tags)
• Usually come in pairs
• Begin with < and end with >
– Lines 1 and 4 - begin and end the HTML tags
– Line 2 - begins <applet> tag
• Specifies code to use for applet
• Specifies width and height of display area in pixels
– Line 3 - ends </applet> tag
– Steps involved in developing and testing a applet:
– Building an applet code(.java file)
– Creating an executable applet(.class file)
– Designing Web page using html tags
– Preparing <APPLET>tag
– Incorporating <APPLET> tag into web page
– Creating the html file
– Testing applet code
SYNTAX OF HTML<APPLET> TAG:
<Applet
Codebase=url
code=AppletFilename.class
[Alt=alternate_text]
[Name =applet_instance_name]
width=pixels
height=pixels
[Align=alignment]
[Vspace=pixels]
[Hspace=pixels]
>
Contact no:9323638139 Vag’s Page 7
APPLET PROGRAMMING Shiv Ahire
[<param name=name1 value=value1>]
[<param name=name2 value=value2>]
. .....
[text to be displayed in the absence of java]
</applet>
STATUS WINDOW AND ITS APPLICATION
HOW THE MESSAGE IS DISPLAY IN THE STATUS WINDOW
Below the applet window there is a small rectangle window,this window is called status window.
Syntax:
void showStatus(String s)
TO display message without distributing output(like,mouse co-ordinate,cursor position)
showStatus window is used as one of the helpful debugging aid by displaying various message in
status window.
Status window is a good place to give the user feedback about what is occuring in the applet
window.
import java.awt.*;
import java.applet.*;
public class Status extends Applet
{
public void init()
{
setBackground(Color.BLUE);
}
public void paint(Graphics g)
{
g.setColor(Color.YELLOW);
g.drawString(“ Applet window”,100,100);
showStatus(“ Status window”);
}
}/*<applet code=Status.class height=300,width=300>
</applet>
*/
Contact no:9323638139 Vag’s Page 8
APPLET PROGRAMMING Shiv Ahire
WRITE A PROGRAM TO DISPLAY A NUMERIC VALUES
NOTE: In applet we can display numeric values by first converting them into string and then
using drawString() method of Graphics class.
1. Program to illustrates how an applet handles numeric value.
2. import java.awt.*;
3. import java.applet.*;
4. public class Numvalue extends Applet
5. {
6. public void paint(Graphics g)
7. {
8. int value1=10;
9. int value2=20;
10. int sum = value1+value2;
11. String s="sum:" +String.valueOf(sum);
12. g.drawString(s,100,100);
13. }
14. }
15. /*<applet code =Numvalue.class
16. width=300
17. height=300>
18. </applet>*/
• WRITE A PROGRAM TO DRAW LINES AND RECTANGLES.
• PROGRAM TO DRAW DIFFERENT SHAPES ON APPLET.
1. import java.awt.*;
2. import java.applet.*;
3. public class LineRect extends Applet
4. {
5. public void paint (Graphics g)
6. {
7. g.drawLine(20,20,60,60);
8. g.drawRect(10,60,40,30);
9. g.fillRect(60,10,30,80);
10. g.drawRoundRect(10,100,80,50,10,10);
11. g.fillRoundRect(20,110,60,30,5,5);
12. g.drawLine(100,10,230,140);
13. g.drawLine(100,140,230,10);
14. }
15. }
16. /*<APPLET CODE=LineRect. Class HEIGHT=250 WIDTH=200>
17. </APPLET>*/
Contact no:9323638139 Vag’s Page 9
APPLET PROGRAMMING Shiv Ahire
• Program to draw concentric circle.
1. import java.awt.*;
2. import java.applet.*;
3. public class Circle extends Applet{
4. Color C[]={Color.red,Color.blue,Color.black,Color.green};
5. Font f;
6. public void init()
7. {
8. f=new Font("Arial",Font.BOLD|Font.ITALIC,36);
9. }
10. public void paint(Graphics g)
11. {
12. int xc=300;
13. int yc=300;
14. int r=25;
15. for(int i=0;i<4;i++)
16. {
17. g.setColor(C[i]);
18. g.fillOval(xc-r,yc-r,2*r,2*r);
19. r=r+30;
20. }
21. g.setFont(f);
22. g.drawString("Concentric Circles",150,150);
23. }
24. }
25.
26. /* <applet code=Circle.class Width=600 height=600 >
27. </applet> */
PASSING PARAMETER TO THE APPET:
To pass the parameter param tag is required. Pass more value we required more param tag.
Syntax : <param name=variable name value=variable value>
A great benefit of passing parameter from HTML pages to the applets they call its portability.
The area between the opening and closing applet tag is also includes to pass parameter to
applets.
SYNTAX:To retrieve data in applet code
String getParameter(argument);
Contact no:9323638139 Vag’s Page 10
APPLET PROGRAMMING Shiv Ahire
Wrie an Applet program to display string ”welcome to applet “ .pass string as a parameter to
applet.
Program:
import java.awt.*;
import java.applet.*;
public class ParameterPassing extends Applet
{
String S;
public void init()
{
setBackground(Color.black);
S=getParameter(“ t1 “);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString(“ welcome to applet “,80 ,80);
}
}
/*<applet code=ParameterPassing.class width=300 height=300>
<param name=“ t1 “ value= “welcome to applet “>
</applet>
*/
1. import java.awt.*;
2. import java.applet.*;
3. /*<applet code =InputData width=300 height=300>
4. </applet>*/
5. public class InputData extends Applet
6. { int x,y,z;
7. TextField t1,t2,t3;
8. public void init()
9. {
10. t1 =new TextField(20);
11. t2 =new TextField(20);
12. t3 =new TextField(20);
13. add(t1);
14. add(t2);
Contact no:9323638139 Vag’s Page 11
APPLET PROGRAMMING Shiv Ahire
15. add(t3);
16. t1.setText("0");
17. t2.setText("0");
18. t3.setText("0");
19. }
20. public void paint(Graphics g)
21. {
22. g.drawString("enter the numbers: ",10,50);
23. try
24. {
25. x=Integer.parseInt(t1.getText());
26. y=Integer.parseInt(t2.getText());
27. }
28. catch (NumberFormatException e)
29. {
30. g.drawString(" The user had not Entered no: ",100,200);
31. }
32. z=x+y;
33. t3.setText(“ ”+z);
34. }
35. public boolean action(Event e,Object o)
36. {
37. repaint();
38. return true;
39. }
40. }
Write the applet to draw square inside a circle.
Ans.
import java.applet.*;
import java.awt.*;
public class Welcome extends Applet
{
///Font f;
public void init()
{
///f=new Font("Lucida Handwriting",Font.BOLD,24);
///setFont(f);
///setBackground(new Color(44,82,211));
///setForeground(new Color(208,56,34));
Contact no:9323638139 Vag’s Page 12
APPLET PROGRAMMING Shiv Ahire
}
public void paint (Graphics g)
{
g.setColor(Color.red);
g.drawOval(100,100,71,71);
g.setColor(Color.blue);
g.drawRect(111,111,50,50);
g.drawString(“square inside a circle.”,111,190);
}
}
/*<applet code=Welcome.class width=500 height=500>
</applet>
*/
Html file:
<html>
<body>
<applet code=Hello.class width=500 height=500>
</applet>
</body>
</html>
------------------------------------------------------------------------------------------
/* A simple banner applet.
This applet creates a thread that scrolls
the message contained in msg right to left
across the applet's window.
*/
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleBanner" width=300 height=50>
</applet>
*/
public class SimpleBanner extends Applet implements Runnable {
String msg = " A Simple Moving Banner.";
Thread t = null;
int state;
boolean stopFlag;
// Set colors and initialize thread.
Contact no:9323638139 Vag’s Page 13
APPLET PROGRAMMING Shiv Ahire
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
}
// Start thread
public void start() {
t = new Thread(this);
stopFlag = false;
t.start();
}
// Entry point for the thread that runs the banner.
public void run() {
char ch;
// Display banner
for( ; ; ) {
try {
repaint();
Thread.sleep(250);
ch = msg.charAt(0);
msg = msg.substring(1, msg.length());
msg += ch;
if(stopFlag)
break;
} catch(InterruptedException e) {}
}
}
// Pause the banner.
public void stop() {
stopFlag = true;
t = null;
}
// Display the banner.
public void paint(Graphics g) {
g.drawString(msg, 50, 30);
}
}
Contact no:9323638139 Vag’s Page 14