Professional Documents
Culture Documents
AWT Classes Window Fundamentals Working with Frame Windows Creating a Frame Window in an Applet Creating a Windowed Program Displaying Information within a Window Working with Graphics Working with color Setting the Paint Mode Working with Fonts Managing Text Output Using Font Metrics.
AWT Classes
one of Javas largest packages
AWT Classes
AWT Abstract Window Toolkit The main purpose of the AWT is to support applet windows It can also be used to create stand-alone windows that run in a GUI environment, such as Windows.
The AWT classes are contained in the java.awt package
AWT Classes -
some of the original methods were deprecated and replaced by new ones when Java 1.1 was released. For backwardcompatibility, Java 2 still supports all the original 1.0 methods.
AWT Classes
AWT Classes
AWT Classes
AWT Classes
Window Fundamentals
The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level. The two most common windows are
Window Fundamentals
Much of the functionality of these windows is derived from their parent classes.
- Component
An abstract class that encapsulates all of the attributes of a visual component.(All user interface elements are subclasses) It defines over a hundred public methods for
- Container
A subclass of Component
- Panel A Panel is a window that does not contain a title bar, menu bar, or border. A concrete subclass of Container The superclass for Applet It doesnt add any new methods; it simply implements Container.
- Panel
Other components can be added to a Panel
object by its add( ) method. add( ) method inherited from Container. you can position and resize the added components manually using setLocation( ), setSize( ), or setBounds( ) methods defined by Component.
- Window
This class creates a top-level window
- Frame
A subclass of Window and has a title bar, menu bar, borders, and resizing corners. If you create a Frame object from with in an applet window , it will contain a warning message, such as Java Applet Window, to the user that an applet window has been created.
You will use it to create child windows within applets, top-level or child windows for applications. Two of Frames constructors: 1. Frame( ) creates a standard window that does not contain a title. 2. Frame(String title) creates a window with the title specified by title
fields
setVisible(false).
you must implement the windowClosing( ) method of the WindowListener interface Inside windowClosing( ), you must remove the window from the screen
Sample output
The following program creates a window that responds to mouse events. The main applet window also responds to mouse events.
Sample output
It is possible to create stand-alone AWT-based applications, too. To do this, simply create an instance of the window or windows you need inside main( ).
Sample output
Drawing Lines
void drawLine(int startX, int startY, int endX, int endY)
-
Drawing Lines
Drawing Rectangles
1. void drawRect(int top, int left, int width, int height) 2. void fillRect(int top, int left, int width, int height) top ,left
The upper-left corner of the rectangle
width and height The dimensions of the rectangle 3. void drawRoundRect(int top, int left, int width, int height, int xDiam, int yDiam)
xDiam The diameter of the rounding arc along the X axis yDiam The diameter of the rounding arc along the Y axis.
Drawing Rectangles
4. void fillRoundRect(int top, int left, int width, int height, int xDiam, int yDiam) draws a filled rectangle
Drawing Rectangles
Sample output
Sample output
corner is specified by top, left and whose width and height are
specified by width and height To draw a circle, specify a square as the bounding rectangle
Drawing Arcs
1. void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
2. void fillArc(int top, int left, int width, int height, int
startAngle, int sweepAngle) top,left upper-left corner of the bounding rectangle width and height width and height of the bounding rectangle
Drawing Arcs
The arc is drawn counterclockwise if sweepAngle is positive, and clockwise if sweepAngle is negative.
To draw an arc from twelve oclock to six oclock, the start angle
Drawing Arcs
Drawing Arcs
Sample output
Drawing Polygons
1. void drawPolygon(int x[ ], int y[ ], int numPoints) 2. void fillPolygon(int x[ ], int y[ ], int numPoints)
Drawing Polygons
Drawing Polygons
Sample output
Sizing Graphics
To size a graphics object first obtain the current dimensions of the window by calling getSize( ) on the window object Once you have the current size of the window, you can scale your graphical output accordingly. getSize( ) returns the dimensions of the window encapsulated within a Dimension object
Sizing Graphics
Sizing Graphics
Sizing Graphics
You can also create your own colors, using one of the color constructors. 1. 2. 3. Color(int red, int green, int blue) Color(int rgbValue) Color(float red, float green, float blue)
0 and 255
2. The second color constructor takes a single integer that contains the mix of red, green, and blue packed into an integer red in bits 16 to 23, green in bits 8 to 15, and blue in bits 0 to 7 3. The final constructor, takes three float values (between 0.0 and 1.0) that specify the relative mix of red, green, and blue
Color Methods
The Color class defines several methods that help manipulate colors.
1. static int HSBtoRGB(float hue, float saturation, float brightness)
2. static float[ ] RGBtoHSB(int red, int green, int blue, float values[ ])
xorColor specifies the color that will be XORed to the window when an object is drawn.
the following program displays cross hairs that track the mouse pointer. The cross hairs are XORed onto the window and are always visible, no matter what the underlying color is.
Sample output
Fonts have 1. A family name the general name of the font, such as Courier 2. A logical font name specifies a category of font such as
Monospaced
3. A face name a specific font, such as Courier Italic.
GraphicsEnvironment class
String[ ] getAvailableFontFamilyNames( );
Font[ ] getAllFonts( )
This method returns an array of Font objects for all of the available fonts. Since these methods are members of GraphicsEnvironment, you need a GraphicsEnvironment reference to call them. You can
Sample output
Font getFont( )
Once you have obtained the currently selected font, you can retrieve information about it using various methods defined by Font.
this applet displays the name, family, size, and style of the currently selected font:
FontMetrics class
Encapsulates various information about a font.
Basic Terms :
When drawString( ) method is used , the location specifies left edge of the baseline of the characters, not at the upper-left corner as is usual with other drawing methods
FontMetrics class
FontMetrics defines several methods that help you manage text output.
FontMetrics class
FontMetrics defines several methods that help you manage text output.
FontMetrics class
FontMetrics defines several methods that help you manage text output.
Centering Text
Centering Text
Centering Text
Centering Text