Java applet
• An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because
it has the entire Java API at its disposal.
• There are some important differences between an applet and a standalone Java application, including the following
−
• An applet is a Java class that extends the java.applet.Applet class.
• A main() method is not invoked on an applet, and an applet class will not define main().
• Applets are designed to be embedded within an HTML page.
• When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's
machine.
• A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime
environment.
• The JVM on the user's machine creates an instance of the applet class and invokes various methods during the
applet's lifetime.
• Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to
as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.
• Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
Life Cycle of an Applet
• Four methods in the Applet class gives you the framework on which you build any serious
applet −
• init − This method is intended for whatever initialization is needed for your applet. It is
called after the param tags inside the applet tag have been processed.
• start − This method is automatically called after the browser calls the init method. It is
also called whenever the user returns to the page containing the applet after having gone
off to other pages.
• stop − This method is automatically called when the user moves off the page on which
the applet sits. It can, therefore, be called repeatedly in the same applet.
• destroy − This method is only called when the browser shuts down normally. Because
applets are meant to live on an HTML page, you should not normally leave resources
behind after a user leaves the page that contains the applet.
• paint − Invoked immediately after the start() method, and also any time the applet needs
to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
Import java.awt.*;
Import java.applet.*;
<applet code=“app” width=300 height=400> </applet>
Public class app extends Applet{
Public void init();{ }
}
Public void start(){ }
Public void stop() { }
Public void destroy() { }
Public void paint(Graphics g){ }
}
Preparing to write applets
• Building an applet code (.java file)
• Creating an executable applet(.class file)
• Design a web page using HTML.
• Preparing <Applet>
• Incorporating <Applet> tag in the web page.
• Creating HTML file.
• Testing the applet file.
Import java.applet.*;
Import java.awt.*;
Public class Main extends Applet{
Public void paint(Graphics g){
g.drawstring(“welcome in java applet”,40,20);} }
• Now compile the above code and call the generated .class file in your
html code
<html>
<head></head>
<body><div>
<applet code=“Main.class” width=“800” height=“500”>
</applet></div></body></html>
• OUTPUT
Welcome in java applet.
Building applet code
• To build applet code two class of java library are essential 1. applet 2. Graphics.
• The applet class contained in java.applet package provide life and behavior to the
applet through its methods such as, init(), start(), paint().
• When applet is loaded java automatically calls a series of applet class methods for
starting, running and stopping the applet code.
• To display the result Paint() method is called.
• The output may be text, graphics, sound.
• Public void paint(Graphics g).
• This requires applet code to import java.awt package that contain graphics class.
• All output operation of an applet are performed using the method defined in the
graphics class.
Import java.applet.*;
Import java.awt.*;
Public class HelloApplet extends Applet{
Public void paint(Graphics g){
g.drawstring(“welcome in java applet”,100,100);} }
• Now compile the above code and call the generated .class file in your
html code
<html>
<head><title>Hello applet</title></head>
<body><div>
<applet code=“HelloApplet.class” width=“800” height=“500”>
</applet></div></body></html>
• OUTPUT
Welcome in java applet.
Creating an executable applet
• Write applet code
• Import java.applet.*;
• Import java.awt.*;
• Public class Myapplet extends Applet{
• Public void paint(Graphics g)
• G.drawstring(“java programming”, 40,40);
• }
Compile applet code and generate byte code
Javac Myapplet.java
• Create an html page
• Use any of the html editor and create html page.
• Include applet code in html page in body section using <applet> tag.
• Save the file in same directory as .html file.
<html>
<head><title>some title</title></head>
<body>
<applet code=“Myapplet.class” width=300 height=400>
</applet>
</body>
</html>
• Execute an applet using appletviewer
• Appletviewer applet.html
• Designing a web page
In this we have to include comment near the top of the applet source code file that
contains applet tag. Here, the applet souce code and applet tag are in the same
java file.
Import java.applet.*;
Import java.awt.*;
/*<applet code=Myapplet.class width=400 height=500>
</applet> */
Public class Myapplet extends Applet{
Public void paint(Graphics g)
{
g.drawstring(“simple page”,40.40);
}
}
Applet tag
• HTML <applet> tag was used to embed the Java applet in an HTML
document. This element has been deprecated in HTML 4.0 and
instead of it we can use <object> and newly added element <embed>.
• Syntax:
• <applet code="URL" height="200" width="100">.............</applet>
Attribute name Value Description
code URL It specifies the URL of Java applet class file.
width pixels It specifies the display width of the applet panel.
height pixels It specifies the display height of applet panel
align •Left right top
middle
•bottom
It specifies the position of applet application relative to surrounding content.
alt text It is used to display alternative text in case browser does not support Java.
archive URL This specifies the archived or compressed version of an applet application.
object name It specifies the URL or reference to a serialized representation of an applet.
codebase URL It specifies the exact or relative URL of applets .class file specified in the code
attribute.
hspace pixels It specifies the horizontal space around the applet.
vspace pixels It specifies the vertical space around the applet.
name name It specifies the name for the applet
Adding applet to html page
• <applet code=appletsubclass.class width=anInt height=anInt>
• </applet>
• <applet code=appletsubclass.class CODEBSE=aURL width= anInt
height=anInt>
• </applet>
Passing parameter to applet
• Import java.awt.*;
• Import java.applet.*;
• Public class Fapplet extends Applet{
• String fname;
• Int fsize;
• Public void init(){
• Fname=getParameter(“font”);
• Fsize=Integer.ParseInt(getParameter(“size”));}
• Public void paint(Graphics g){
• Font f=new Font(Fname,Font.italic,Fsize);
• G.setFont(f);
• G.drawstring(“Applet programming”,40, 60);}}
• <html>
• <head><title>some title</title></head>
• <body>
• <applet code=“Fapplet.class” width=400 height=300>
• <param name=font value=“calibri”>
• <param name=size value=“12”>
• </applet>
• </body>
• </html>
Aligning the display
• <align>
• Left
• Right
• Top
• Middle
• texttop

JAVA - Introduction to Applet PowerPoint presentation

  • 1.
    Java applet • Anapplet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. • There are some important differences between an applet and a standalone Java application, including the following − • An applet is a Java class that extends the java.applet.Applet class. • A main() method is not invoked on an applet, and an applet class will not define main(). • Applets are designed to be embedded within an HTML page. • When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. • A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment. • The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime. • Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed. • Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
  • 2.
    Life Cycle ofan Applet • Four methods in the Applet class gives you the framework on which you build any serious applet − • init − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed. • start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages. • stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet. • destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet. • paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
  • 3.
    Import java.awt.*; Import java.applet.*; <appletcode=“app” width=300 height=400> </applet> Public class app extends Applet{ Public void init();{ } } Public void start(){ } Public void stop() { } Public void destroy() { } Public void paint(Graphics g){ } }
  • 4.
    Preparing to writeapplets • Building an applet code (.java file) • Creating an executable applet(.class file) • Design a web page using HTML. • Preparing <Applet> • Incorporating <Applet> tag in the web page. • Creating HTML file. • Testing the applet file.
  • 5.
    Import java.applet.*; Import java.awt.*; Publicclass Main extends Applet{ Public void paint(Graphics g){ g.drawstring(“welcome in java applet”,40,20);} } • Now compile the above code and call the generated .class file in your html code <html> <head></head> <body><div> <applet code=“Main.class” width=“800” height=“500”> </applet></div></body></html> • OUTPUT Welcome in java applet.
  • 6.
    Building applet code •To build applet code two class of java library are essential 1. applet 2. Graphics. • The applet class contained in java.applet package provide life and behavior to the applet through its methods such as, init(), start(), paint(). • When applet is loaded java automatically calls a series of applet class methods for starting, running and stopping the applet code. • To display the result Paint() method is called. • The output may be text, graphics, sound. • Public void paint(Graphics g). • This requires applet code to import java.awt package that contain graphics class. • All output operation of an applet are performed using the method defined in the graphics class.
  • 7.
    Import java.applet.*; Import java.awt.*; Publicclass HelloApplet extends Applet{ Public void paint(Graphics g){ g.drawstring(“welcome in java applet”,100,100);} } • Now compile the above code and call the generated .class file in your html code <html> <head><title>Hello applet</title></head> <body><div> <applet code=“HelloApplet.class” width=“800” height=“500”> </applet></div></body></html> • OUTPUT Welcome in java applet.
  • 8.
    Creating an executableapplet • Write applet code • Import java.applet.*; • Import java.awt.*; • Public class Myapplet extends Applet{ • Public void paint(Graphics g) • G.drawstring(“java programming”, 40,40); • } Compile applet code and generate byte code Javac Myapplet.java
  • 9.
    • Create anhtml page • Use any of the html editor and create html page. • Include applet code in html page in body section using <applet> tag. • Save the file in same directory as .html file. <html> <head><title>some title</title></head> <body> <applet code=“Myapplet.class” width=300 height=400> </applet> </body> </html> • Execute an applet using appletviewer • Appletviewer applet.html
  • 10.
    • Designing aweb page In this we have to include comment near the top of the applet source code file that contains applet tag. Here, the applet souce code and applet tag are in the same java file. Import java.applet.*; Import java.awt.*; /*<applet code=Myapplet.class width=400 height=500> </applet> */ Public class Myapplet extends Applet{ Public void paint(Graphics g) { g.drawstring(“simple page”,40.40); } }
  • 11.
    Applet tag • HTML<applet> tag was used to embed the Java applet in an HTML document. This element has been deprecated in HTML 4.0 and instead of it we can use <object> and newly added element <embed>. • Syntax: • <applet code="URL" height="200" width="100">.............</applet>
  • 12.
    Attribute name ValueDescription code URL It specifies the URL of Java applet class file. width pixels It specifies the display width of the applet panel. height pixels It specifies the display height of applet panel align •Left right top middle •bottom It specifies the position of applet application relative to surrounding content. alt text It is used to display alternative text in case browser does not support Java. archive URL This specifies the archived or compressed version of an applet application. object name It specifies the URL or reference to a serialized representation of an applet. codebase URL It specifies the exact or relative URL of applets .class file specified in the code attribute. hspace pixels It specifies the horizontal space around the applet. vspace pixels It specifies the vertical space around the applet. name name It specifies the name for the applet
  • 13.
    Adding applet tohtml page • <applet code=appletsubclass.class width=anInt height=anInt> • </applet> • <applet code=appletsubclass.class CODEBSE=aURL width= anInt height=anInt> • </applet>
  • 14.
    Passing parameter toapplet • Import java.awt.*; • Import java.applet.*; • Public class Fapplet extends Applet{ • String fname; • Int fsize; • Public void init(){ • Fname=getParameter(“font”); • Fsize=Integer.ParseInt(getParameter(“size”));} • Public void paint(Graphics g){ • Font f=new Font(Fname,Font.italic,Fsize); • G.setFont(f); • G.drawstring(“Applet programming”,40, 60);}}
  • 15.
    • <html> • <head><title>sometitle</title></head> • <body> • <applet code=“Fapplet.class” width=400 height=300> • <param name=font value=“calibri”> • <param name=size value=“12”> • </applet> • </body> • </html>
  • 16.
    Aligning the display •<align> • Left • Right • Top • Middle • texttop