COM 3118
Dated: April 16, 2001
AWT
Abstract Window Toolkit
AWT contains numerous classes and methods that allow you to create and manage windows.
Main purpose to support applet windows, can also be used to create stand alone windows that run in a GUI environment such as Windows.
AWT Classes
AWTEvent
AWTEventMulticaster
BorderLayout
Button
Canvas
CardLayout
Checkbox
CheckboxGroup
CheckboxMenuItem
Choice
Color
Component
Container
Cursor
Dialog
Dimension
Event
EventQueue
FileDialog
FlowLayout
Font
FontMetrics
Frame
Graphics
GraphicsDevice
GraphicsEnvironment
GridBagConstraints
GridBagLayout
GridLayout
Image
Insets
Label
List
MediaTracker
Menu
MenuBar
MenuComponent
MenuItem
MenuShortcut
Panel
Point
Polygon
PopupMenu
PrintJob
Rectangle
Scrollbar
ScrollPane
SystemColor
TextArea
TextComponent
TextField
Toolkit
Window
A Class Hierarchy for Panel and Frame
The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level.
Component
At top of AWT hierarchy
Is an abstract class that encapsulates all attributes of a visual component.
All user interface elements displayed on the screen and interacting with the user are subclasses of Component
A Component object is responsible for remembering the current foreground and background colors and the currently selected text font.
Container
Subclass of Component
Additional methods that allow other Component objects to be nested in it.
Other Container objects can be stored inside a Container (since they are themselves instances of Component). This makes for a multi-level containment system.
Responsible for laying out(positioning) any component that it contains.
Panel
Concrete subclass of Container
Does not add any new methods, simply implements Container
May be thought of as a recursively nestable concrete screen component
Superclass for Applet
When screen output is directed to an Applet, it is drawn on a surface of a Panel object.
Window that does not contain a title bar, menu bar, or border.
Window
Window class creates a Top-level window.
Is not contained with in any other object.
Frame
Encapsulates what is commonly thought of as a "window".
Subclass of window,has a title bar, menu bar, borders and resizing corners.
If created within an Applet, contains waring message to user. "Warning: Applet Window"
Working with Graphics
***Remember: Graphics Context
public void paint(Graphics g){
g.drawLine(0,0,100,100);
}
Drawing Lines
void drawLine (int startX, int startY, int endX, int endY)
Drawing Rectangle
void drawRect (int top, int left, int width, int height)
void fillRect (int top, int left, int width, int height)
void drawRoundRect (int top, int left, int width, int height, int xDiam, int yDiam)
void fillRoundRect (int top, int left, int width, int height, int xDiam, int yDiam)
Drawing Circles and Ellipses
void drawOval (int top, int left, int width, int height)
void fillOval (int top, int left, int width, int height)
Drawing Arcs
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)
The arc is bounded by a rectangle ____________.
The arc is drawn from startAngle through the angular distance specified by sweepAngle.
Angles specified in degrees.
Zero Degrees in on the horizontal, as at 3 o’ clock.Arc drawn counterclockwise if sweepAngle is positive and clockwise if sweepAngle is negative.
Drawing Polygons
void drawPolygon (int x[], int y[], int numPoints)
void fillPolygon (int x[], int y[], int numPoints)
Sizing Graphics
Dimension getSize()
void setSize(Dimension d)
Working with Color
Java supports color in a portable, device-independent fashion.
Color (int red, int green, int blue)
Color (int rgbValue) //int rgbValue = (0xff000000 | (0xc0<<16) | (0x00 <<8) | 0x00);
Color (float red, float green, float blue)
Color Methods
Using Hue, Saturation, and Brightness
hue is wheel of color, value between 0.0 to 1.0
saturation is light pastels to intense hues, value between 0.0 to 1.0
brightness: 1 is bright white and 0 is black, value between 0.0 to 1.0
static int HSBtoRGB (float hue, float saturation, float brightness)
static float[] RGBtoHSB (int red, int green, int blue, float values[])
If values is not null, then this array is given the HSB values and returned. Otherwise, a new array is created and the HSB values are returned in it.
int getRed()
int getGreen()
int getBlue()
int getRGB()
Setting Color
void setColor(Color newColor) // in Graphics
Color getColor()
Setting Paint Mode
Determines how objects are drawn in a window. By default, new output to a window overwrites any preexisting contents.
void setXORMode (Color xorColor)
To return to overwrite mode,
void setPaintMode()
//piece of code
The cross hairs are XORed onto the window and are always visible, no matter what the underlying color is.
public void paint (Graphics g) {
g. drawLine ( 0,0,100,100);
g. drawLine (0,100,100,0);
g.setColor (Color.blue);
g.drawLine (40,25,250,180);
g.drawLine (75,90,400,400);
g.setColor (Color.green);
g.drawRect (10,10,60,50);
g.fillRect (100,10,60,50);
g.setColor (Color.red);
g.drawRoundRect (190,10,60,50,15,15);
g.fillRoundRect (70,90,140,100,30,40);
g.setColor (Color.cyan);
g.drawLine(20,150,400,40);
g.drawLine(5,290,80,19);
//xor cross hairs
g.setXORMode(Color.black);
g.drawLine(90,100,110,100);
g.drawLine(100,90,100,110);
g.setPaintMode();
}
Working with Fonts
AWT supports multiple type fonts.
Beginning with Java 2, fonts have a family name, a logical font name, and a face name. The family name is the general name of the font such as Courier. The logical name specifies a category such as monospaced. The face name specifies a specific font such as Courier Italic.
Determining the available Fonts
Use the getAvailableFontFamilyNames() method defined by the GraphicsEnvironment class.
String[] getAvailableFontFamilyNames ()
Font[] getAllFonts ()
//piece of code
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String FontList [];
FontList = ge.getAvailableFontFamilyNames ();
Creating and Selecting a Font
Font(String fontName, int fontStyle, int pointSize)
FontName specifies name of desired Font, Logical or Face Name can be used
FontStyle specifies style of the Font like, Font.PLAIN, Font.BOLD, Font.ITALIC.
Combine styles like Font.BOLD|Font.ITALIC
Font getFont()
void setFont(Font fontObj)
eg. Font f = new Font ("Dialog", Font.PLAIN, 12);
setFont(f);