By
Dr M. Senthilkumar
Assistant Professor
Department of Computer Science
Government Arts and Science College, Avinashi - 641654
Building Applets
2
Writing Applets
 When to use?
 How applets works?
 What sort of features an applet has?
 Where to start when we first create our own applets?
3
Writing Applets
 When we need something dynamic to be included in
the display of a Web Page
 When we need flash outputs
 Place useful program in Internet for others to use it
 Example:
 Share prices of companies in a list
 Bar Chart of Tables
 Playing Sounds, Animations, Special Effects
4
Writing Applets
 Install Java properly
 Ensure appletviewer or Java enabled Browser
 Building an applet code (.java file)
 Creating an executable applet (.class file)
 Designing a Web page using HTML tags
 Preparing <APPLET> tag
 Incorporating <APPLET> tag into the web page
 Creating HTML file
 Testing the applet code
5
Building an applet code (.java file)
 Applet and Graphics class
 Applet class is available within java.applet package
 Applet class provides life and behavior of applet
 Applet class contains init ( ), start ( ) and paint ( ) methods
 Applet methods are called automatically by Java instead
of main( )
6
Building an applet code (.java file)
 paint( ) displays result of the applet code
 paint( ) requires an Graphics object as an argument
 paint( ) outputs text, image, sound, animations using
methods of Graphics Object
 paint( ) is defined as public void paint(Graphics g)
 We must import java.awt package that contains Graphics
7
Building an applet code (.java file)
import java.awt.*;
import java.applet.*;
………….
………….
public class appletclassname extends Applet
{
………...
…………
public void paint(Graphics g)
{
…………
…………
}
………
}
8
Our First Applet
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello Java”, 10, 100);
}
}
100
Hello Java
10
(0,0)
D:jdk1.8.0_111jdk1.8.0_111bin>javac HelloJava.java
D:jdk1.8.0_111jdk1.8.0_111bin>appletviewer.exe HelloJava.html
9
Chain of Classes Inherited by Applet Class
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
10
Life Cycle of an Applet
stop ( )
Begin
(Load Applet)
start ( )
Born
Running Idle
start ( )
Idle
destroy ( )
paint ( )
End
Destroyed
Display
Stopped
11
Life Cycle of an Applet - Initialization State
 Initialization occurs only once in the applet’s life cycle
 Create Objects needed by the applet
 Setup initial values
 Load images or fonts
 Setup colors
public void init( )
{
……………
…………… (Action)
……………
}
12
Life Cycle of an Applet - Running State
 Applet enters running state using start( )
 start( ) can be called from Idle state
(Switching between web pages)
 start( ) Can be called more than once
 start( ) can be overridden to create a thread to
control applet
 Occurs automatically after initialization of applet
public void start( )
{
……………
…………… (Action)
……………
}
13
Life Cycle of an Applet – Idle / Stopped State
 Applet becomes idle when it is
stopped using stop( )
 Occurs automatically when leaving
a web page
(Switching between web pages)
 If we use a thread to control applet,
then we must use stop( ) to
terminate the thread by Overriding
the stop( )
public void start( )
{
……………
…………… (Action)
……………
}
14
Life Cycle of an Applet – Dead State
 Applet becomes dead when it is
removed from memory using destroy( )
 Occurs automatically when exiting from
browser
 Occurs only once in Life cycle
 Resources like threads are released by
Overriding destroy( )
public void destroy( )
{
……………
…………… (Action)
……………
}
15
Life Cycle of an Applet – Display State
 Occurs automatically after Running state using paint( )
 We must Override paint( ) to display Contents
public void paint(Graphics g)
{
……………
…………… (Display statements)
……………
}
16
Creating an Executable Applet
 Compile the applet and generate byte code
 javac HelloJava.java
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello Java”, 10, 100);
}
}
17
Designing a Web Page – HelloJava.html
<HTML>
<! Title Bar>
<HEAD>
<TITLE> Welcome to Java Applets </TITLE>
</HEAD>
<BODY>
<CENTER>
<H1> Welcome to the world of Applets </H1>
</CENTER>
<BR>
<CENTER>
<APPLET
CODE = HelloJava.class
WIDTH = 400
HEIGHT = 200>
</APPLET>
</CENTER>
</BODY>
</HTML>
18
References
 Programming with Java – A Primer - E. Balagurusamy, 3rd
Edition, TMH
19
Thank You

171-33B - Java Programming-Building Applets.ppt

  • 1.
    By Dr M. Senthilkumar AssistantProfessor Department of Computer Science Government Arts and Science College, Avinashi - 641654 Building Applets
  • 2.
    2 Writing Applets  Whento use?  How applets works?  What sort of features an applet has?  Where to start when we first create our own applets?
  • 3.
    3 Writing Applets  Whenwe need something dynamic to be included in the display of a Web Page  When we need flash outputs  Place useful program in Internet for others to use it  Example:  Share prices of companies in a list  Bar Chart of Tables  Playing Sounds, Animations, Special Effects
  • 4.
    4 Writing Applets  InstallJava properly  Ensure appletviewer or Java enabled Browser  Building an applet code (.java file)  Creating an executable applet (.class file)  Designing a Web page using HTML tags  Preparing <APPLET> tag  Incorporating <APPLET> tag into the web page  Creating HTML file  Testing the applet code
  • 5.
    5 Building an appletcode (.java file)  Applet and Graphics class  Applet class is available within java.applet package  Applet class provides life and behavior of applet  Applet class contains init ( ), start ( ) and paint ( ) methods  Applet methods are called automatically by Java instead of main( )
  • 6.
    6 Building an appletcode (.java file)  paint( ) displays result of the applet code  paint( ) requires an Graphics object as an argument  paint( ) outputs text, image, sound, animations using methods of Graphics Object  paint( ) is defined as public void paint(Graphics g)  We must import java.awt package that contains Graphics
  • 7.
    7 Building an appletcode (.java file) import java.awt.*; import java.applet.*; …………. …………. public class appletclassname extends Applet { ………... ………… public void paint(Graphics g) { ………… ………… } ……… }
  • 8.
    8 Our First Applet importjava.awt.*; import java.applet.*; public class HelloJava extends Applet { public void paint(Graphics g) { g.drawString(“Hello Java”, 10, 100); } } 100 Hello Java 10 (0,0) D:jdk1.8.0_111jdk1.8.0_111bin>javac HelloJava.java D:jdk1.8.0_111jdk1.8.0_111bin>appletviewer.exe HelloJava.html
  • 9.
    9 Chain of ClassesInherited by Applet Class java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet
  • 10.
    10 Life Cycle ofan Applet stop ( ) Begin (Load Applet) start ( ) Born Running Idle start ( ) Idle destroy ( ) paint ( ) End Destroyed Display Stopped
  • 11.
    11 Life Cycle ofan Applet - Initialization State  Initialization occurs only once in the applet’s life cycle  Create Objects needed by the applet  Setup initial values  Load images or fonts  Setup colors public void init( ) { …………… …………… (Action) …………… }
  • 12.
    12 Life Cycle ofan Applet - Running State  Applet enters running state using start( )  start( ) can be called from Idle state (Switching between web pages)  start( ) Can be called more than once  start( ) can be overridden to create a thread to control applet  Occurs automatically after initialization of applet public void start( ) { …………… …………… (Action) …………… }
  • 13.
    13 Life Cycle ofan Applet – Idle / Stopped State  Applet becomes idle when it is stopped using stop( )  Occurs automatically when leaving a web page (Switching between web pages)  If we use a thread to control applet, then we must use stop( ) to terminate the thread by Overriding the stop( ) public void start( ) { …………… …………… (Action) …………… }
  • 14.
    14 Life Cycle ofan Applet – Dead State  Applet becomes dead when it is removed from memory using destroy( )  Occurs automatically when exiting from browser  Occurs only once in Life cycle  Resources like threads are released by Overriding destroy( ) public void destroy( ) { …………… …………… (Action) …………… }
  • 15.
    15 Life Cycle ofan Applet – Display State  Occurs automatically after Running state using paint( )  We must Override paint( ) to display Contents public void paint(Graphics g) { …………… …………… (Display statements) …………… }
  • 16.
    16 Creating an ExecutableApplet  Compile the applet and generate byte code  javac HelloJava.java import java.awt.*; import java.applet.*; public class HelloJava extends Applet { public void paint(Graphics g) { g.drawString(“Hello Java”, 10, 100); } }
  • 17.
    17 Designing a WebPage – HelloJava.html <HTML> <! Title Bar> <HEAD> <TITLE> Welcome to Java Applets </TITLE> </HEAD> <BODY> <CENTER> <H1> Welcome to the world of Applets </H1> </CENTER> <BR> <CENTER> <APPLET CODE = HelloJava.class WIDTH = 400 HEIGHT = 200> </APPLET> </CENTER> </BODY> </HTML>
  • 18.
    18 References  Programming withJava – A Primer - E. Balagurusamy, 3rd Edition, TMH
  • 19.