Computer Engineering
Unit 1 Diploma
Java Applets Unit - 1
Java Applets
09CE1504
Prepared By:-
Prof. Smit Thacker
• An applet is a Java program that runs on a web
page
• An applet is embedded in an HTML page using
the APPLET or OBJECT tag and hosted on a
web server.
• Applets can be run within any modern
What is browser.
• To run modern Java applets, old browsers
Applet? need an up-to-date Java plug-in.
• Applet viewer is a program that can run.
• An application is a Java program that runs all
by itself.
• Java supplies a huge library of pre-written “code,”
ready for you to use in your programs
• Code is organized into classes
• Classes are grouped into packages
Packages and • One way to use this code is to import it
classes • You can import a single class, or all the classes in a
package
• To create an applet, you must import the Applet
class
• This class is in the java.applet package
The Applet
• The Applet class contains code that works with a
Class
browser to create a display window
• Capitalization matters!
• applet and Applet are different names
• Here is the directive that you need:
• import java.applet.Applet;
• import is a keyword
Importing the • java.applet is the name of the package
Applet class • A dot ( . ) separates the package from the class
• Applet is the name of the class
• There is a semicolon ( ; ) at the end
• “awt” stands for “Abstract Window Toolkit”
• The java.awt package includes classes for:
• Drawing lines and shapes
The java.awt • Drawing letters
package • Setting colors
• Choosing fonts
• If it’s drawn on the screen, then java.awt is probably
involved!
Since you may want to use many classes from the java.awt
package, simply import them all:
import java.awt.*;
Importing the The asterisk, or star (*), means “all classes”
java.awt package The import directives can go in any order, but must be the
first lines in your program
Applet Life Cycle Diagram
init()
Begin Born
stop()
start()
Applet Life
Cycle Running Idle
destroy()
paint() start()
Dead End
(1)public void init()
The first method invoked on the applet when it
is initially instantiated.
This is your chance to perform any
Applet Life initialization, such as locating resources or
preparing event handlers.
Cycle
(2) public void start()
Methods Invoked by the browser to inform the applet
that it should start executing.
The start() method is called right after the
init() method, and is also called when the page
is revisited.
(3) public void paint(Graphics g)
Invoked immediately after the start() method,
and also any time the applet needs to repaint
itself in the browser.
Applet Life The paint() method is actually inherited from
the java.awt. Graphics parameter is the
Cycle graphics context, representing the portion of
Methods the screen on which your applet is allowed to
paint.
(4) public void stop()
Invoked by the Web browser to inform the
applet that it should stop executing.
The stop() method is called right before the
Applet Life destroy() method is invoked, and also when a
user leaves the Web page.
Cycle Typically, anything you started in the start()
Methods method is stopped in the stop() method.
(5) public void destroy()
Invoked by the Web browser to inform the
applet that it is about to be destroyed (in other
words, garbage collected).
Applet Life Typically, any resources allocated in the init()
method are freed in the destroy() method.
Cycle
Methods
Applet Vs. Applications
HTML applet Tags
The <applet> tag in HTML was used to embed Java applets into
any HTML document.
The <applet> tag was deprecated in HTML 4.01, and it’s support
has been completely discontinued starting from HTML 5.
Alternatives available in HTML 5 are the <embed> and the
Applet Tags <object> tags.
There are still some browsers that support the <applet> tag with
the help of some additional plug-ins/installations to work.
The <applet> tag takes a number of attributes, with one of the
most important being the code attribute.
This code attribute is used to link a Java applet to the concerned
HTML document.
It specifies the file name of the Java applet.
Syntax
<applet code="URL" height="200" width="100">.............</applet>
Following are some specifications about <applet> tag
Display Block
Start tag/End tag Both Start tag and End tag
Usage Embed Applets
Applet Tags:
Example
Syntax, Example <html>
<head>
<title>Applet Tag</title>
</head>
<body>
<p>Example of Applet Tag</p>
<applet code="Shapes.class" height="200" width="300">
<b>Sorry! you need Java to see this</b>
</applet>
</body>
</html>
Attribute name Value Description
code URL It specifies the URL of Java applet class file.
width pixels It specifies the display width of the applet panel.
height pixels It specifies the display height of applet panel
align •left It specifies the position of applet application relative to
•right surrounding content.
•top
•middle
•bottom
Applet Tags : alt text It is used to display alternative text in case browser
does not support Java.
Attributes archive URL This specifies the archived or compressed version of
an applet application.
object name It specifies the URL or reference to a serialized
representation of an applet.
codebase URL It specifies the exact or relative URL of applets .class
file specified in the code attribute.
hspace pixels It specifies the horizontal space around the applet.
vspace pixels It specifies the vertical space around the applet.
name name It specifies the name for the applet
Types of Applets in Java
A special type of Java program that runs in a Web browser is
referred to as Applet.
It has less response time because it works on the client-side.
Types of It is much secured executed by the browser under any of the
Applets in platforms such as Windows, Linux and Mac OS etc.
There are two types of applets that a web page can contain.
Java
1) Local Applet
2) Remote Applet
Local Applets in Java
Local Applet is written on our own, and then we will embed it into
web pages.
Local Applet is developed locally and stored in the local system.
Types of A web page doesn't need to get the information from the internet
Applets in when it finds the local Applet in the system.
It is specified or defined by the file name or pathname.
Java There are two attributes used in defining an applet, i.e., the
codebase that specifies the path name and code that defined the
name of the file that contains Applet's code.
Specifying Local applet
<applet
codebase = "tictactoe"
code = "FaceApplet.class"
Types of width = 120
Applets in height = 120>
</applet>
Java
Local applet Example
1. First, we will create a Local Applet for embedding in a web page.
2. After that, we will add that Local Applet to the web page.
//Import packages and classes
import java.applet.*;
import java.awt.*;
Types of public class Test extends Applet
Applets in {
public void init()
{
Java setBackground(Color.white);
setForeground(Color.black);
}
public void paint(Graphics g)
{
g.drawString("Hello World!", 100, 100);
}
}
Remote Applets in Java
A remote applet is designed and developed by another developer.
It is located or available on a remote computer that is connected to
the internet.
Types of In order to run the applet stored in the remote computer, our
Applets in system is connected to the internet then we can download run it.
In order to locate and load a remote applet, we must know the
Java applet's address on the web that is referred to as Uniform
Recourse Locator(URL).
Specifying Remote applet
<applet
codebase = "http://www.xyz.com/applets/"
code = “Test.class"
Types of width = 120
Applets in height =120>
</applet>
Java
Difference Between Local
Applet and Remote Applet
Local Applet Remote Applet
There is no need to define the Applet's We need to define the Applet's URL in
Difference URL in Local Applet. Remote Applet.
Local Applet is available on our computer. Remote Applet is not available on our
Between computer.
In order to use it or access it, we don't need In order to use it or access it on our
Local applet Internet Connection. computer, we need an Internet Connection.
It is written on our own and then It was written by another developer.
and Remote embedded into the web pages.
We don't need to download it. It is available on a remote computer, so we
Applet need to download it to our system.
Hierarchy of Applet
Applet class extends Panel. Panel class extends Container which is the subclass of Component
Hierarchy of
Applet
Applet Panel Container Component Object
// A Hello World Applet OUTPUT
// Save file as HelloWorld.java
import java.applet.Applet;
import java.awt.Graphics;
// HelloWorld class extends Applet
Applet public class HelloWorld extends Applet
Example {
// Overriding paint() method
public void paint(Graphics g)
{
g.drawString("Hello World", 100, 100);
}
}
(1) The above java program begins with two import statements. The first import
statement imports the Applet class from applet package. Every AWT-based(Abstract
Window Toolkit) applet that you create must be a subclass (either directly or
indirectly) of Applet class. The second statement import the Graphics class from
AWT package.
(2) The next line in the program declares the class HelloWorld. This class must be
Explanation: declared as public because it will be accessed by code that is outside the program.
Inside HelloWorld, paint( ) is declared. This method is defined by the AWT and must
be overridden by the applet.
(3) Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
This method outputs a string beginning at the specified X,Y location. It has the
following general form:
void drawString(String message, int x, int y)
• After you enter the source code for HelloWorld.java, compile
in the same way that you have been compiling java
programs(using javac command).
Running the • However, running HelloWorld with the java command will
HelloWorld generate an error because it is not an application.
Applet :
• There are two standard ways in which you can run an applet:
• Executing the applet within a Java-compatible web browser.
• Using an applet viewer, such as the standard tool, applet-
Running the viewer.
HelloWorld • An applet viewer executes your applet in a window.
Applet : • This is generally the fastest and easiest way to test your
applet.
• 1. Using java enabled web browser :
• To execute an applet in a web browser we have to write a short HTML text
file that contains a tag that loads the applet. We can use APPLET or OBJECT
tag for this purpose.
• Using APPLET, here is the HTML file that executes HelloWorld :
Running the • <applet code="HelloWorld" width=200 height=60></applet>.
HelloWorld • 2. Using appletviewer :
Applet : • This is the easiest way to run an applet. To execute HelloWorld with an
applet viewer, you may also execute the HTML file shown earlier. For
example, if the preceding HTML file is saved with
• RunHelloWorld.html, then the following command line will run HelloWorld:
• appletviewer RunHelloWorld.html
Graphic Class in Applet
Graphic Class - Methods
Methods Description
public abstract void drawString(String str, int x, int y) It is used to display string.
public abstract void drawOval(int x, int y, int width, int height) It is used to draw Oval with specified height
Hierarchy of and width.
Applet
public abstract void fillOval(int x, int y, int width, int height) It is used to fill color with default color.
public abstract void drawLine(int x1, int y1, int x2, int y2) It is used to draw line.
public abstract void drawRect(int x, int y, int width, int height) It is used to draw rectangle with specified
dimension.
public abstract void fillRect(int x, int y, int width, int height) It is used to fill rectangle.
public abstract void setColor(Color c) It is used to set specific color.
public abstract void setFont(Font font) It is used to set specific font.
import java.applet.Applet; g.fillRect(170,100,30,30);
import java.awt.*; g.drawOval(60,180,100,100);
public class GraphicsDemo extends Applet g.fillOval(70,200,30,30);
{ g.setColor(Color.pink);
public void paint(Graphics g) g.drawArc(400,60,100,100,90,180);
{ g.drawArc(90,150,30,30,30,270);
Graphic Class - setBackground(Color.cyan);
}
g.setColor(Color.red);
Example g.drawString("Welcome",50, 50);
}
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
Graphic Class -
Output
Event Handling
• Event is nothing but change in the state of Object.
• For example Button press, Drag mouse, Mouse click etc..
• Three Components of Event Handling:
(1) Event:
It describes change in state of object
Events
Handling (2) Event Source:
It is an object which generate event.
(3) Event Listener:
Listener used to listen an event.
Events button.addActionListener(new ActionListener()
Handling - {
public void actionPerformed(ActionEvent e)
Example {
numClicks++;
label.setText(labelPrefix + numClicks);
}
}
);
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
public class EventDemo extends Applet implements ActionListener
{
Events Button bttn;
TextField txtfld;
Handling - public void init()
Example {
txtfld=new TextField();
txtfld.setBounds(35,45,250,30);
bttn=new Button("click me");
bttn.setBounds(90,110,70,60);
add(bttn);
add(txtfld);
bttn.addActionListener(this);
setLayout(null);
Events }
public void actionPerformed(ActionEvent ae)
Handling - {
Example txtfld.setText("Welcome To Advance Java Programming");
}
}
/*
<applet code="EventDemo" width="400" height="400">
</applet >
*/
• Swing Provides programmer the facility to change the look
and feel of components being displayed on any system.
• This is called “PLAF”.
Pluggable Look • “LOOK” refers to the appearance of the components on the
and Feel screen.
• “FEEL” represents how the user can interact with the
component.
• They are defined as classes in the javax.swing.plaf package.
There are three types of look and feel
1) Metal look and feel
Javax.swing.plaf.metal.MetalLookAndFeel
Pluggable Look
and Feel (cont) 2) Motif look and feel
Com.sun.java.swing.plaf.motif.MotifLookAndFeel
3) Windows look and feel
com.sun.java.swing.plaf.windows.WindowsLookAndFeel
Each picture shows the same program but with a different
look and feel.
Pluggable Look
and Feel (cont)
Thank you….