You are on page 1of 10

School of Informatics

Department of Computer Science

Chapter 2 Java Applet

Compiled by Dawit Uta. (M. Tech.)


Computer Science Department, WSU
website address: www.davidtechnotips.com
2 Course outline

Chapter 2 Java Applet


2.1. Overview of Java Applets
2.2. Java Applets Vs Java Application
Overview of Applet
3
 Java programs that run on the Web are called applets;

 An applet is a small java program that are not stand-alone programs. Instead, they run
within either a web browser or an applet viewer.

 Java can work within the World Wide Web of computers via browsers, such as Netscape and
Internet Explorer, google chrome etc. Applets are usually run from web browsers.

 The JVM is able to run a Java program for the particular computer on which it is running.

 An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a
web server.
 Applets provide sound, graphics and animation in various forms and formats for web pages.
Overview of Applet cont…
4

 Applets are used to make the website more dynamic and entertaining, also used in
games, gaming consoles, commercial websites, learning tools and many more.

 All applets are sub-classes of the java.applet.Applet class

 Swing provides a newer subclass of the Applet class called javax.swing.Japplet so


more functionality is inherited by extending the JApplet class

 JDK provides a standard applet viewer tool called applet viewer.

 In general, execution of an applet does not begin at main() method.

 Output of an applet window is not performed by System.out.println(). Rather it is


handled with various AWT methods, such as drawString().
Life cycle of an applet :
5

 It is important to understand the order in which the various


methods shown here in the image are called.
 When an applet begins, the following methods are called, in
this sequence:
1.init( )
2.start( )
3.paint( )

 When an applet is terminated, the following sequence of


method calls takes place:
1.stop( )
2.destroy( )
Life cycle of an applet cont…
6

 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.
7

 paint(): is 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

 There many types of web applications and numerous ways in which applets may be
applied.
Java Applets Vs Java Application
8
Java Applets Example

9 import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World This is First Java Applet Program", 10, 50); } }

To run the applet program


javac HelloWorldApplet.java
appletviewer RunAppletHelloWorld.html

Save this html as “RunAppletHelloWorld.html”


10

Java Applets are Obsolete/depricated


 There are two distinct ways in which applets have been deprecated:
The applet element was deprecated in HTML 4.01 and rendered
entirely obsolete by HTML5.
 Modern web technologies like JavaScript, HTML5, and CSS3 have
largely replaced Java Applets for creating interactive web content.

End of Chapter Two

You might also like