The document discusses Java AWT event handling and graphics. It covers key concepts like events, event classes, event handling process, commonly used event listeners and adapter classes. It also covers AWT containers, layout managers, menu classes, graphics classes and how to work with frames and graphics in Java. The document is intended to teach programming in Java and is part of a larger unit on AWT.
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
PROGRAMMING IN JAVA- unit 4-part II
1. PROGRAMMING IN JAVA
A. SIVASANKARI
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER APPLICATION
SHANMUGA INDUSTRIES ARTS AND SCIENCE
COLLEGE,
TIRUVANNAMALAI. 606601.
Email: sivasankaridkm@gmail.com
2. PROGRAMMING IN JAVA
UNIT - 4
PART II
AWT EVENT HANDLING
AWT EVENT CLASSES
AWT EVENT LISTENERS
AWT ADAPTER CLASSES
AWT CONTAINERS
AWT LAYOUT MANAGER
AWT MENU CLASSES
MENU CONTROLS
AWT GRAPHICS CLASSES
GRAPHICS
WINDOW FUNDAMENTALS
WORKING WITH FRAME WINDOW
WORKING WITH GRAPHICS
A. SIVASANKARI - SIASC-TVM UNIT-4
3. PROGRAMMING IN JAVA
AWT EVENT HANDLING
EVENT
• Change in the state of an object is known as event i.e. event describes the change in state of
source. Events are generated as result of user interaction with the graphical user interface
components. For example, clicking on a button, moving the mouse, entering a character
through keyboard, selecting an item from list, scrolling the page are the activities that causes
an event to happen.
TYPES OF EVENT
• The events can be broadly classified into two categories:
• Foreground Events - Those events which require the direct interaction of user. They are
generated as consequences of a person interacting with the graphical components in Graphical
User Interface. For example, clicking on a button, moving the mouse, entering a character
through keyboard, selecting an item from list, scrolling the page etc.
• Background Events - Those events that require the interaction of end user are known as
background events. Operating system interrupts, hardware or software failure, timer expires,
an operation completion are the example of background events.
A. SIVASANKARI - SIASC-TVM
4. PROGRAMMING IN JAVA
EVENT HANDLING
• Event Handling is the mechanism that controls the event and decides what should happen if
an event occurs. This mechanism have the code which is known as event handler that is
executed when an event occurs. Java Uses the Delegation Event Model to handle the events.
This model defines the standard mechanism to generate and handle the events.
• The Delegation Event Model has the following key participants namely:
• Source - The source is an object on which event occurs. Source is responsible for providing
information of the occurred event to it's handler. Java provide as with classes for source
object.
• Listener - It is also known as event handler. Listener is responsible for generating response to
an event. From java implementation point of view the listener is also an object. Listener waits
until it receives an event. Once the event is received , the listener process the event an then
returns.
• The benefit of this approach is that the user interface logic is completely separated from the
logic that generates the event. The user interface element is able to delegate the processing
of an event to the separate piece of code. In this model ,Listener needs to be registered with
the source object so that the listener can receive the event notification. This is an efficient way
of handling the event because the event notifications are sent only to those listener that want
to receive them.
STEPS INVOLVED IN EVENT HANDLING
• The User clicks the button and the event is generated.
• Now the object of concerned event class is created automatically and information about the
source and the event get populated with in same object.
• Event object is forwarded to the method of registered listener class.
• The method is now get executed and returns.
A. SIVASANKARI - SIASC-TVM
5. PROGRAMMING IN JAVA
AWT EVENTS
• Java.awt.event.*;
EVENT CLASSES
Syntax:
• Event object(object src)
It contains two methods.
1. getsource( ) returns source of the event 2.tostring( ) returns the string equivalent of the
event.
Events Commands
Action event Button press or double clicked.
Adjustment event Scrollbar is manipulated.
Component event Hidden, resized, moved etc
Container event Component is added or removed.
Focus event Keyboard focus
Input event All input component
Item event Checkbox or listed items
Key event Keyboard process
Mouse event Mouse process
Text event Text area or text field
Window event Window close, open or quit.
A. SIVASANKARI - SIASC-TVM
8. PROGRAMMING IN JAVA
• Syntax:
• Boolean is Altdown( )
• Boolean is AltGraphdown( )
• Example:
• int getmodifiers( );
ITEM EVENT:
• The constants are
• Deselected
• Selected
• Syntax:
• ItemEvent( itemSelectable src, int type, obj entry)
• Example:
• Object getItem( );
KEY EVENT:
• Digits: VK _ 0 TO VK_ 9
• Character: VK _ A to VK _ Z, VK _ ENTER, VK_ UP .. etc
• Syntax:
• KeyEvent(component src, int type, long when, int modifiers, int code, char ch);
A. SIVASANKARI - SIASC-TVM
9. PROGRAMMING IN JAVA
Example:
• Char getkeychar( );
• Int getkeycode ( );
MOUSE EVENT
• Constants are :
• MOUSE-CLICKED, MOUSE- ENTERED, MOUSE-DRAGGED, MOUSE-EXITED,
MOUSE-MOVED, MOUSE-PRESSED, MOUSE-RELEASED, MOUSE-WHEEL.
• Syntax:
• MouseEvent(Component src, int type, long when,
• int modifiers, int x, int y, int clicks, Boolean trigger )
Example:
• int getclickcount( );
• int getButton( );
MOUSE WHEEL EVENT
• Constants are:
• WHEEL-BLOCK-SCROLL
• WHEEL-UNIT-SCROLL
• Syntax:
• MouseWheelEvent(Component src, int type, long when, int Modifiers, int x, int y, int clicks,
• Boolean trigger, int amount,int scroll, int count)
A. SIVASANKARI - SIASC-TVM
10. PROGRAMMING IN JAVA
• Example:
• int getWheelRotation( )
• int getscrolltype( )
TEXT EVENT
• Constants are
• TEXT-VALUE-CHANGED
• Syntax:
• TextEvent(object src, int type)
WINDOW EVENT
• Constants are
• WINDOW-ACTIVATED, CLOSED, CLOSING,
• DEACTIVATED, LOST FOCUS, OPENED,…
• Syntax:
• WindowEvent(window src, int type)
• Example:
• int getAppositeWindow( )
• int getOldState ( )
SOURCE OF EVENTS
• Src-Button, checkbox, choice, list, menu item,
• scroll bar, text, window.
A. SIVASANKARI - SIASC-TVM
11. PROGRAMMING IN JAVA
AWT EVENT LISTENERS
• The Event listener represent the interfaces responsible to handle events. Java provides us
various Event listener classes but we will discuss those which are more frequently used. Every
method of an event listener method has a single argument as an object which is subclass of
EventObject class. For example, mouse event listener methods will accept instance of
MouseEvent, where MouseEvent derives from EventObject.
EventListner interface
• It is a marker interface which every listener interface has to extend. This class is defined in
java.util package.
• Java .awt.event.packages
Class declaration
• Following is the declaration for java.util.EventListener interface:
• public interface EventListener
• AWT Event Listener Interfaces:
Following is the list of commonly used event listeners.
ACTION LISTENER INTERFACES
• Example:
• void ActionPerformed(ActionEvent ae)
A. SIVASANKARI - SIASC-TVM
15. PROGRAMMING IN JAVA
AWT ADAPTER CLASSES
• An adapter class provides an empty implementation of all methods in an event listener interface. It
execute or want particular events
• Adapters are abstract classes for receiving various events. The methods in these classes are empty.
These classes exists as convenience for creating listener objects.
AWT Adapters
• Following is the list of commonly used adapters while listening GUI events in AWT.
Sr. No. Adapter & Description
1 FocusAdapter An abstract adapter class for receiving focus events.
2 KeyAdapter An abstract adapter class for receiving key events.
3 MouseAdapter An abstract adapter class for receiving mouse events.
4 MouseMotionAdapter An abstract adapter class for receiving mouse motion events.
5 WindowAdapter An abstract adapter class for receiving window events.
A. SIVASANKARI - SIASC-TVM
16. PROGRAMMING IN JAVA
AWT CONTAINERS
• Containers are integral part of AWT GUI components. A container provides a space where a
component can be located. A Container in AWT is a component itself and it adds the
capability to add component to itself. Following are noticeable points to be considered.
• Sub classes of Container are called as Containter. For example Panel, Frame and Window.
• Container can add only Component to itself.
• A default layout is present in each container which can be overridden using setLayout method.
AWT UI ELEMENTS
• Following is the list of commonly used containers while designed GUI using AWT.
S No. AWT Container & Description
1 Container It is a generic container object which can contain other AWT components.
S No AWT UI Container & Description
1 Panel Panel is the simplest container. It provides space in which any other component can be
placed, including other panels.
2 Frame A Frame is a top-level window with a title and a border
3 Window A Window object is a top-level window with no borders and no menubar.
A. SIVASANKARI - SIASC-TVM
17. PROGRAMMING IN JAVA
LAYOUT MANAGER
LAYOUT
• Layout means the arrangement of components within the container. In other way
we can say that placing the components at a particular position within the container.
The task of layouting the controls is done automatically by the Layout Manager.
LAYOUT MANAGER
• The layout manager automatically positions all the components within the
container. If we do not use layout manager then also the components are positioned
by the default layout manager. It is possible to layout the controls by hand but it
becomes very difficult because of the following two reasons.
• It is very tedious to handle a large number of controls within the container.
• Oftenly the width and height information of a component is not given when we
need to arrange them.
• Java provide us with various layout manager to position the controls. The properties
like size, shape and arrangement varies from one layout manager to other layout
manager. When the size of the applet or the application window changes the size,
shape and arrangement of the components also changes in response i.e. the layout
managers adapt to the dimensions of appletviewer or the application window.
• The layout manager is associated with every Container object.
• Each layout manager is an object of the class that implements the LayoutManager
interface.
A. SIVASANKARI - SIASC-TVM
18. PROGRAMMING IN JAVA
LAYOUT MARQUES
• Each container object as a layout manager associated with it.
• It is otherwise called as Layout Managers.
Syntax
• void SetLayout(LayoutManage layobject)
TYPES OF LAYOUT
S No. LayoutManager & Description
1 BorderLayout The borderlayout arranges the components to fit in the five regions: east,
west, north, south and center.
2 CardLayoutT he CardLayout object treats each component in the container as a card. Only
one card is visible at a time.
3 FlowLayout The FlowLayout is the default layout.It layouts the components in a directional
flow.
4 GridLayout The GridLayout manages the components in form of a rectangular grid.
5 GridBagLayout This is the most flexible layout manager class.The object of GridBagLayout
aligns the component vertically,horizontally or along their baseline without requiring the
components of same size.
A. SIVASANKARI - SIASC-TVM
20. PROGRAMMING IN JAVA
AWT MENU CLASSES
• As we know that every top-level window has a menu bar associated with it. This menu bar
consist of various menu choices available to the end user. Further each choice contains list of
options which is called drop down menus. Menu and MenuItem controls are subclass of
MenuComponent class.
MENU HIERARCHY
A. SIVASANKARI - SIASC-TVM
21. PROGRAMMING IN JAVA
MENU CONTROLS
S.
No.
Menu Controls & Description
1 MenuComponent It is the top level class for all menu related controls.
2 MenuBar The MenuBar object is associated with the top-level window.
3 MenuItem The items in the menu must belong to the MenuItem or any of its subclass.
4 Menu The Menu object is a pull-down menu component which is displayed from the menu
bar.
5 CheckboxMenuItem CheckboxMenuItem is subclass of MenuItem.
6 PopupMenu PopupMenu can be dynamically popped up at a specified position within a
component.
A. SIVASANKARI - SIASC-TVM
22. PROGRAMMING IN JAVA
AWT GRAPHICS CLASSES
• Graphics controls allows application to draw onto the component or on image.
GRAPHICS CONTROLS
SNo. Control & Description
1 Graphics It is the top level abstract class for all graphics contexts.
2 Graphics2D It is a subclass of Graphics class and provides more sophisticated control over
geometry, coordinate transformations, color management, and text layout.
3 Arc2D Arc2D is the abstract superclass for all objects that store a 2D arc defined by a framing
rectangle, start angle, angular extent (length of the arc), and a closure type (OPEN, CHORD,
or PIE).
4 CubicCurve2D The CubicCurve2D class is the abstract superclass fpr all objects which store a
2D cubic curve segment and it defines a cubic parametric curve segment in (x,y) coordinate
space.
5 Ellipse2D The Ellipse2D is the abstract superclass for all objects which store a 2D ellipse and
it describes an ellipse that is defined by a framing rectangle.
A. SIVASANKARI - SIASC-TVM
23. PROGRAMMING IN JAVA
Sr.
No.
Control & Description
6 Rectangle2D The Rectangle2D class is an abstract superclass for all objects that store a 2D
rectangle and it describes a rectangle defined by a location (x,y) and dimension (w x h).
7 QuadCurve2D The QuadCurve2D class is an abstract superclass for all objects that store a 2D
quadratic curve segment and it describes a quadratic parametric curve segment in (x,y)
coordinate space.
8 Line2D This Line2D represents a line segment in (x,y) coordinate space.
9 Font The Font class represents fonts, which are used to render text in a visible way.
10 Color The Color class is used encapsulate colors in the default sRGB color space or colors in
arbitrary color spaces identified by a ColorSpace.
11 BasicStroke The BasicStroke class defines a basic set of rendering attributes for the outlines of
graphics primitives, which are rendered with a Graphics2D object that has its Stroke attribute set
to this BasicStroke.
A. SIVASANKARI - SIASC-TVM
24. PROGRAMMING IN JAVA
GRAPHICS
WINDOW FUNDAMENTALS
1. COMPONENTS
• It is an abstract class that encapsulate all of the attributes of a visual components
• A components object is responsible for remembering the current foreground and
background colors and the currently selected text font.
2. CONTAINER
• The container class is a subclass of components
• A container is responsible for laying out any components that is contains.
3. PANEL
• It is a subclass of container
• It is a superclass for applet
• Panel is a window that does not contain a title bar, Menubar or border.
The methods are SetLocation(),setsize(), or setbounds()
4.WINDOW
• It is called window
5.FRAME
• It is a subclass of window and has a tittlebar, menubar,border and resizing corners
6.CANVAS
• Canvas encapsulate a blank window upon which we can draw
A. SIVASANKARI - SIASC-TVM
26. PROGRAMMING IN JAVA
FRAME
• Sytanx:
• Frame()
• Frame(string title)
Size setting
• Sytanx:
• Void SetSize( Int new width.int new height)
• Void SetSize( Dimension new size)
Hidding and Showing a Window
• Sytanx:
• Void Setvisible ( Boolean visible flag )
• // True Means Visible // False Hidden
Setting a window title
• Sytanx:
• Void settittle(string new title)
Closing a Frame
• Sytanx :
• Set visible(False) or Window Closing()
• Example
• Panel and Layout. Record
A. SIVASANKARI - SIASC-TVM
27. PROGRAMMING IN JAVA
WORKING WITH GRAPHICS
• The origin of each window is at the topleft corner and is 0.0.
• Coordinates are specified in Pixels use two ways.
• Paint() or update()
• getgraphics()
DRAWING LINES
• Syntax:
• Void drawline (int starting,int stray,int stray,int endx,int endy)
• Ex:
• g.drawline(0, 0, 100, 100)
• g.drawline(0, 100, 100 ,0)
DRAWING RECTANGLE
• Syntax:
• Void drawrect(int top, int left, int width, int height)
• Void fillrect(int top, int left, int width, int height)
ROUNDED RECTANGLE
• Syntax:
Void drawroundrect(int top, int left, int width,
int height, int xdim, int ydim)
• Example:
• g.drawrect(10, 10,60, 50)
• g.fillroundrect(70,90, 140.100,30,40,)
A. SIVASANKARI - SIASC-TVM
28. PROGRAMMING IN JAVA
DRAWING ELLIPSES AND CIRCLE
• Syntax:
• Void drawOval(int top, int left, int width, int height)
• Void filloval(int top, int left, int width, int height)
• Example :
• g. drawoval(10,10,50,50)
• g.filloval(100,10,75,50)
DRAWING ARCS
• Syntax:
• 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);
DRAWING POLYGONS
• Syntax:
• Void drawPolygon(int [],int, [], int num of points);
• Void fillPolygon(int [],int, [], int num of points);
WORKING WITH COLORS
• Syntax:
• Setcolor();
• New color(255,100,100);
Two type of colors:
• 1.HSB - Hue, Satuaration, Brightness
• 2. RGB – Red, Green, Blue
A. SIVASANKARI - SIASC-TVM
29. PROGRAMMING IN JAVA
Example:
• Get red();
• int getgreen();
• int getblue();
• int getrgb();
WORKING WITH FONTS
• Fonts have a family name, logical name and face name.
• Family namily General name of font
• Example: Courier
• Logical name Category of font
• Example: monospaced
• Face name Specific font
• Example: Courier italic
• Syntax are methods :
• String getfamily(); String getname(); Int get style();
• Boolean is Bold();
• Syntax: Font(String font name, int fontstyle, int pointsize)
• Ex: Void setfont(font fontobject)
A. SIVASANKARI - SIASC-TVM