0% found this document useful (0 votes)
3 views17 pages

Java Applet and AWT Basics

Java Applet is a client-side program embedded in webpages that generates dynamic content, offering advantages like reduced response time and cross-platform compatibility, but requiring a plugin to run. The applet lifecycle includes initialization, starting, painting, stopping, and destruction, with specific methods defined in the java.applet.Applet class. Java AWT (Abstract Window Toolkit) is used for creating GUI applications, with components being platform-dependent and the framework allowing for various container types such as Frame and Panel.
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)
3 views17 pages

Java Applet and AWT Basics

Java Applet is a client-side program embedded in webpages that generates dynamic content, offering advantages like reduced response time and cross-platform compatibility, but requiring a plugin to run. The applet lifecycle includes initialization, starting, painting, stopping, and destruction, with specific methods defined in the java.applet.Applet class. Java AWT (Abstract Window Toolkit) is used for creating GUI applications, with components being platform-dependent and the framework allowing for various container types such as Frame and Panel.
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/ 17

Java Applet

Applet is a special type of program that is embedded in the webpage to generate the dynamic content.
It runs inside the browser and works at client side.

Advantage of Applet
There are many advantages of applet. They are as follows:

o It works at client side so less response time.


o Secured
o It can be executed by browsers running under many plateforms, including Linux, Windows, Mac
Os etc.

Drawback of Applet

o Plugin is required at client browser to execute applet.

Do You Know
o Who is responsible to manage the life cycle of an applet ?
o How to perform animation in applet ?
o How to paint like paint brush in applet ?
o How to display digital clock in applet ?
o How to display analog clock in applet ?
o How to communicate two applets ?

Hierarchy of Applet

As displayed in the above diagram, Applet class extends Panel. Panel class extends Container which is
the subclass of Component.

Lifecycle of Java Applet


1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.

Lifecycle methods for Applet:


The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle
methods for an applet.
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of
applet.

1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is maximized. It is used to start
the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.

java.awt.Component class
The Component class provides 1 life cycle method of applet.

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

Who is responsible to manage the life cycle of an applet?


Java Plug-in software.

How to run an Applet?


There are two ways to run an applet

1. By html file.
2. By appletViewer tool (for testing purpose).

Simple example of Applet by html file:


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. Now click the html file.

//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{

public void paint(Graphics g){


g.drawString("welcome",150,150);
}

Note: class must be public because its object is created by Java Plugin software that resides on the
browser.
myapplet.html

<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>

Simple example of Applet by appletviewer tool:


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 First.java. Now Html file is not required but it is for testing
purpose only.

//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
}

10. }
11. /*
12. <applet code="First.class" width="300" height="300">
13. </applet>
14. */

To execute the applet by appletviewer tool, write in command prompt:

c:\>javac First.java
c:\>appletviewer First.java

Java AWT Tutorial


Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or windows-based
applications in Java.

Java AWT components are platform-dependent i.e. components are displayed according to the view of
operating system. AWT is heavy weight i.e. its components are using the resources of underlying operating
system (OS).
The java.awt package

provides classes
for AWT API such as TextField
, Label
, TextArea
, RadioButton, CheckBox
, Choice
, List
etc.

The AWT tutorial will help the user to understand Java GUI programming in simple and easy steps.

Play Videox

Why AWT is platform independent?


Java AWT calls the native platform calls the native platform (operating systems) subroutine for creating API
components like TextField, ChechBox, button, etc.

For example, an AWT GUI with components like TextField, label and button will have different look and feel
for the different platforms like Windows, MAC OS, and Unix. The reason for this is the platforms have
different view for their native components and AWT directly calls the native subroutine that creates those
components.

In simple words, an AWT application will look like a windows application in Windows OS whereas it will look
like a Mac application in the MAC OS.

Java AWT Hierarchy


The hierarchy of Java AWT classes are given below.
Components
All the elements like the button, text fields, scroll bars, etc. are called components. In Java AWT, there are
classes for each component as shown in above diagram. In order to place every component in a particular
position on a screen, we need to add them to a container.

Container
The Container is a component in AWT that can contain another components like buttons

, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel.

It is basically a screen where the where the components are placed at their specific locations. Thus it contains
and controls the layout of components.

Note: A container itself is a component (see the above diagram), therefore we can add a container inside container.

Types of containers:

There are four types of containers in Java AWT:

1. Window
2. Panel
3. Frame
4. Dialog

Window

The window is the container that have no borders and menu bars. You must use frame, dialog or another
window for creating a window. We need to create an instance of Window class to create this container.
Panel

The Panel is the container that doesn't contain title bar, border or menu bar. It is generic container for
holding the components. It can have other components like button, text field etc. An instance of Panel class
creates a container, in which we can add components.

Frame

The Frame is the container that contain title bar and border and can have menu bars. It can have other
components like button, text field, scrollbar etc. Frame is most widely used container while developing an
AWT application.

Useful Methods of Component Class

Method Description

public void add(Component c) Inserts a component on this component.

public void setSize(int width,int height) Sets the size (width and height) of the component.

public void setLayout(LayoutManager m) Defines the layout manager for the component.

public void setVisible(boolean status) Changes the visibility of the component, by default false.

Java AWT Example


To create simple AWT example, you need a frame. There are two ways to create a GUI using Frame in AWT.

1. By extending Frame class (inheritance)


2. By creating the object of Frame class (association)
AWT Example by Inheritance
Let's see a simple example of AWT where we are inheriting Frame class. Here, we are showing Button
component on the Frame.

AWTExample1.java

1. // importing Java AWT class


2. import java.awt.*;
3.
4. // extending Frame class to our class AWTExample1
5. public class AWTExample1 extends Frame {
6.
7. // initializing using constructor
8. AWTExample1() {
9.
10. // creating a button
11. Button b = new Button("Click Me!!");
12.
13. // setting button position on screen
14. b.setBounds(30,100,80,30);
15.
16. // adding button into frame
17. add(b);
18.
19. // frame size 300 width and 300 height
20. setSize(300,300);
21.
22. // setting the title of Frame
23. setTitle("This is our basic AWT example");
24.
25. // no layout manager
26. setLayout(null);
27.
28. // now frame will be visible, by default it is not visible
29. setVisible(true);
30. }
31.
32. // main method
33. public static void main(String args[]) {
34.
35. // creating instance of Frame class
36. AWTExample1 f = new AWTExample1();
37.
38. }
39.
40. }
download this example
The setBounds(int x-axis, int y-axis, int width, int height) method is used in the above example that sets the
position of the awt button.

Output:

AWT Example by Association


Let's see a simple example of AWT where we are creating instance of Frame class. Here, we are creating a
TextField, Label and Button component on the Frame.

AWTExample2.java

1. // importing Java AWT class


2. import java.awt.*;
3.
4. // class AWTExample2 directly creates instance of Frame class
5. class AWTExample2 {
6.
7. // initializing using constructor
8. AWTExample2() {
9.
10. // creating a Frame
11. Frame f = new Frame();
12.
13. // creating a Label
14. Label l = new Label("Employee id:");
15.
16. // creating a Button
17. Button b = new Button("Submit");
18.
19. // creating a TextField
20. TextField t = new TextField();
21.
22. // setting position of above components in the frame
23. l.setBounds(20, 80, 80, 30);
24. t.setBounds(20, 100, 80, 30);
25. b.setBounds(100, 100, 80, 30);
26.
27. // adding components into frame
28. f.add(b);
29. f.add(l);
30. f.add(t);
31.
32. // frame size 300 width and 300 height
33. f.setSize(400,300);
34.
35. // setting the title of frame
36. f.setTitle("Employee info");
37.
38. // no layout
39. f.setLayout(null);
40.
41. // setting visibility of frame
42. f.setVisible(true);
43. }
44.
45. // main method
46. public static void main(String args[]) {
47.
48. // creating instance of Frame class
49. AWTExample2 awt_obj = new AWTExample2();
50.
51. }
52.
53. }

Output:

You might also like