INTRODUCTION TO AWT
• AWT classes are contained in the java.awt package.It
is one of the Java’s largest packages.
• A full set of interface widgets and other
components,including windows,menu
buttons,text_box,check_box etc.
• Support for UI containers(Frame,Panel) which can
contain others embedded containers or User Interface
Widgets.
• An event system for managing system and user events
among parts of awt.
• Mechanism for laying out components.
Window Fundamentals
• AWT defines windows according to a class
heirarchy that adds functionality at each
specific level.
• Most commonly used windows are
• Panel-Used by applets
• Frame-creates a standard window.
• Much of the functionality of both the panel and
the frame is same as both are derived from the
same parent class.
LAYOUT MANAGER
CONTAINER
WINDOW SCROLL PANE
PANEL
Java,applet.Applet
Frame
Dialog
Component class
• This is the topmost class in AWT hierarchy.
• It is an abstract class that encapsulates all the
attributes of visual component.
• All UI elements that are displayed on screen and
that interact with the user are subclass
of component class.
. It defines over a public methods that are
Responsible for various events like Keyboard
&Mouse events,positioning and sizing of
window and repainting.
Component Class
• A component object is responsible for
remembering the cuurent foreground and
background colours and the currently selected
TextFont.
Container Class
• The Container class is a subclass of the
component class.
• It has additional methods that allow other
component objects that can be stored inside
another container.This makes for a Multileveld
containment system.
• A container is responsible for laying out any
components that it contains.For this it makes
the use of various layout managers.
Panel Class
• It is a concrete subclass of container
• It does’nt contain any method of its own.It
simply extends the container class
• Panel is a superclass of applet.
• A panel doesn’t contain a title bar,menu bar
or border and this is the reason why we cannot
see these items when an applet is run inside a
browser.But when run using appletviewer,the
appletviewer provides the title and border.
Panel
• Components or widgets an be added to the
panel by its add() method.Once these have
been added we can position or resize them
manually using setlocation(),setsize() and set
bounds() methods defined by components.
Window Class
• It is another class of container
• The Window class creates a top level window.
• The Top level window is not contained within
any other object.It sits directly on the desktop.
• Generally Window objects are not created
directly.Instead a subclass of window called
frame is used.
FRAME CLASS
• It is a subclass of window and has title
bar,menubar,borders and resizing corners.
• It encapsulates what is commonly thought of
as a window.
• We can add components or widget with its add
• Method.Then we can also use
setsize(),setlocation() and setBounds() to
positon and resize the widget.
• Frame Implements MenuContainer Interface.
Working with Frame Windows
• After the applet, the type of window you will most
often create is derived from Frame .
• it creates a standard-style window.
• Here are two of Frame ’s constructors:
• Frame( );
• Frame(String title );
• Notice that you cannot specify the dimensions of
• the window. Instead, you must set the size of the
window after it has been created.
Setting the Window’s Dimensions
• void setSize(int newWidth, int newHeight);
• void setSize(Dimension newSize);
• The getSize( )method is used to obtain the
current size of a window. Its signature is
• shown here:
• Dimension getSize( );
• This method returns the current size of the
window contained within the width and height
• fields of a Dimension object
Hiding and Showing a Window
• After a frame window has been created, it will
not be visible until you call setVisible( ) .
• void setVisible(boolean visibleFlag);
• The component is visible if the argument to this
method is true . Otherwise, it is hidden
• Setting a Window’s Title
• You can change the title in a frame window using
setTitle( ),
• void setTitle(String newTitle)
Creating a Frame Window in an Applet
• Most of the time, you will create a subclass of
Frame . Doing so lets you override Frame ’s
methods and provide event handling.
• Creating a new frame window from within an
applet is actually quite easy. First, create a
subclass of Frame . Next, override any of the
standard applet methods, such as init( ) ,
start( ) , and stop( ) , to show or hide the frame
as needed.
• Finally, implement the windowClosing( )
method of the WindowListenerinterface, calling
setVisible(false) when the window is closed.
• Once you have defined a Frame subclass, you can
create an object of that class. You make it visible
by calling setVisible( ).
• When created, the window is given a default
height and width. You can set the size of the
window explicitly by calling the setSize() method.
• The following applet creates a subclass of
Frame called SampleFrame. A window of this
subclass is instantiated within the init()
method of AppletFrame.
• Notice that SampleFrame calls Frame ’s
constructor. This causes a standard frame
window to be created with the title
passed in title .
Handling Events in a Frame Window
• Since Frame is a subclass of Component, it
inherits all the capabilities defined by
Component.
• Whenever an event occurs in a window, the
event handlers defined by that window will be
called. Each window handles its own events.
• For example, the following program creates a
window that responds to mouse events. The
main applet window also responds to mouse
events.
Closing a Frame Window
• When using a frame window, your program must
remove that window from the screen when it is
closed, by calling setVisible(false).
• To intercept a window-close event, you must
implement the windowClosing( )method of
theWindowListenerinterface.
• Inside windowClosing( ),
• you must remove the window from the screen.
• Finally, implement the windowClosing( )
• method of the WindowListenerinterface, calling
setVisible(false) when the window is closed.
AWT PACKAGES
• Java.awt.package contains the core awt graphics classes.
• GUI component classes(Button,TextField and label).
• GUI Container classes(Frame,panel,Dialog &Scroll pane)
• GUI Layout manager(FlowLayout,BorderLayout
,GridLayout)
• Custom Graphics classes(Graphics,color and font).
• Containers and Components
• Component: Components are elementary GUI elements.
• Container:Containers (such as frame,panel and Applet)
• Are used to hold components in a specific layout
• (such as flow or grid).A container can also hold sub
containers
• In GUI program a component must be kept in
container.You need to identify,a container to
hold the components.Every container has a
method called add(Component c).
• A container can invoke a
container.add(component) to add a
component to itself.
For Eg panel p1=new panel();
Button btn=new Button(“Press”);
panel.add(btn);
AWT CONTAINER CLASSES
• Top level containers:Frame,Dialog and Applet.
• Each GUI program has a top-level container.The
commonly used top level containers in Awt are
Frame,Dialog and Applet.
• A frame provides the “main window” for the
GUI application,which has title bar[containing
an icon, a title,minimize,maximize/restore-
down and close buttons].
• To write a GUI Program,we typically start with a
subclass extending from java.awt.Frame to
inherit the main window as follows.
• An Awt Dialog is a pop up window used for
interacting with the users.A dialog has a title
bar(Containing an icon,title and close button)
and content display area is displayed.
• An AWT applet is the top level container for an
applet,which is a java program running inside
browser.
• Secondry containers:Panel and Scrollpane
• Secondry containers are placed inside a top-
level container or another secondry
container.AWT also provide these secondry
containers.
• Panel:A rectangular box under a high level
container,used to layout a set of related GUI
components in pattern such as Grid or flow.
• Scroll Pane:Provides automatic horizontal
and/or vertical scrolling for a single child
component.
LAYOUT MANAGER
CONTAINER
WINDOW SCROLL PANE
PANEL
Java,applet.Applet
Frame
Dialog
Working with Graphics
• The AWT supports a rich assortment of graphics
methods. All graphics are drawn relative to a
window. This can be the main window of an
applet, a child window of an applet, or a stand-
alone application window.
• All output to a window takes place through a
graphics context. A graphics context is
encapsulated by the Graphics class and is
obtained in two ways:
• It is passed to an applet when one of its various
methods, such as paint( )or update( ),is called.
• It is returned by the getGraphics( ) method of
Component.
• The Graphics class defines a number of drawing
functions. Each shape can be drawn edge-only
or filled. Objects are drawn and filled in the
currently selected graphics color, which
• is black by default.
Drawing Lines
• void drawLine(int startX , int startY , int endX,
int endY);
• drawLine( )displays a line in the current
drawing color that begins at startX ,startY and
ends at endX,endY.
• // Draw lines
• import java.awt.*;
• import java.applet.*;
• public class Lines extends Frame{
• public void paint(Graphics g) {
• g.drawLine(0, 0, 100, 100);
• g.drawLine(0, 100, 100, 0);
• g.drawLine(40, 25, 250, 180);
• g.drawLine(75, 90, 400, 400);
• g.drawLine(20, 150, 400, 40);
• g.drawLine(5, 290, 80, 19);
• }
• }
Drawing Rectangles
• void drawRect(int top, int left, int width, int
height);
• void fillRect(int top, int left, int width, int
height);
• To draw a rounded rectangle, use
drawRoundRect( )or fillRoundRect( ), both
shown here:
• void drawRoundRect(int top, int left, int width,
int height,int xDiam , int yDiam );
• void fillRoundRect(int top, int left, int width,
int height,int xDiam , int yDiam );
• A rounded rectangle has rounded corners. The
upper-left corner of the rectangle is at top,left.
• The dimensions of the rectangle are specified
by width and height. The diameter of the
rounding arc along the X axis is specified by
xDiam.The diameter of the rounding arc along
the Y axis is specified by yDiam.
• // Draw rectangles
• import java.awt.*;
• import java.applet.*;
• public class Rectangles extends Frame{
• public void paint(Graphics g) {
• g.drawRect(10, 10, 60, 50);
• g.fillRect(100, 10, 60, 50);
• g.drawRoundRect(190, 10, 60, 50, 15, 15);
• g.fillRoundRect(70, 90, 140, 100, 30, 40);
• }
• }
Working with Graphics
• The AWT supports a rich assortment of Graphics
methods. All graphics are drawn relative to a window.
This can be the main window of an applet, a child
window of an applet, or a stand-alone application
window.
• The origin of each window is at the top-left corner and
is 0,0.
• A graphics context is encapsulated by the Graphics
class and is obtained in two ways:
It is passed to an applet when one of its various
methods, such as paint( ) or update( ),is called.
Working with Graphics
• Among other things the Graphics class defines a
number of methods that draw various types of
objects ,such as lines,rectangles and arcs.
• Objects are drawn and filled in the currently
selected colour;which is black by default.
• When a graphics object is drawn that exceeds
the dimensions of the window output is
automatically clipped.
Drawing Lines and Rectangles
• Lines are drawn by means of drawLine() method
shown here:
• void drawLine(int startx,int starty,int endx,int
endy)
• Void drawRect(int left,int top,int width,int
height)
• Void fillRect(int left,int top,int width,int height)
• Void drawRoundRect(int left,int top,int width,int
height,int xDiam,int yDiam) ;
Drawing Rectangles
• A rounded rectangle has rounded corners.The
upper left corner of the rectangle ia at left
top.The Dimensions of the rectangle are
specified by width and height.
• The diameter of the rounding arc along the x
axis is specified by xDiam.The diameter of the
rounding arc along the y axis is specified by
yDiam.
Drawing Ellipses and Circles
• void drawOval(int top, int left, int width, int
height)
• void fillOval(int top, int left, int width, int height)
• The ellipse is drawn within a bounding rectangle
whose upper-left corner is specified by
• top,left and whose width and height are specified
by width and height. To draw a circle, specify
• a square as the bounding rectangle.
• The following program draws several ellipses:
• // Draw Ellipses
• import java.awt.*;
• public class Ellipses extends Frame {
• public void paint(Graphics g) {
• g.drawOval(10, 10, 50, 50);
• g.fillOval(100, 10, 75, 50);
• g.drawOval(190, 10, 90, 30);
• g.fillOval(70, 90, 140, 100);
• }
• }
Drawing Arcs
• void drawArc(int top, int left, int width, int height, int startAngle,int
sweepAngle);
• void fillArc(int top, int left, int width, int height, int startAngle,
• int sweepAngle);
• The arc is bounded by the rectangle whose upper-left corner is specified by
top,left and whose width and height are specified by width and height.
• The arc is drawn from startAngle through the angular distance specified by
sweepAngle.
• Angles are specified in degrees. Zero degrees is on the horizontal, at the
three o’clock position.
• The arc is drawn counterclockwise if
sweepAngle is positive, and clockwise if
sweepAngle is negative.
• Therefore, to draw an arc from twelve
• o’clock to six o’clock, the start angle would be
90 and the sweep angle 180.
• import java.awt.*;
• import java.awt,event.*;
• Public class GraphicsDemo extends Frame{
• Public GraphicsDemo(){
• addWindowListener(new WindowAdapter() {
• Public void windowClosing(WindowEvent we){
• System.exit(0);
• }
• };
• }
Working with Graphics
public void paint(Graphics g) {
• g.drawLine(20,40,100,90);
• g.drawLine(20,90,100,40);
• g.drawLine(40,45,250,90);
• //Draw Rectangles
• g.drawRect(20,150,60,50);
• g.fillRect(110,150,60,50);
• g.drawRoundRect(200,150,60,50,15,15);
• g.fillRoundRect(290,150,60,50,30,40);
• g.drawOval(20,250,50,50);
Working with Graphics
• //Draw Arcs
• g.drawArc(10, 40, 70, 70, 0, 75);
• g.fillArc(100, 40, 70, 70, 0, 75);
• //Draw Polygons
• int xpoints[]={20,200,20,200,20};
• int ypoints[]={450,450,650,650,450}
• Int num=5;
• g.drawPolygon(xpoints,ypoints,num);
• }
Working with Graphics
• Public static void main(String args[]){
• GraphicsDemo appwin=new GraphicsDemo();
• appwin.setsize(new Dimension(370,700));
• Appwin.setTitle(“GraphicsDemo”);
• Appwin.SetVisible(true);
• }
Working with Graphics
• public class Arcs extends Applet {
• public void paint(Graphics g) {
• g.drawArc(10, 40, 70, 70, 0, 75);
• g.fillArc(100, 40, 70, 70, 0, 75);
• g.drawArc(10, 100, 70, 80, 0, 175);
• g.fillArc(100, 100, 70, 90, 0, 270);
• g.drawArc(200, 80, 80, 80, 0, 180);
• }
• }
• Sample output from this program is shown he
• import java.awt.*;
• public class Arcs extends Frame {
• public void paint(Graphics g) {
• g.drawArc(10, 40, 70, 70, 0, 75);
• g.fillArc(100, 40, 70, 70, 0, 75);
• g.drawArc(10, 100, 70, 80, 0, 175);
• g.fillArc(100, 100, 70, 90, 0, 270);
• g.drawArc(200, 80, 80, 80, 0, 180);
• }
• }
• Sample output from this program is shown he
Drawing Polygons
• void drawPolygon(int x[ ], int y[ ], int
numPoints)
• void fillPolygon(int x[ ], int y[ ], int numPoints)
• The polygon’s endpoints are specified by the
coordinate pairs contained within the x and y
• arrays. The number of points defined by x and
y is specified by numPoints.
• // Draw Polygon
• import java.awt.*;
• public class HourGlass extends Frame {
• public void paint(Graphics g) {
• int xpoints[] = {30, 200, 30, 200, 30};
• int ypoints[] = {30, 30, 200, 200, 30};
• int num = 5;
• g.drawPolygon(xpoints, ypoints, num);
• }
• }
Working with Color
• The AWT color system allows you to specify any
color you want. It then finds the best match for
that color, given the limits of the display
hardware currently executing your program or
applet.
• Color is encapsulated by the Color class.
• Color defines several constants You can also
create your own colors, using one of the
color constructors.
• Three commonly used forms are shown here:
• Color(int red, int green, int blue)
• Color(int rgbValue)
• Color(float red, float green, float blue)
• The first constructor takes three integers that specify the color
as a mix of red, green, and blue. These values must be between
0 and 255.
• new Color(255, 100, 100); // light red
• The second color constructor takes a single integer that
contains the mix of red, green, and blue packed into an integer.
• The integer is organized with red in bits 16 to 23, green in bits 8
• to 15, and blue in bits 0 to 7. Here is an example of this
constructor.
• int newRed = (0xff000000 | (0xc0 << 16) | (0x00 << 8) | 0x00);
• Color darkRed = new Color(newRed);
• The final constructor, Color(float, float, float), takes three float
values (between 0.0 and 1.0) that specify the relative mix of
red, green, and blue.
Color Methods
• Using Hue, Saturation, and Brightness
• The hue-saturation-brightness (HSB) color model is an
alternative to red-green-blue (RGB) for specifying particular
colors.
• Figuratively, hue is a wheel of color.
• The hue is specified with a number between 0.0 and 1.0((the
colors are approximately red, orange, yellow, green, blue,
• indigo, and violet).
• Saturation is another scale ranging from 0.0 to 1.0, representing
light pastels to intense hues.
• Brightness values also range from 0.0 to 1.0, where 1 is bright
white and 0 is black.
• Color supplies two methods that let you convert
between RGB and HSB. They are shown here:
• static int HSB to RGB(float hue, float saturation,
float brightness)
• static float[ ] RGB to HSB(int red, int green, int
blue , float values [ ]);
• HSB to RGB( )returns a packed RGB value
compatible with the Color(int) constructor. RGB
to HSB( )returns a float array of HSB values
corresponding to RGB integers.
getRed( ), getGreen( ), getBlue( )
• You can obtain the red, green, and blue
components of a color independently using
getRed( ),getGreen( ), and getBlue( ), shown
here.
• int getRed( );
• int getGreen( );
• int getBlue( );
• Each of these methods returns the RGB color
component found in the invoking Color object
in the lower 8 bits of an integer.
getRGB( )
• To obtain a packed, RGB representation of a
color, use getRGB( ), shown here:
• int getRGB( );
• The return value is organized as described
earlier.
• Setting the Current Graphics Color
• You can change this color by calling the Graphics
method setColor( ).
• void setColor(Color newColor )
• Here,newColor specifies the new drawing color.
Working With Colour
Working With Colour