Chapter 2 Java Applets
Chapter 2 Java Applets
University of Gondar
College of Informatics
1
Aleka T.
2017 E.C
Chapter Two : Java Applets
ByAleka T.
2
2017 E.C
Chapter 2: Java Applets
Overview
Java applets vs java Application
3 2017 E.C
Overview
Applets are small applications that are accessed on an internet
server, transported over the internet, automatically installed and
run as part of a web document
5 2017 E.C
Hierarchy of Applet
6 2017 E.C
Applet Lifecycle
An applet undergoes various stages between its creation of
objects and object removal as the work of the applet will get
done.
7 2017 E.C
…
java.applet.Applet class
For creating any applet java.applet.Applet class must be
inherited.
It provides 4 life cycle methods of applet:
public void init(): is used to initialize the Applet.
public void start(): is invoked after the init() method or browser is
maximized.
public void stop(): is used to stop the Applet.
public void destroy(): is used to destroy the Applet.
8 2017 E.C
…
java.awt.Component class
The Component class provides 1 life cycle method of applet.
public void paint(Graphics g): is used to paint the Applet.
It provides Graphics class object that can be used for drawing
oval, rectangle, arc etc.
9
1
2017 E.C
0
How do we run an Applet?
Two ways to run an applet
By html file
By appletViewer tool (for testing purpose).
To execute the applet by html file, create an applet and compile
it. After that create an html file and place the applet code in html
file. Then run it…
1
2017 E.C
1
Steps to run applet in XAMPP server
1. Javac and Java version must be 1.8.0+
Check version in CMD using : java –version javac –version
2. Place html and java file in htdocs folder of local web server (XAMPP)
3. Compile .java file using cmd javac command
Javac “java file name”.java
4. Install an applet runner extension in browser E.g Cheerpj Applet
Runner
5. Open the html file using local web server
6. Activate applet runner extension.
1
2017 E.C
1
// AppletTest.java
import java.applet.Applet; import java.awt.Graphics;
public class AppletTest extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("welcome",150,150);
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);
}
1 }
2017 E.C
2
…
<!--Mapplet.html--!>
1.<html>
<title> Applet </title>
2. <body>
3.<applet code=“AppletTest.class" width="300" height="300">
4.</applet>
5.</body>
6.</html>
1
2017 E.C
3
…
To execute the applet by appletviewer tool, create an applet that
contains applet tag in comment and compile it.
After that run it by: appletviewer AppletTest.java. Now Html file
is not required but it is for testing purpose only.
c:\>javac AppletTest.java
c:\>appletviewer AppletTest.java
1
2017 E.C
4
Chapter 2: Java Applets
Overview
Java applets vs java Application
1
2017 E.C
5
Java applets vs java Application