Documente Academic
Documente Profesional
Documente Cultură
GRAPHICS
PROGRAMMING
USING AWT
UNIT V
Mallikharjuna Rao K
AWT
Abstract Window Toolkit represents a
class library to develop applications
using GUI.
The java.awt package got classes
and interfaces to develop GUI which
is useful to interact more friendly.
UNIT V
Mallikharjuna Rao K
Object
Check box
Group
Compone
nt
Label
Button
Check
box
Choice
Canvas
Scrollb
ar
TextCompo
nen
Check box
Group
UNIT V
Mallikharjuna Rao K
TextFi
eld
TextAr
ea
Pan
el
Appl
et
Windo
Fram
w
e
UNIT V
Mallikharjuna Rao K
Creating a Frame
A FRAME becomes the basic component in AWT.
The FRAME has to be created before any other component. Because all other
components can be displayed in a frame.
Three ways:
Create a Frame class object
Frame f = new
Frame( );
Create a subclass MyFrame class and create an object to the subclass as:
UNIT V
UNIT V
Mallikharjuna Rao K
UNIT V
Mallikharjuna Rao K
Compone
nt
This method
handles event
Listener
Method3( )
event
UNIT V
Method2( )
Mallikharjuna Rao K
Mallikharjuna Rao K
UNIT V
Mallikharjuna Rao K
10
UNIT V
public void
windowActivated(WindowEvent e)
public void
windowClosed(WindowEvent e)
public void
windowClosing(WindowEvent e)
public void
windowDeactivated(WindowEvent e)
public void Mallikharjuna Rao K
11
Mallikharjuna Rao1
K
VERSION
12
UNIT V
Mallikharjuna Rao2
K
VERSION
13
Uses of a Frame
Once the frame is created, we can use it for any of
the purposes mentioned below:
To draw some graphical shapes like dots, lines,
rectangles,.etc. in the frame.
To display some text in the frame
To display pictures or images in the frame
To display components like push buttons, radio buttons, etc
in the frame.
UNIT V
Mallikharjuna Rao K
14
Mallikharjuna Rao K
15
UNIT V
UNIT V
Mallikharjuna Rao K
17
UNIT V
Mallikharjuna Rao K
18
g.fillRect(40,40,200,200);
// set yellow color
g.setColor(Color.yellow);
//face
g.fillOval(90,70,80,80);
// set black color
g.setColor(Color.black);
// eyes
g.fillOval(110,95,5,5);
g.fillOval(145,95,5,5);
// nose
g.drawLine(130,95,130,115);
//set red color
g.setColor(Color.red);
//mouth
g.fillArc(113,115,35,20,0,-180);
UNIT V
Mallikharjuna
}Rao K
19
UNIT V
Mallikharjuna Rao K
20
Displaying Dots
To display a dot or point on the screen, we
can take the help of drawLine() method.
UNIT V
Mallikharjuna Rao K
21
Mallikharjuna Rao K
22
Mallikharjuna Rao K
23
UNIT V
Mallikharjuna Rao K
24
}
});
}
UNIT V
this.setBackground(new
Color(100,20,20));
// set font for the text
g.setColor(Color.green);
// display the message
Mallikharjuna Rao K
25
import java.awt.*;
class Fonts
{
public static void main(String s[])
{
// get the local graphics environment information into GraphicsEnvironment object ge
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
//from ge, get available font family names into fonts[]
String fonts[] = ge.getAvailableFontFamilyNames();
System.out.println("Available fonts on this system:");
//retrieve one by one the font names from fonts[] and display
for(int i=0; i<fonts.length; i++)
System.out.println(fonts[i]);
}
} UNIT V
Mallikharjuna Rao K
26
We can display images like .gif and .jpg files in the frame. We should
follow the below steps.
Load image into Image class object using getImage() method of
the Toolkit class.
Image img = Toolkit.getDefaultToolkit().getImage(diamonds.gif)
But, loading the image into img will take some time.JVM uses a
separate thread to load the image into img and continues with
the rest of the program.
To do this we need Media Tracker class. Add the image to
MediaTracker class and allot an identification number to it
MediaTracker track = new
starting from
0,1,
MediaTracker(this);
Track.addImage(img,0); // 0 is the id number
of image
UNIT V
Rao K
50,Mallikharjuna
null)
27
UNIT V
Mallikharjuna Rao K
28
UNIT V
Mallikharjuna Rao K
29
UNIT V
Mallikharjuna Rao K
30
UNIT V
Mallikharjuna Rao K
31
Push Buttons
Button class is useful to create Push buttons. A
Push button is useful to perform a particular action.
To create a push button with a label, we can create
an object to Button class. As
Mallikharjuna Rao K
32
To know the source object which has been clicked by the user,
we can use, getSource() method of ActionEvent class, as:
Object obj = ae.getSource();
b.addActionListener(ActionListener obj);
b.removeActionListener(ActionListener obj);
UNIT V
Mallikharjuna Rao K
33
Push buttonsMybuttons.java
UNIT V
Mallikharjuna Rao K
34
Check Boxes
A check box is a square shaped box which displays an option to the user. The
user can select one or more options from a group of check boxes.
To create check box, we can create an object to Checkbox class, as:
Cb.setState(true);
String s = cb.getLabel();
UNIT V
Object x[] =
Mallikharjuna Rao K
cb.getSelectedObjects();
35
Mycheckbox.java
UNIT V
Mallikharjuna Rao K
36
Radio Button
A radio button represents a round shaped button, such that only
one can be selected from a group of buttons.
Radio buttons can be created using CheckboxGroup class and
Checkbox classes.
Creation of a radio button
First we should create a CheckboxGroup class object.
We should pass CheckboxGroup object to the Checkbox class. It represents the
group to which the radio button belongs.
Checkbox cb =
cbg.getSelectedCheckbox();
UNIT V
String label =
cbg.getSelectedCheckbox().getLabel;
Mallikharjuna Rao K
37
Myradio.java
UNIT V
Mallikharjuna Rao K
38
Text Field
A text field represents a long rectangular box where the user
can type a single line of text.
To create a TextField:
TextField tf = new TextField();
TextField tf = new TextField(25);
UNIT V
Mallikharjuna Rao K
39
TextArea
A textArea is similar to a text field, it can accommodate
several lines of text.
To create a TextArea:
TextArea ta = new TextArea();
TextArea ta = new
TextArea(rows, cols);
ta.setText(text);
Mallikharjuna Rao K
40
Label
UNIT V
Mallikharjuna Rao K
41
UNIT V
Mallikharjuna Rao K
42
Choice class
Choice class is useful to display a choice menu. It is a pop-up list
of items and the user can select only one item from the
available items.
To create a Choice menu:
Choice ch = new Choice();
Once a choice menu is created, we should add items to it using
add() method, as:
ch.add(item);
ch.removeAll();
Mallikharjuna Rao K
43
Mychoice.java
UNIT V
Mallikharjuna Rao K
44
List class
This class is useful to create a list box which is similar to choice
menu.
A list box presents the user with a scrolling list of text items.
The user can select one or more items from the list box.
To create a list box, we can create an object to List class:
List lst = new List();
To add items to the list box, we can use add() method, as:
lst.add(item);
Mallikharjuna Rao K
45
Mylist.java
UNIT V
Mallikharjuna Rao K
46
Scrollbar Class
Scrollbar class is useful to create scrollbars that can be
attached to a frame or text area.
Scrollbars are used to select continuous values between a
specified minimum and maximum.
To create a scrollbar we can create an object to Scrollbar
class, as: Scrollbar sb = new Scrollbar(alignment, start,
step, min, max);
UNIT V
Mallikharjuna Rao K
47
Myscroll.java
UNIT V
Mallikharjuna Rao K
48
UNIT V
Mallikharjuna Rao K
49
UNIT V
Mallikharjuna Rao K
50
Layout Managers
Layout Managers
Flow Layout
Grid Layout
Border Layout
UNIT V
Mallikharjuna Rao K
51
Layout Managers
Layout managers
Provided to arrange GUI components in a
container
Provide basic layout capabilities
Implement the interface LayoutManager
UNIT V
Mallikharjuna Rao K
52
FlowLayout
FlowLayout
Simplest layout manager
Components are placed left to right in
the order they are added
Components can be left aligned,
centered or right aligned
UNIT V
Mallikharjuna Rao K
53
Mallikharjuna Rao K
54
UNIT V
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
Mallikharjuna Rao K
55
FlowLayout Demo
UNIT V
Mallikharjuna Rao K
56
57
FlowLayoutDemo.java
BorderLayout
BorderLayout
Arranges components into five regions
north, south, east, west and center
Implements interface LayoutManager2
Provides horizontal gap spacing and
vertical gap spacing
UNIT V
Mallikharjuna Rao K
58
Border Layout
Arrangement
If one or more
of the components, except the
Center component, are missing then the rest
of the existing components are stretched to fill
the remaining space in the container.
UNIT V
Mallikharjuna Rao K
59
Mallikharjuna Rao K
60
UNIT V
FlowLayout.NORTH
FlowLayout.SOUTH
FlowLayout.EAST
FlowLayout.WEST
FlowLayout.CENTER
Mallikharjuna Rao K
61
BorderLayout Demo
UNIT V
Mallikharjuna Rao K
62
GridLayout
GridLayout
Divides container into a grid
Every component has the same width
and height
UNIT
63 V
Mallikharjuna Rao K
UNIT V
Mallikharjuna Rao K
64
Mallikharjuna Rao K
65
GridLayout Demo
UNIT V
Mallikharjuna Rao K
66