Unit VI EventHandling&AWT
Unit VI EventHandling&AWT
AWT: Abstract window Toolkit is the part of java to create the graphical user
interface(GUI) components. These are the set of classes in java.awt package used for
creating graphical interfaces for the java applications or the applets.
Components Hierarchy:
Component
Button
CheckBox
Choice
Label
List
ScrollBar
Canvas
TextComponent TextField
TextArea
Container Panel Applet
ScrollPane
Window Dialog FileDialog
Frame
Window : Window is the top level container with no title and border.
Frame : Frame is the top level container with both title and border.
Panel: A panel is the simplest containers. It can add other panels and AWT components.
But must be placed on some other container.
Applet: An applet is a small java program that can be executed in an java compatible
web browser or the appletviewer.
Applet can be either remote or local. A remote applet is trhe applet that is downloaded
from remote server using the URL(Universal Resource Locator).
The local applet is the one which is created in the local pc.
Dialog: A Dialog is a top-level window with a title and a border that is typically used to
take some form of input from the user.
FileDilaog : The FileDialog class displays a dialog window from which the user can
select a file.
Event : The event is an object which describes the state change of the source
component.
Event Handling-:
DelegationEventModel : In this technique of event handling , only the events whose
sources are registered to the respective listeners are handled ignoring all others.
In java earlier versions the all the events handled down the hierarchy, causing the
wastage of time and memory.
Font class:
Constants:
public static final int PLAIN
public static final int BOLD
public static final int ITALIC
Constructor :
public Font(String fontName, int style, int size)
Methods:
public int getStyle()
public int getSize()
public String getName()
public String getFamily()
public boolean isPlain()
public boolean isBold()
public boolean Italic()
public Font getFont()
public void setFont(Font f)
Font Metrics:
Methods:
public int getAcsent()
public int getDescent()
public int getLeading()
public int getHeight()
public FontMetrics getFontMetrics()- Returns the FontMetrics object for the
current font.
public FontMetrics getFontMetrics(Font f)- Returns the FontMetrics object for the
specified font.
Color Class :
Constants:
public static final Color ORANGE 255, 200, 0 (R, G, B) values
public static final Color PINK 255, 175, 175
public static final Color CYAN 0, 255, 255
public static final Color MAGENTA 255, 0, 255
public static final Color YELLOW 255, 255, 0
public static final Color BLACK 0, 0, 0
public static final Color WHITE 255, 255, 255
public static final Color GRAY 128, 128, 128
public static final Color RED 255, 0, 0
public static final Color GREEN 0, 255, 0
public static final Color BLUE 0, 0, 255
Constructors:
public Color(int r, int g, int b) – creates the Color object with the given r,g,b
values ranging between 0- 255.
public Color(float r, float g, float b) - creates the Color object with the given r,g,b
values ranging between 0.0- 1.0.
Methods:
public int getRed() – Returns the red color intensity in the current color object.
public int getGreen()– Returns the green color intensity in the current color
object.
public int getBlue()– Returns the blue color intensity in the current color object.
public Color getColor()- Returns the Color object for current graphics color.
public void setColor(Color c)- Sets the given color for the drawing.
Graphics class:
Graphics context & Graphics Object : The graphics context enables the drawing on the
screen and the graphics object manages the graphics context by controlling how
information is drawn.
The graphics object has the methods for drawing, color manipulation and font
manipulation.
In order to manage the applets graphics context, the graphics object is used for drawing
on the screen.
Component class –
paint(Graphics g) This graphics object ‘g’ is sent by the system when a
paint operation is required for a component. This object is the refernce of the system
derived Graphics class.
Graphics class is an abstract class.
Methods:
abstract void drawLine(int x1, int y1, int x2, int y2)
abstract void drawOval(int x, int y, int width, int height)
abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
void drawPolygon(Polygon p)
void drawRect(int x, int y, int width, int height)
abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int
arcHeight)
abstract void drawString(String str, int x, int y)
abstract void drawArc(int x, int y, int width, int height, int startAngle, int
arcAngle)
abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
abstract void fillOval(int x, int y, int width, int height)
abstract void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
void fillPolygon(Polygon p)
abstract void fillRect(int x, int y, int width, int height)
abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int
arcHeight)
abstract Color getColor()
abstract Font getFont()
abstract void setColor(Color c)
abstract void setFont(Font f)