OOP-Java_Unit-5
OOP-Java_Unit-5
UNIT-5
APPLETS
OBJECT ORIENTED PROGRAMMING
THROUGH JAVA
1
Prepared by GSK
GUI PROGRAMMING
WITH SWING
2
WHAT IS GUI IN JAVA?
GUI (Graphical User Interface) in Java is an easy-
to-use visual experience builder for Java
applications.
Prepared by GSK
It is mainly made of graphical components like
buttons, labels, windows, etc. through which the
user can interact with an application.
GUI plays an important role to build easy
interfaces for Java applications.
We use AWT, swing, applet to develop GUI
Applications.
3
It is a type of
software interface,
offering a service to
Prepared by GSK
other pieces of
software
4
WHAT IS AWT?
AWT stands for Abstract Window Toolkit.
It is a platform-dependent API to develop
GUI (Graphical User Interface) or window-
based applications in Java.
Prepared by GSK
It was developed by Sun Microsystem In
1995.
It is heavy-weight in use because it is
generated by the system’s host operating
system.
It contains a large number of classes and
methods, which are used for creating and
managing GUI. 5
LIMITATIONS OF AWT
Prepared by GSK
Two very important components trees and
tables are not present.
Extensibility is not possible as it is platform
dependent
6
WHAT IS SWING IN JAVA?
Swing in Java is a Graphical User Interface (GUI)
toolkit that includes the GUI components.
Swing provides a rich set of widgets and packages to
Prepared by GSK
make sophisticated GUI components for Java
applications.
Swing is a part of Java Foundation Classes(JFC),
which is an API for Java GUI programing that
provide GUI.
The Java Swing library is built on top of the Java
Abstract Widget Toolkit (AWT), an older, platform
dependent GUI toolkit.
7
WHAT IS SWING IN JAVA? CONTD..
Swing is a Set Of API ( API- Set Of Classes and
Interfaces )
Swing is Provided to Design Graphical User
Prepared by GSK
Interfaces
Includes New and improved Components that have
been enhancing the looks and Functionality of GUIs’
Swing can be used to build(Develop) The Standalone
swing GUI Apps Also as Servlets And Applets
It Employs model/view design architecture
Prepared by GSK
9
WHAT IS A CONTAINER CLASS?
Prepared by GSK
types of Java Swing containers.
1. Panel: It is a pure container and is not a window in
itself. The sole purpose of a Panel is to organize
the components on to a window.
2. Frame: It is a fully functioning window with its
title and icons.
3. Dialog: It can be thought of like a pop-up window
that pops out when a message has to be displayed. It
is not a fully functioning window like the Frame.
10
WHAT IS MVC?
The Model-View-Controller (MVC) is a well-
known design pattern in the web
development field. It is way to organize our
Prepared by GSK
code.
It specifies that a program or application
shall consist of data model, presentation
information and control information.
The MVC pattern needs all these
components to be separated as different
objects.
11
WHAT IS MVC ARCHITECTURE IN JAVA?
Prepared by GSK
user interface while designing the software
using model designs.
12
WHAT IS MVC ARCHITECTURE IN JAVA?
CONTD..
In MVC terminology, the model corresponds
to the state information associated with the
Prepared by GSK
Component
The view determines how the component is
displayed on the screen, including any aspects
of the view that are affected by the current
state of the model.
The controller determines how the
component reacts to the user
13
WHAT IS MVC ARCHITECTURE IN JAVA?
CONTD..
Prepared by GSK
14
Prepared by GSK
JAVA APPLET
15
WHAT IS AN APPLET?
Prepared by GSK
Applet is typically embedded inside a web page and
runs in the browser.
16
WHAT IS AN APPLET? CONTD…
An Applet class does not have any main() method.
This is the first Java program that can run over the
network using the browser.
Prepared by GSK
JVM creates an instance of the applet class and
invokes init() method to initialize an Applet.
17
Hierarchy of Applet:
Prepared by GSK
18
APPLET VS JAVA APPLICATION
Prepared by GSK
A main() method is not invoked on an applet, and an
applet class will not define main().
Prepared by GSK
20
LIFECYCLE METHODS FOR APPLET:
The java.applet.Applet class provides 4 life cycle
methods
java.awt.Component class provides 1 life cycle
Prepared by GSK
methods for an applet.
21
JAVA.APPLET.APPLET CLASS
Prepared by GSK
method or browser is maximized. It is used to start
the Applet.
public void stop( ): is used to stop the Applet. It is
invoked when Applet is stop or browser is
minimized.
public void destroy( ): is used to destroy the
Applet. It is invoked only once.
22
JAVA.AWT.COMPONENT CLASS
Prepared by GSK
public void paint(Graphics g): is used to paint
the Applet.
23
HOW TO RUN AN APPLET?
Prepared by GSK
By appletviewer tool (for testing purpose).
24
SIMPLE EXAMPLE OF APPLET BY HTML FILE
Prepared by GSK
java. The following steps should be followed :
25
import java.applet.Applet;
import java.awt.Graphics;
Prepared by GSK
(Java File)
public void paint(Graphics g)
Save as:
{ FirstApplet.java
g.drawString("welcome",150,150);
} Compile:
javac FirstApplet.java
Prepared by GSK
</applet>
</body>
</html>
HTML Code:
Write this code in notepad and save with .html
extension.
27
Just double click on html file to run
SIMPLE EXAMPLE OF APPLET BY
APPLETVIEWER TOOL
It is a java tool provided to view applets.
It is like a small browser provided to have a preview of
applet as they would look in browser.
Prepared by GSK
It understands APPLET tag and used in the creation
phase. The APPLET tag should be written in source
code file enclosing in comments.
The following steps should be followed:
}
/*
<applet code="First.class" width="300" height="300"
> 29
</applet>
*/
PARAMETER IN APPLET
We can get any information from the HTML file as a
parameter.
For this purpose, Applet class provides a method
named getParameter( ).
Syntax:
public String getParameter(String parameterName)
30
EXAMPLE OF USING PARAMETER IN APPLET:
import java.applet.Applet;
import java.awt.Graphics;
Prepared by GSK
public class UseParam extends Applet
{
} 31
<html>
<body>
Prepared by GSK
height="300">
</applet>
</body>
</html>
32