Event Handling 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 Container
TextField TextArea Panel ScrollPane Window
Applet Dialog Frame
FileDialog
Container : Which can hold other AWT components. 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. ScrollPane: A container class which implements automatic horizontal and/or vertical scrolling for a single child component.
Canvas: A Canvas component represents a blank rectangular area of the screen onto which the application can draw or from which the application can trap input events from the user. Source : The source is an object which generates an event. Listener : A listener is an object that gets notified when an event occurs.
Event : The event is an object which describes the state change of the source component. Different sources generate different kinds of events. All the events types are defined in java.awt.event package, and EventObject of java.util package is the super class of all the event classes. 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. To handle the event, when an event occurs 1) Register the source to its corresponding listener. 2) Implement the listener api as what handling should be done for the event occurred. Types of Events : The state change in different source components is defined by different events. Source Button CheckBox Choice Scrollbar TextField, TextArea List Window Mouse Key
Event type ActionEvent ItemEvent ItemEvent AdjustmentEvent Action Event, TextEvent ActionEvent, ItemEvent WindowEvent MouseEvent KeyEvent
All the events are defined as classes in java.awt.event package. All listeners are interfaces in java present in java.awt.event package.
The listener interfaces declare abstract methods that has to be implemented in the class which implement these interface. For Example for ActionEvent the listener to be registered is ActionListener, and implement the actionPerformed() method. Source Button CheckBox Choice Scrollbar TextField, TextArea List Window Mouse Key
Event type ActionEvent ItemEvent ItemEvent AdjustmentEvent Action Event, TextEvent ActionEvent, ItemEvent WindowEvent MouseEvent KeyEvent
Listener ActionListener ItemListener ItemListener AdjustmentListener ActionListener, TextListener ActionListener, ItemListener WindowListener MouseListener, MouseMotionListener KeyListener
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)