0% found this document useful (0 votes)
42 views39 pages

Fundamentals of Software Development CT010-3-1: Java API and Class Libraries The Abstract Window Toolkit

The document discusses Java applets and GUI components. It provides an overview of applets and differences between applets and applications. It also describes various AWT components like labels, text fields, text areas, buttons and methods for drawing in applets.

Uploaded by

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

Fundamentals of Software Development CT010-3-1: Java API and Class Libraries The Abstract Window Toolkit

The document discusses Java applets and GUI components. It provides an overview of applets and differences between applets and applications. It also describes various AWT components like labels, text fields, text areas, buttons and methods for drawing in applets.

Uploaded by

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

Fundamentals of Software

Development
CT010-3-1
Java API and Class Libraries
The Abstract Window Toolkit

Prepared by: NR First Prepared on:21 st July 2005 Last Modified on:15th December 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation

Topic & Structure of the lesson


Java API and Class Libraries

Introduction to Applets
Difference between an applet and an application
Drawing methods
Executing applets

The Abstract Window Toolkit


Creating and Using GUI Components

TextField
TextArea
Button
Choice
List
Checkbox
CheckBoxGroup
Label

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Learning Outcomes
At the end of this topic, you should be able to:
Implement the usage of various classes
Write applet programs using the drawing
methods available
State and explain the various GUI
components learnt
Create, edit, compile, run and debug simple
Java programs implementing GUI
components.
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Key Terms
If you have mastered this topic, you should be able to
use the following terms correctly in your assignments
and exams:
Applet
Application
AWT
GUI
TextField
TextArea
Button
Choice
List
Checkbox
CheckBoxGroup
Label
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Introduction to Applet
Differences:

Applet

Application

A Java Applet is made up of


at least one public class
that has to be subclasses
from java.awt.Applet.The
applet is confined to living
in the users Web
browser,and the browsers
security rules.

CT010-3-1 Fundamentals of Software Development

A Java application is made


up of a main() method
declared as public static
void that accepts a string
array argument,along with
any other classes that
main() calls.It lives in the
environment that the host
OS provides.

API, Applets & AWT

Overview of an Applet
Every Applet is implemented by creating a
subclass of the Applet class.
Hierarchy of an Applet:
java.lang.Object
java.awt.Component
java.awt.Contanier
java.awt.Panel
java.awt.Applet
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Creating A Java Applet


1. Create the HelloWorld program below :
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString(HelloWorld,50,25);
}
}

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Creating A Java Applet


2.

Compile the applet Source file


Compile the java source file using the java
compiler like this:
javac HelloWorld.java
If the compilation succeeds, the compiler creates
a file called HelloWorld.class in the same
directory(folder) as the java source file
(HelloWorld.java). This class file contains java
bytecodes, which are platform independent
codes interpreted by java runtime system.

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Creating A Java Applet


3.

Create the HTML file


Using the text editor,create a file named Hello.html in
the same directory that contains HelloWorld.class.
The HTML file should contains the following text:
<HTML>
<HEAD>
<TITLE>A SIMPLE PROGRAM</TITLE>
</HEAD>
<BODY>
Here is the output of the HelloWorld program:
<APPLET CODE=HelloWorld.class WIDTH=400
HEIGHT=400></APPLET>
</BODY>
</HTML>

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Creating A Java Applet


4. Run the applet
To run the applet, you need to load the HTML file
into an application that can run java applets.An
example would be a Netscape Navigator or
Internet Explorer.

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Creating A Java Applet


You can also run the applet using a Java Applet
viewing program,such as AppletViewer provided by
the JDK.To run the applet issue this command.
appletviewer Hello.html
And you will get this:

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Drawing in Applets
Drawing Methods:

drawLine (x1, y1, x2, y2)


drawRect(x, y, width, height)
clearRect(x, y, width, height)
drawRoundRect(x, y, width, height, arcWidth,
arcHeight)
fillRoundRect(x, y, width, height, arcWidth, arcHeight)
draw3DRect(x, y, width, height, boolean raised)
drawOval (x, y, width, height)
fillOval (x, y, width, height)
drawArc(x, y, width, height, startAngle, arcAngle)
fillArc(x, y, width, height, startAngle, arcAngle)
drawPolygon(int [ ] xPoints, int[ ] yPoints, npoints)
drawString(String s, x, y)
drawImage(im, 10, 20, this)

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Drawing in Applets
The predefined colors include:

CT010-3-1 Fundamentals of Software Development

white
black
blue
yellow
lightGray
red
green

API, Applets & AWT

gray
pink
magenta
darkGray
Orange
cyan

Example 1 : Creating an Olympic Logo


import java.awt.*;
import java.applet.*;
public class drawOlympics extends Applet {
public void paint(Graphics g) {
setBackground(Color.pink);
g.setColor(Color.blue);
g.drawOval(50,50,100,100);
g.setColor(Color.yellow);
g.drawOval(165,50,100,100);
g.setColor(Color.black);
g.drawOval(285,50,100,100);
g.setColor(Color.green);
g.drawOval(105,100,100,100);
g.setColor(Color.red);
g.drawOval(225,100,100,100);
}
}
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Example 2 : Creating a Face


import java.awt.*;
import java.applet.*;
public class drawObjects extends Applet {
public void paint(Graphics g) {
int[] x={155,230,305};
int[] y={215,155,215};
setBackground(new Color(220,255,245));
g.setColor(Color.darkGray);
g.drawArc(20, 20, 200, 150, 45, 90);
g.drawArc(220, 20, 200, 150, 45, 90);
g.setColor(new Color(180,180,240));
g.fillOval(70,50,100,100);
g.fillOval(270,50,100,100);
g.setColor(new Color(230,230,150));
g.fillPolygon(x, y, 3);
g.setColor(new Color(250,210,210));
g.fillRoundRect(155,260,150,50, 15,15);
} }
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Quick Review Question


What are the differences between a Java Applet and a
Java Application?
Name some Java drawing methods.
Which Java API should you import to draw in an
applet?

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Abstract Windowing ToolKit


A rich set of tools provided by Java to help the programmer
build Graphical User Interfaces (GUI) with ease.
GUIs build with the AWT promises this:
The final interface can run anywhere!
This is unlike many GUI software's which are mainly
hardware or operating system dependant.

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - Label


A Label object is a component for placing text in
a container. It is used for displaying a single line
of read-only text.
The text can be changed by the application, but a
user cannot edit it directly.
The Label object is located in the following API
hierarchy;
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.Label
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - Label


Label
Label lName;
lName = new Label(Name :);
add(lName);
OR
Label lName = new Label(Name :);
add(lName);

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - Label


Creating your first Label
import java.awt.*;
public class MyFrame extends Frame {
public static void main(String[] args) {
MyFrame me = new MyFrame();
}
public MyFrame() {
setSize(150,150);
setLocation(100,100);
setTitle("My First java.awt.Frame!");
setLayout(new FlowLayout());
Label l = new Label("java.awt.Label");
add(l);
setVisible(true);
}
}
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - Label


The output of the program might look
something like this :

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - TextField


A TextField object is a text component that
allows for the editing of a single line of text.
This allows for general user-input.
The TextField object is located in the following
API hierarchy;
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.TextField

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - TextField


TextField
TextField tName;
tName = new Textfield(30);
add(tName);
OR
TextField tName = new TextField(30);
add(tName);

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - TextField


Various Variations of TextField
TextField tf1, tf2, tf3, tf4;
// a blank text field
tf1 = new TextField();
// blank field of 20 columns
tf2 = new TextField("", 20);
// predefined text displayed
tf3 = new TextField("Hello!");
// predefined text in 30 columns
tf4 = new TextField("Hello", 30);

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - TextField


import java.awt.*;
public class MyFrame extends Frame {
public static void main(String[] args) {
MyFrame me = new MyFrame();
}
public MyFrame() {
setSize(150,150);
setLocation(100,100);
setTitle("My First java.awt.Frame!");
setLayout(new FlowLayout());
Label l = new Label("java.awt.Label");
add(l);
TextField tf = new
TextField("java.awt.TextField", 20);
add(tf);
setVisible(true);
}
}
CT010-3-1 Fundamentals of Software Development
API, Applets & AWT

AWT Components - TextField


The output of the program might look
something like this :

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - TextArea


A TextArea object is a multi-line region that
displays text.
It can be set to allow editing or to be read-only.
The TextArea object is located in the following
API hierarchy;
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.TextArea
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - TextArea


TextArea
TextArea tComment;
tComment = new TextArea (Please type in your
comment here, 5, 40);
add(tComment);
OR
TextArea tComment = new TextArea(Please type in
your comment here, 5, 40);
add(tComment);

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - TextArea


Creating a TextArea
TextArea ta = new TextArea("java.awt.TextArea", 5, 20);
add(ta);

By adding the codes above to the previous program,


the output of the program might look something
like this :

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - Button


Button
Button okButton;
okbutton = new Button(OK);
add(okButton);
OR
Button okButton = new Button(OK);
add(okButton);

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - Button


Creating a Button
Button b = new Button("OK");
add(b);
By adding the codes above to the previous program,
the output of the program might look something
like this :

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - List


The List component presents the user with a
scrolling list of text items
The List can be set up so that the user can
choose either one item or multiple items.
The List object is located in the following API
hierarchy;
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.List
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - List


Creating a List
List lst = new List(4, false);
lst.add("Mercury");
lst.add("Venus");
lst.add("Earth");
lst.add("JavaSoft");
lst.add("Mars");
lst.add("Jupiter");
lst.add("Saturn");
lst.add("Uranus");
lst.add("Neptune");
lst.add("Pluto");
add(lst);
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - List


By adding the codes above to the previous program, the
output of the program might look something like this :

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - Choice


The Choice class presents a pop-up menu of
choices.
The Choice object is located in the following API
hierarchy;
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.Choice
CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - Choice


Creating a Choice
Choice c = new Choice();
c.add("red");
c.add("green");
c.add("blue");
add(c);

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

AWT Components - Choice


By adding the codes above to the previous program, the
output of the program might look something like this :

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Summary of Main Teaching Points


Java API and Class Libraries

Introduction to Applets
Difference between an applet and an application
Drawing methods
Executing applets

The Abstract Window Toolkit


Creating and Using GUI Components

TextField
TextArea
Button
Choice
List
Checkbox
CheckBoxGroup
Label

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

Next Lesson
The Abstract Window Toolkit
Using Layout Managers
FlowLayout
GridLayout
BorderLayout
Sample Programs

CT010-3-1 Fundamentals of Software Development

API, Applets & AWT

You might also like