0% found this document useful (0 votes)
5 views

OOP-Java_Unit-5

The document provides an overview of GUI programming in Java, focusing on key components such as AWT, Swing, and Applets. It explains the differences between AWT and Swing, highlighting Swing's advantages like lightweight components and platform independence. Additionally, it covers the lifecycle of Java Applets, their methods, and how to run them using HTML or the appletviewer tool.

Uploaded by

amreen2825
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

OOP-Java_Unit-5

The document provides an overview of GUI programming in Java, focusing on key components such as AWT, Swing, and Applets. It explains the differences between AWT and Swing, highlighting Swing's advantages like lightweight components and platform independence. Additionally, it covers the lifecycle of Java Applets, their methods, and how to run them using HTML or the appletviewer tool.

Uploaded by

amreen2825
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Prepared by GSK

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

 The buttons of AWT does not support


pictures.
 It is heavyweight in nature.

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

 Swing is Entirely written in Java

 Java Swing Components are Platform-independent


And The Swing Components are lightweight 8
Java Swing class Hierarchy Diagram

Prepared by GSK
9
WHAT IS A CONTAINER CLASS?

 Container classes are classes that can have other


components on it. So for creating a Java Swing GUI,
we need at least one container object. There are 3

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?

 The model designs based on the MVC


architecture follow MVC design pattern.
 The application logic is separated from the

Prepared by GSK
user interface while designing the software
using model designs.

 The MVC pattern architecture consists of


three layers:

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?

 An applet is a special kind of Java program that


runs in a Java enabled browser.

Prepared by GSK
 Applet is typically embedded inside a web page and
runs in the browser.

 The applet can produce a graphical user interface.

 To create an applet, a class must class


extends java.applet.Applet class.

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.

 Note: Java Applet is deprecated since Java 9. It


means Applet API is no longer considered important.

17
Hierarchy of Applet:

Prepared by GSK
18
APPLET VS JAVA APPLICATION

 An applet is a Java class that extends the


java.applet.Applet class.

Prepared by GSK
 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.

 When a user views an HTML page that contains an


applet, the code for the applet is downloaded to the
user's machine.
19
LIFECYCLE OF JAVA APPLET

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

 public void init( ): is used to initialized the Applet.


It is invoked only once.
 public void start( ): is invoked after the init( )

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

 The Component class provides 1 life cycle method of


applet.

Prepared by GSK
 public void paint(Graphics g): is used to paint
the Applet.

 It provides Graphics class object that can be used


for drawing oval, rectangle,strings, arc etc.

23
HOW TO RUN AN APPLET?

 There are two ways to run an applet


 By html file.

Prepared by GSK
 By appletviewer tool (for testing purpose).

24
SIMPLE EXAMPLE OF APPLET BY HTML FILE

 To view the applet in a web browser, you will require


a java enabled web browser. To enable java in the
browser, go to browser advanced setting an enable

Prepared by GSK
java. The following steps should be followed :

a) Create an HTML file as above, containing the


APPLET tag.
b) Compile the applet source code using javac.
c) Open the html file in the web browser.

25
import java.applet.Applet;
import java.awt.Graphics;

public class FirstApplet extends Applet


{
Applet code

Prepared by GSK
(Java File)
public void paint(Graphics g)
Save as:
{ FirstApplet.java
g.drawString("welcome",150,150);
} Compile:
javac FirstApplet.java

Note: class must be public because its object is


26
created by Java Plugin software that resides on the
browser.
<html>
<body>

<applet code="FirstApplet.class" width="300"


height="300">

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:

a) Write HTML APPLET tag in comments in the


source file.
b)Compile the applet source code using javac.
c) Use appletviewer ClassName.class to view the
applet. 28
Save as:
First.java
import java.applet.Applet;
import java.awt.Graphics; Compile:
javac First.java
public class First extends Applet Run:
{ appletviewer First.java

public void paint(Graphics g)


{
g.drawString("welcome to applet",150,150);
}

}
/*
<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
{

public void paint(Graphics g)


{
String str=getParameter("msg");
g.drawString(str,50, 50);
}

} 31
<html>
<body>

<applet code="UseParam.class" width="300"

Prepared by GSK
height="300">

<param name="msg" value="Welcome to applet">

</applet>
</body>
</html>
32

You might also like