Introduction To Applets
Unit-V
Presented by:
Ms Nitu L. Pariyal
1
Introduction
• Small java programs.
• Used in internet computing.
• They can be transported over the internet from one computer to
another and run using appletviewer or any web browser.
• It can
1. Perform arithmetic operations.
2. Display graphics
3. Play sound
4. Accept user input
5. create animation and
6. Play interactive games.
2
Local and Remote Applet
• We can embed applets into web pages in two ways:
1. We can write our own applet and embed them into web pages.
2. We can download an applet from a remote computer system and then
embed it into a web page.
• Applet developed locally and stored in local system is called as local
applet. So internet is not required.
• Remote applet is developed by someone and stored on other system and
we can download the remote applet to our computer via internet and run
it on our computer.
• In order to run remote applet we need the address of applet on the web.
This URL must be mentioned in applet’s HTML document.
3
How Applets Differ from Application
• There are significant differences between applet and application
programs.
• Are not full-featured application programs.
• Usually written to do small task.
• Designed for use on internet, they impose certain limitations and
restrictions in their design.
1. Applets do not use main() method for initiating the execution of code.
Applets, when loaded, automatically call certain methods of Applet class
to start and execute the appplet code.
2. Unlike stand-alone applications, applets cannot be run independently.
They are run from inside a web page using a special feature known as
HTML tag.
3. Applets can not read from or write to the files in local computer.
4
How Applets Differ from Application
4. Applets can not communicate with other servers on the
network.
5. Applets can not run any program from the local computer.
6. Are restricted from using libraries from other languages such as
C or C++
• Theses restrictions & limitations are placed in the interest of
security of systems, which ensures applet can not do any
damage to the local system.
5
• First, let us consider the situations when we might need to use
applets
1) When we need something dynamic to be included in the display
of a web page.
2) When we require some flash outputs.
3) When we want to create a program & make it available on the
internet for us by others on their computers.
Preparing to Write Applet
6
• Applet code uses two calsses
1) Applet &
2) Graphics
• Steps involved in developing and testing in applet are:
1) Building an applet code (.java file)
2) Creating an executable applet (.class file).
3) Designing a web page using HTML tags.
4) Preparing <APPLET> tag
5) Incorporating <APPLET> tag into the web page.
6) Creating HTML file
7) Testing the applet code.
Building Applet code
7
The genealogy of Applet
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
8
Lifecycle of Java Applet
1. Applet is initialized
2. Applet is started
3. Applet is painted
4. Applet is stopped
5. Applet is destroyed
 The java.applet.Applet class 4 lifecycle methods
and java.awt.Component class provides 1 Life cycle
methods for an applet
9
Lifecycle of Java Applet
For creating any applet java.applet.Applet class must be
inherited. It provides life cycle methods of applet
1. public void init(): is used to initialized the Applet. It is
invoked only once.
2. public void start(): is invoked after the init() method or
browser is maximized. It is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is
invoked when applet is stop or browser is minimized
4. public void destroy(): is used to destroy the Applet. It is
invoked only once.
5. public void paint(Graphics g): is used to paint the
applet. It provides Graphics class object that can be
used for drawing oval, rcatngle, arc etc.
11
12
import java.awt.*;
import java.applet.*;
public class WelcomeApplet extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("Welcome to Java Programming!", 25, 25 );
}
}
extends allows us to inherit the
capabilities of class Applet.
Method paint is guaranteed to
be called in all applets. Its first
line must be defined as above.
 2003 Prentice Hall, Inc. All rights reserved. Modified by Evan Korth
13
/*
<applet code="WelcomeApplet.class" width="300" height="300">
</applet>
*/
import java.awt.*;
import java.applet.*;
public class WelcomeApplet extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("Welcome to Java Programming!", 25, 25 );
}
}
14
15
import java.awt.*;
import java.applet.*;
public class WelcomeApplet extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("Welcome to Java Programming!", 25, 25 );
}
}
<html>
<body>
<applet code="WelcomeApplet.class width="300" height="300">
</applet>
</body>
</html>
WelcomeApplet.java
welcomeApplet.html
16
Simple Java Applet: Drawing a String
– Our class inherits method paint from Applet
• By default, paint has empty body
• Override (redefine) paint in our class
– Methods init, start and paint.
• Guaranteed to be called automatically
• Our applet gets "free" version of these by inheriting from
Applet
– Free versions have empty body (do nothing)
– Every applet does not need all three methods
» Override the ones you need
– Applet container “draws itself” by calling method paint
public void paint( Graphics g )
Modified by Evan Korth
17
Simple Java Applet: Drawing a String
– Body of paint
• Method drawString (of class Graphics)
• Called using Graphics object g and dot (.)
• Method name, then parenthesis with arguments
– First argument: String to draw
– Second: x coordinate (in pixels) location
– Third: y coordinate (in pixels) location
g.drawString( "Welcome to Java Programming!", 25, 25 );
18
Simple Java Applet: Drawing a String
• Running the applet
– Compile
• javac WelcomeApplet.java
• If no errors, bytecodes stored in WelcomeApplet.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
Modified by Evan Korth
19
Simple Java Applet: Drawing a String
<html>
<applet code = "WelcomeApplet.class" width = "300" height = "45">
</applet>
</html>
public void init( )
• Applet enters initialization state when it is first loaded.
• This is the first method to execute.
• It is an ideal place to initialize variables.
• It is the best place to define the GUI Components
(buttons, text fields, scrollbars, etc.), lay them out, and
add listeners to them.
• It the place to load images or fonts as well as set up
colors.
• Almost every applet you ever write will have an init( )
method.
20
Running State [public void start ( )]
• Not always needed.
• Called after init( ).
• Called each time the page is loaded and restarted.
• Used mostly in conjunction with stop( )
• start() and stop( ) are used when the Applet is doing time-
consuming calculations that you don’t want to continue
when the page is not in front.
• We may override the start() method to create a thread to
control the applet.
21
Idle or Stopped State [public void stop( )]
• Not always needed.
• Called when the browser leaves the page.
• Called just before destroy( ).
• Use stop( ) if the applet is doing heavy computation that you
don’t want to continue when the browser is on some other
page.
• Used mostly in conjunction with start().
22
Dead State public void destroy( )
• 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.
• Seldom (rarely) needed.
• Called after stop( ).
• Use to explicitly release system resources (like threads).
• System resources are usually released automatically.
23
Methods are called in this order
• init and destroy are only
called once each
• start and stop are called
whenever the browser enters
and leaves the page
• do some work is code called
by your listeners
• paint is called when the
applet needs to be repainted
init()
start()
stop()
destroy()
do some work
24
25
More about HTML tag
26
More about HTML tag
Write a java applet program for addition of two numbers
import java.awt.*;
import java.applet.*;
public class numval extends Applet
{
public void paint(Graphics g)
{
int val1=10, val2=20;
int sum=val1+val2;
String str="sum:" +String.valueOf(sum);
g.drawString(str,100,150);
}
}
Displaying Numerical Values
27
/*
<html>
<applet code=numval.class width=200 height=200>
</applet>
<html>*/
28
Sample Graphics methods
• A Graphics is something you can paint on
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, height);
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height);
g.setColor(Color.red);
g.drawString(“Hello”, 20, 20); Hello
29
import java.awt.*;
import java.applet.*;
public class Hello extends Applet {
String msg;
public void init() {
setBackground(Color.red);
setForeground(Color.blue);
}
public void paint(Graphics g) {
msg="Hello";
g.setFont(new Font("Times New Roman", Font.BOLD, 24));
g.drawString(msg,20,20);
}
}
<html>
<applet code=Hello.class width=450 height=500>
</applet>
</html>`
30
31

Introduction To Applets methods and simple examples

  • 1.
  • 2.
    Introduction • Small javaprograms. • Used in internet computing. • They can be transported over the internet from one computer to another and run using appletviewer or any web browser. • It can 1. Perform arithmetic operations. 2. Display graphics 3. Play sound 4. Accept user input 5. create animation and 6. Play interactive games. 2
  • 3.
    Local and RemoteApplet • We can embed applets into web pages in two ways: 1. We can write our own applet and embed them into web pages. 2. We can download an applet from a remote computer system and then embed it into a web page. • Applet developed locally and stored in local system is called as local applet. So internet is not required. • Remote applet is developed by someone and stored on other system and we can download the remote applet to our computer via internet and run it on our computer. • In order to run remote applet we need the address of applet on the web. This URL must be mentioned in applet’s HTML document. 3
  • 4.
    How Applets Differfrom Application • There are significant differences between applet and application programs. • Are not full-featured application programs. • Usually written to do small task. • Designed for use on internet, they impose certain limitations and restrictions in their design. 1. Applets do not use main() method for initiating the execution of code. Applets, when loaded, automatically call certain methods of Applet class to start and execute the appplet code. 2. Unlike stand-alone applications, applets cannot be run independently. They are run from inside a web page using a special feature known as HTML tag. 3. Applets can not read from or write to the files in local computer. 4
  • 5.
    How Applets Differfrom Application 4. Applets can not communicate with other servers on the network. 5. Applets can not run any program from the local computer. 6. Are restricted from using libraries from other languages such as C or C++ • Theses restrictions & limitations are placed in the interest of security of systems, which ensures applet can not do any damage to the local system. 5
  • 6.
    • First, letus consider the situations when we might need to use applets 1) When we need something dynamic to be included in the display of a web page. 2) When we require some flash outputs. 3) When we want to create a program & make it available on the internet for us by others on their computers. Preparing to Write Applet 6
  • 7.
    • Applet codeuses two calsses 1) Applet & 2) Graphics • Steps involved in developing and testing in applet are: 1) Building an applet code (.java file) 2) Creating an executable applet (.class file). 3) Designing a web page using HTML tags. 4) Preparing <APPLET> tag 5) Incorporating <APPLET> tag into the web page. 6) Creating HTML file 7) Testing the applet code. Building Applet code 7
  • 8.
    The genealogy ofApplet java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet 8
  • 9.
    Lifecycle of JavaApplet 1. Applet is initialized 2. Applet is started 3. Applet is painted 4. Applet is stopped 5. Applet is destroyed  The java.applet.Applet class 4 lifecycle methods and java.awt.Component class provides 1 Life cycle methods for an applet 9
  • 11.
    Lifecycle of JavaApplet For creating any applet java.applet.Applet class must be inherited. It provides life cycle methods of applet 1. public void init(): is used to initialized the Applet. It is invoked only once. 2. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. 3. public void stop(): is used to stop the Applet. It is invoked when applet is stop or browser is minimized 4. public void destroy(): is used to destroy the Applet. It is invoked only once. 5. public void paint(Graphics g): is used to paint the applet. It provides Graphics class object that can be used for drawing oval, rcatngle, arc etc. 11
  • 12.
    12 import java.awt.*; import java.applet.*; publicclass WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); } } extends allows us to inherit the capabilities of class Applet. Method paint is guaranteed to be called in all applets. Its first line must be defined as above.  2003 Prentice Hall, Inc. All rights reserved. Modified by Evan Korth
  • 13.
    13 /* <applet code="WelcomeApplet.class" width="300"height="300"> </applet> */ import java.awt.*; import java.applet.*; public class WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); } }
  • 14.
  • 15.
    15 import java.awt.*; import java.applet.*; publicclass WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); } } <html> <body> <applet code="WelcomeApplet.class width="300" height="300"> </applet> </body> </html> WelcomeApplet.java welcomeApplet.html
  • 16.
    16 Simple Java Applet:Drawing a String – Our class inherits method paint from Applet • By default, paint has empty body • Override (redefine) paint in our class – Methods init, start and paint. • Guaranteed to be called automatically • Our applet gets "free" version of these by inheriting from Applet – Free versions have empty body (do nothing) – Every applet does not need all three methods » Override the ones you need – Applet container “draws itself” by calling method paint public void paint( Graphics g ) Modified by Evan Korth
  • 17.
    17 Simple Java Applet:Drawing a String – Body of paint • Method drawString (of class Graphics) • Called using Graphics object g and dot (.) • Method name, then parenthesis with arguments – First argument: String to draw – Second: x coordinate (in pixels) location – Third: y coordinate (in pixels) location g.drawString( "Welcome to Java Programming!", 25, 25 );
  • 18.
    18 Simple Java Applet:Drawing a String • Running the applet – Compile • javac WelcomeApplet.java • If no errors, bytecodes stored in WelcomeApplet.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 Modified by Evan Korth
  • 19.
    19 Simple Java Applet:Drawing a String <html> <applet code = "WelcomeApplet.class" width = "300" height = "45"> </applet> </html>
  • 20.
    public void init() • Applet enters initialization state when it is first loaded. • This is the first method to execute. • It is an ideal place to initialize variables. • It is the best place to define the GUI Components (buttons, text fields, scrollbars, etc.), lay them out, and add listeners to them. • It the place to load images or fonts as well as set up colors. • Almost every applet you ever write will have an init( ) method. 20
  • 21.
    Running State [publicvoid start ( )] • Not always needed. • Called after init( ). • Called each time the page is loaded and restarted. • Used mostly in conjunction with stop( ) • start() and stop( ) are used when the Applet is doing time- consuming calculations that you don’t want to continue when the page is not in front. • We may override the start() method to create a thread to control the applet. 21
  • 22.
    Idle or StoppedState [public void stop( )] • Not always needed. • Called when the browser leaves the page. • Called just before destroy( ). • Use stop( ) if the applet is doing heavy computation that you don’t want to continue when the browser is on some other page. • Used mostly in conjunction with start(). 22
  • 23.
    Dead State publicvoid destroy( ) • 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. • Seldom (rarely) needed. • Called after stop( ). • Use to explicitly release system resources (like threads). • System resources are usually released automatically. 23
  • 24.
    Methods are calledin this order • init and destroy are only called once each • start and stop are called whenever the browser enters and leaves the page • do some work is code called by your listeners • paint is called when the applet needs to be repainted init() start() stop() destroy() do some work 24
  • 25.
  • 26.
  • 27.
    Write a javaapplet program for addition of two numbers import java.awt.*; import java.applet.*; public class numval extends Applet { public void paint(Graphics g) { int val1=10, val2=20; int sum=val1+val2; String str="sum:" +String.valueOf(sum); g.drawString(str,100,150); } } Displaying Numerical Values 27
  • 28.
    /* <html> <applet code=numval.class width=200height=200> </applet> <html>*/ 28
  • 29.
    Sample Graphics methods •A Graphics is something you can paint on g.drawRect(x, y, width, height); g.fillRect(x, y, width, height); g.drawOval(x, y, width, height); g.fillOval(x, y, width, height); g.setColor(Color.red); g.drawString(“Hello”, 20, 20); Hello 29
  • 30.
    import java.awt.*; import java.applet.*; publicclass Hello extends Applet { String msg; public void init() { setBackground(Color.red); setForeground(Color.blue); } public void paint(Graphics g) { msg="Hello"; g.setFont(new Font("Times New Roman", Font.BOLD, 24)); g.drawString(msg,20,20); } } <html> <applet code=Hello.class width=450 height=500> </applet> </html>` 30
  • 31.