Java AWT

Part of the java API that declares graphical components.
Main Classes The three main classes are:ComponentContainers

Layout Managers

java.awt packages As of JDK 1.2 there are 11 packages supporting the java.awt package.
java.awt The original classes supporting the GUI interfaces.
java.awt.color Advanced color manipulation classes
java.awt.datatransfer Facilitate cut-and-paste operation with native applications
java.awt.dnd Facilate drag-and-drop operations
java.awt.event Classes that encapsulate and process events
java.awt.font Facilate font manipulation
java.awt.geom Profices class for 2D manipulation
java.awt.im Contains the InputContext class, which manages communication between user input event and text editing components
java.awt.Image Image manipulation classes
java.awt.rendeable Allows for image rendering
java.awt.print Support classes which support flexible printing from java applications.
Components It is the base class of all compoments:java.awt.Component 

The subclasses of the java.awt.Component make up the visual components of Java.

java.awt.Component has method for components, handling events and rendering component.
Components render graphical widgets. It is an abstract class.
They always occuply an rectangle on the screen.
As of Java 1.1 the delegation event model is used.There are three participant in this event mechanism:

  • The event source
  • The event object
  • The event listener
Steps for the delegation event model:
  1. Decide on the Event xxxx (Ex: xxxx = Mouse event)
  2. Implement its listener: xxxxListener (MouseListener)

listenermethod(xxxEvent)

Ex: MousePressed( MouseEvent )

  1. Register the event: addxxxListener( listener )

Ex: source.addMouseListener(listener)

The event source keeps a list of the events to be notified the event type occurs. The addxxxListener method adds an instance of the listener.
Exam Tip Know the various methods of the listener interfaces.
Subclasses of Component The basic classes which extend the Component class are as follows:
ButtonCanvasCheckbox

Choice

ContainerLabelList

Scrollbar

TextComponent

TextComponent extend to create the TextField and TextArea components.
setEditable(boolean) It allows or prevents user editing of the displayed text for TextField and TextArea.
Interfaces of Components ImageObserver Provides for notifying a component about changes in Image information.
MenuContainer Provide methods used to coordinate menu-related classes
Serializable Tag for saving files to an object output streams.
Visual Setting Each component can set its foreground or background color. If it does not, it inherits the setting of the parent container.
public void setForeground(Color c)
public void setBackground(Color c)
Exam Tip Know the color setting methods and procedure
Font of a component can be set using the:java.awt.Font class.
public void setFont( Font f) new Fon(“SanSerif”, Font.Plain, 14)
Setting and getting the size of a component.
public void setSize(Dimention d)public void setSize( int x, int Y)public Dimension getSize() Use the java.awt.Dimension class or the x or y coordinate of the object.
setSize() sizes the component.
The location of a component is relative to the upper-left corner of the component. Setting a component location:
public void setLocation(Point p)public void setLocation(int x, int y)public Point getLocation() Use the java.awt.Point class to get, set a point.
setLocation() method moves the component.
Getting the complete geometry of the component.
public void setBounds(Rectangle e)public void setBounds(int x, int y, int h, int w)public Rectangle getBounds() Use the java.awt.Rectangle class.
boolean contains(int x, int y) Test is a point is inside the component.
setBounds() method sets the location of the upper-left corner of the component and its width and height in pixels.
Exam Tip Screen position is measured in pixels from upper-left corner of the container x increases to the left. y increases downwards.
Exam Tip Know the alternate data type these size and location methods used.
Exam Tio It is up to the layout manager to pay attention to these geometric settings. setSize() and setBounds() may not be obeyed.
Rendering Each component contain methods for rendering itself.
public void paint(Graphics g) paint(Graphics g) is the most common rendering method. Here is where the graphic definition, coding is placed,
public void update(Graphics g) Call automatically when repaint() method is called.It can be overriding to call paint) to stop flickering.
public void repaint() Called by a program instead of paint() when screen painting is desired.
Exam Tip update() is overriding to call paint()
The sequence or rendering are:repaint() callsupdate() calls

paint()

Using components A components is instantiated.Modify their characteristicsAdd them to a container.
The layout manager will size and position that component within itself.
Containers Used to contain other components.Each container will utilize a layout manager to size and position those components.
They are specialized components use to hold other components.
The method that containers adds to component generally have to do with add, removing, positioning, and location components.
The container maintains a list of Component references.
Use the add method to add components.The add method is overloaded.
add(Component component) Component is added to the end of the list and component is returned.
add(Component component, int index) Add a component at the index position.
add(String name, Component component) Component is added to the end of the list and the name is used as a constraint if necessary.Don’t use this.
add(Component component, Object constraints) Add component with constraint object.
add(Component component, Object constraints, int index) Add component with constraint object at a specified index.
Dimension getSize() The size of a container is returned as a Dimension. It reflect the amount of space the container occupies.Not the space available to the container.
setLayout(LayoutManage m) Sets the layout manager for the container.
remove(Component c) Removes this component from the list.
Exam Tip Know these add() signatures
Setting the layout manager within a container is done by using:
setLayout( LayoutManager)
Exam Tip setEnabled(true) enable the component and allows respond to user input. false disables the component.
Exam Tip setVisible(true) must be used to make all the components visible in a container. false does not display the component.
Container Subclasses Types of containers:
java.awt.Panel Used by applets.Default layout manager is the FlowLayout.
java.awt.ScrollPane Provide scroll bar for the viewable area. Help window like presentation. Uses BorderLayout().
java.awt.Window Has decorations such as borders or menu bars. It is a top-level container. Default layout manager is the BorderLayout.
Although the window is the parent class of a frame, it must be constructed from a Frame or Window.
Window(Frame)Window(Window)
java.awt.Dialog Is a subclass of a java.awt.Window.Default layout manager is the BorderLayout.
One standard dialog is used FileDialog. A file selection dialog which is modal.
Other dialogs can be created by extending Dialog.
Dialog(Frame)Dialog(Dialog)
The dialog constructor takes a frame or another dialog object.
java.awt.Frame Is a subclass of a java.awt.Window.Has decorative borders, title and menu bars. Default layoutmanger is the BorderLayout. It is a top-level container.
The frame is set to the upper-left corner of the screen.
Frame()Frame(String title)
java.awt.MenuContainer It is an interface. The java.awt classes which implement the MenuContainer are Container, Frame, Menu and Menubar.
Classes that create menu do not extend the Component class. They implement the interface. Classes like MenuBar, Menu.
Frame, Window and Dialog boxes can support menus.
To create traditional menus the java.awt.MenuBar and java.awt.MenuItem. java.awt.Menu class must be used.
java.awt.MenuBar Organizes the Menu object to create familiar drop-down menu.
java.awt.Menu A Menu creates a single drop-down menu. Objects in the menu can only be MenuItem or ChecboxMenuItem.
java.awt.MenuItem These are the items in the selected Menu
java.awt.MenuShortcut MenuShortcut object represent keyboard alternatives of a MenuItem.
java.awt.PopUpMenu A PopUpMenu can be attached to any screen component.
Exam Tip setBounds() is required for a frame or the window will not be shown.
Exam Tip The Window and Frame are top-level containers
Container-Content Relationship There is a relationship between the container and its content. A sort of hierarchy relation exist.
Suppose a Button is in a Panel.Container c = myButton.getParent();
The reference Panel is returned, not the Button class. Panels owns the Button.0
Layout Managers Layout Managers are responsible for positioning and sizing components within a container.
The parent container layout manager determines where components gets positioned within that container or they obey their preferred sizes.
Layout Manager Interface A class wishing to perform the role of a layout manager must implement either:java.awt.LayoutManagerjava.awt.LayoutManager2
User do not usual call the interface methods directly, the container to which the LayoutManager is attached does this.
Methods of LayoutManager interface
addLayoutComponent(String name, Component c) Used by layout managers using name to determine the section of the container the component will be placed.
removeComponent(Component c) Remove a component from the container.
preferredLayoutSize(Container c) This method provides dimension necessary to render a given container. It is up to the layout manager to determine how to calculate the dimensions.
Dimension minimumLayoutSize(Container parent) Returns the minimum size necessary to render the container.
layoutContainer(Container c) This method is used to implement the layout manager class. It manages size, positioning of each components.
Methods of LayoutManager2 interface mainly add contraints to the layout manager.
addLayoutComponent( Component c, Object constraints) Adds component to the container using a constraints.
maximumLayoutSize(Container c) Set the maximum size for the container
getLayoutAlignmentX(Container c) Returns the horizontal alignment of the component. Ranges are 0.0 to 1.0. 0.0 means more to the left. .5 means centered.
` getLayoutAlignmentY(Container c) Returns the vertical alignment of the component.Ranges are 0.0 to 1.0. 0.0 means more to the top. .5 means centered.
invalidateLayout(Container c) invalidates the current layout manager.
Types of Layout Manager
GridLayout Each cell in the grid is of equal dimension. Each component is added from top-left and down each row.
Exam Tip The number of columns added are calculated by the number of rows and the number of components. Not by using the constructor , setRows() to setColumns()
new GridLayout() Create a 1 row grid with cells equally divided among all components. Gaps are zero.
new GridLayout(int rows, int cols) Create a grid of rows by cols.Gaps are zero.
new GridLayout(int rows, int cols, int hgap, int vgap) Create a grid of rows by cols.Gaps are specified between compoments. Not the components and container border.
To control the container gap use getInset() method.
Exam Tip If you add more components than the specified cell, additional columns are automatically computed.
Exam Tip When there are fewer components for the cells, the unoccupied cell are painted in the containers color.
Exam Tip Components do not keep their preferred size (height or width) for GridLayout.
FlowLayout Arranges components from left to right.When container runs out of room on the current row, it goes to the next row.
Components can be aligned using the FlowLayout constants (LEFT, CENTER, RIGHT).
new FlowLayout()new FlowLayout( FlowLayout.LEFT)

 

new FlowLayout(FlowLayout.RIGHT, 10, 2)

Centers components with a default gap of 5 pixels.Aligns component to the left with a default gap of 5 pixels.

 

Aligns component to the right with a default horizontal gap of 10 and vertical gap of 2.

Exam Tip Components keep their preferred size for FlowLayout.
BorderLayout java.awt.BorderLayout implements the LayoutManager2 interface.
Container is dividedd in five areas: NORTH, SOUTH, EAST, WEST, CENTER
North and South components of the container are resized to the entire width of the container.
Exam Tip Components keep their preferred height for North and South areas.
Exam Tip If a constraint is not given the CENTER constraint is assumed.
East and West components of the container are resized to the entire height of the container.
Exam Tip Components keep their preferred width for East and West areas.
Any component within the CENTER section resize to fill whatever space is left.
Exam Tip Components don’t keep their preferred width or height.
GridBagLayout Use contraints to set up the components. To difficult to use.
CardLayout java.awt.CardLayout implements the LayoutManager2 interface.
Each components are added as a stack of cards.
Whichever was added first will be visible first when container is displayed. (FIFO)
The program switches the view between components either by addressing it by name or by methods that file through the cards.
new CardLayout() Creates no border around compoment.
new CardLayout(int hborder, int vborder) Creates a gap in the background color around the component.
No LayoutManagersetLayout(null) When a layout manager is set to null, the absolute positioning is used to set layout the component.
Eash component will be sized and positioned according to its own parameters using the setBounds() method.
absolute positioning Absolute positioning means that each component is placed and sized by the coordinates defined by getBounds() method.
Window Classes Window class extends the Container class.
java.awt Geometric Utilities All these classes allow 2 dimensional or more shapes. They all take int parameters.
Dimension Dimension object encapsulate the width and height of a component.
Point Point encapsulate the x and y coordinate of a point in 2D space.
Rectangle Rectangle encapsulate the x and y for the upper-left corner plus the width and height of a component.
Polygon Polygon represent arbitrary number of lines connection of points established by an array for the x an y points. The first and final points in the list are joined to complete the polygon.
java.awt.Graphics. The base class for all graphics context that allow a program to draw on the screen. Know these methods of the Graphics class.
drawString(str, x, y) Draws a string on the screen.The string has these qualities:
ascent The top of the string
baseline the bottom of the string
descent Any part of the string below the baseline.
drawLine(x1,y1,x2,y2) Draws a line pixel wide from point 1 to 2.
drawRec(x,y,width,height) Draws a rectangle based on the upper-left corner at point(x,y) with the width and height specified.
fillRec(x,y,width,height) Draws a filled rectangle based on the upper-left corner at point(x,y) with the width and height specified.
drawOval(x, y, width, height) Draws a single pixel wide line to create an oval that just fits the rectangle specified.
fillOval(x, y, width, height) fill the oval that just fits the rectangle specified.
drawArc(x,y,wide,high,startAngle,endAngle) Draws an outline of an arc just fitting the rectangle described by the first four parameters. Start and end angle begin from 3 o’clock position, 0 degrees.
fillArc(x,y,wide,high,startAngle,endAngle) fills the arc just fitting the rectangle described by the first four parameters. Start and end angle begin from 3 o’clock position.
drawPoligon(Polygon) Draws a polygon.
fillPoligon(Polygon) Fills a polygon.
drawImage(Image, x, y, ImageObserver)drawImage(Image, x, y, width, height, Color bkg, ImageObserver) Draws an image. ImageObserver tracks the image rendering.The second method bounds the image by a rectangle.
public Graphics getGraphics(); Returns a graphic object for any Component
The JVM will return null if no container is on the screen. getGraphics() will return null.
public void dispose() Disposes of the system resources if to many graphics objects are retrieved.