The document discusses Java AWT (Abstract Window Toolkit). It describes that AWT is an API that allows developing GUI applications in Java. It provides classes like TextField, Label, TextArea etc. for building GUI components. The document then explains key AWT concepts like containers, windows, panels, events, event handling model, working with colors and fonts.
Java AWT (AbstractWindow Toolkit) Elizabeth Alexander Hindustan University
2.
Java AWT ● JavaAWT (Abstract Window Toolkit) is an API to develop GUI or window- based applications in java. ● Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. ● The java.awt package provides classes for AWT API such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc. ● The AWT was designed to provide a common set of tools for graphical user interface design that work on a variety of platforms.
Java AWT Controls ●Container : The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel. ● Window : The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. ● Panel : The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc. ● Frame : The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.
5.
AWT UI Elements ●Label - A Label object is a component for placing text in a container. ● Button - This class creates a labeled button. ● Check Box - A check box is a graphical component that can be in either an on(true) or off (false) state. ● Check Box Group - The CheckboxGroup class is used to group the set of checkbox. ● List - The List component presents the user with a scrolling list of text items. ● Text Field - A TextField object is a text component that allows for the editing of a single line of text. ● Text Area - A TextArea object is a text component that allows for the editing of a multiple lines of text. ● Choice - A Choice control is used to show pop up menu of choices. Selected choice is shown on the top of the menu.
6.
AWT UI ElementsCont... ● Canvas - A Canvas control represents a rectangular area where application can draw something or can receive inputs created by user. ● Image - An Image control is superclass for all image classes representing graphical images. ● Scroll Bar - A Scrollbar control represents a scroll bar component in order to enable user to select from range of values. ● Dialog - A Dialog control represents a top-level window with a title and a border used to take some form of input from the user. ● File Dialog - A FileDialog control represents a dialog window from which the user can select a file.
7.
Java 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 ● Background Events
8.
Types of Event ForegroundEvents - 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.
9.
Event Handling ● EventHandling 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.
10.
Event Handling ○ 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 and then returns. 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.
11.
Java Adapter Classes ●Java adapter classes provide the default implementation of listener interfaces. ● The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages. Working with Color, and Font. Working with Color ● Java supports color in a portable, device independent fashion ● The AWT color system allows you to specify any color you want ● Color is encapsulated by the Color class ● The most commonly used constructors are ■ Color(int red, int green, int blue) ■ Color(int rgbValue) ■ Color(float red, float green, float blue)
12.
Working with Color ●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 ● Example : 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 ● Example : 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
13.
Setting the CurrentGraphics Color ● By default, graphics objects are drawn in the current foreground color ● We can change this color by calling the Graphics method setColor( ) – void setColor(Color newColor); ● Here, newColor specifies the new drawing color ● We can obtain the current color by calling getColor( ) – Color getColor( ); Working with Fonts ● The AWT supports multiple type fonts ● Fonts are encapsulated by the Font class
14.
Creating and Selectinga Font ● To select a new font, you must first construct a Font object that describes that font ● One Font constructor has this general form – Font(String fontName, int fontStyle, int pointSize) ; ■ Here, fontName specifies the name of the desired font ● To use a font that you have created, you must select it using setFont( ), which is defined by Component ● It has this general form – void setFont(Font fontObj) ; ■ Here, fontObj is the object that contains the desired font