Applets in Java
Applets in Java
Engineering
Course Code: E1UC307C Course Name:Java Programming
1
Faculty Name: Programe Name:
Types of Java
Application
1. Stand alone application(Java application)
2. Web Application( Applet & JSP/Servlet)
Web applications are the applications users can use through a browser. Standalone
applications are known as Desktop/ Offline applications and can be accessed
offline.
Java applications
Run in stand-alone mode
No additional software required (such as a Web browser)
Java applets
Compiled Java class files
Run within a Web browser (or an appletviewer)
Loaded from anywhere on the Internet
2
Applet
An Applet in Java is a small application that runs within a web browser or an
applet viewer. Applets are written in Java and are typically used to provide
interactive features in a web page, such as animations, games, or interactive
forms.
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.
init() method
start() method
paint() method
stop() method
destroy() method
4
In the life-cycle of execution, the
Life Cycle of Applet applet exists (lives) in one of these
4 states.
Applet Life Cycle There are methods that can
changes the state of the applet. All
init() Born of these methods have a name and
Begin
they are called as callback methods.
stop()
start() These methods are named so
because they are called
automatically by the browser when
Idle required for smooth execution of the
Running applet.
destroy() Here, programmers write the
paint() start() above-mentioned methods with
some code but never calls.
End
Dead
5
Life Cycle steps
Four methods in the Applet class give you the framework on which you build any 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
6 java.awt.
init and destroy are only
called once each
init()
start and stop are called
whenever the browser
enters and leaves the page start()
do some work is code called
by your listeners paint
paint is called again when do other work
the applet needs to be
repainted stop()
destroy()
7
Applet Program
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World!", 50, 25);
}
}
Compile this file HelloWorld.java and mention the path in HTML file
Now you have to create an HTML File that Includes the Applet.
Using a text editor, create a file named Hello.html in the same directory that
contains HelloWorld.class. This HTML file should contain the following text:
8
Embedding Applet in Web page
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Java Applet on the Web!</h1>
<APPLET CODE="HelloWorld.class" width=500 height=400></APPLET>
</body>
</HTML>
9
Compile and Run an Applet
To compile: javac HelloWorld.java Generates
HelloWorld.class
To run:
a) Use the appletviewer from JDK
index.html
10
Hello
import java.applet.Applet;
import java.awt.*; World.java
public class GraphicsDemo extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180); }
11
}
Displaying Image in Applet
import java.awt.*;
import java.applet.*;
public class DisplayImage extends Applet {
Image picture;
public void init() {
picture = getImage(getDocumentBase(),“abc.jpg");
}
public void paint(Graphics g) {
g.drawImage(picture, 30,30, this);
}
}
12
Passing Parameters to Applet
<HTML>
<HEAD><TITLE> Hello World Applet</TITLE></HEAD>
<body>
<h1>Hi, This is My First Communicating Applet on the Web!</h1>
<APPLET
CODE="HelloAppletMsg.class" width=500 height=400>
</APPLET>
</body>
</HTML>
13
Applet Program Accepting Parameters
//HelloAppletMsg.java
import java.applet.Applet;
import java.awt.*;
public class HelloAppletMsg extends Applet {
String msg;
public void init()
{
msg = getParameter("Greetings");
if( msg == null)
msg = "Hello";
}
public void paint(Graphics g) {
g.drawString (msg,10, 100);
}
This is name of parameter specified in PARAM tag;
} This method returns the value of parameter.
14
Output
15
Interactive Applet Program
import java.applet.Applet;
import java.awt.*; public void paint(Graphics g) {
public class SumNumsInteractive extends int num1 = 0;
Applet { int num2 = 0;
int sum;
TextField text1, text2; String s1, s2, s3;
public void init() g.drawString("Input a number in each
{ box ", 10, 50);
text1 = new TextField(10); try {
s1 = text1.getText();
text2 = new TextField(10); num1 = Integer.parseInt(s1);
text1.setText("0"); s2 = text2.getText();
text2.setText("0"); num2 = Integer.parseInt(s2);
add(text1); }
catch(Exception e1)
add(text2); {}
}
16
Displaying Numeric Value
//SumNums.java
import java.applet.Applet;
import java.awt.*;
18
Thank you
19