Module 6
Module 6
AWT Classes
• The AWT classes are contained in the java.awt package.
Window Fundamentals
• AWT defines windows according to a class hierarchy that adds
functionality & specificity in each level.
• The two most common windows are
• those derived from Panel
• those derived from Frame
• The following fig. shows the class hierarchy for Panel and Frame
Component
Container
Window Panel
Frame Applet
Component
• All user interface elements that are displayed on the screen and that
interact with the user are subclasses of Component.
• Responsible for managing events, such as mouse and keyboard input,
positioning and sizing the window, and repainting.
Container
• subclass of Component
• Responsible for laying out(i.e. positioning) any components that it
contain.
• It does this through the use of various layout managers.
Panel
• Concrete subclass of Container.
• No new methods; it simply implements Container.
• Panel is the superclass for Applet.
• Panel is a window that does not contain a title bar, menu bar or
border. (AppletViewer provides this; browser won’t)
• Components can be added to a Panel object by its add( ) method.
• Once added, they can be positioned and resized manually using the
setLocation( ), setSize( ), or setBounds( ) methods defined by
Component.
Window
• creates top-level window.
• Window objects generally are not created directly; instead we will use a
subclass of Window called Frame.
Frame
• subclass of Window and has a title bar, menu bar, border and resizing corners.
• Frame object can be created from within an applet window
• it will contain a message “Java Applet Window”
• When a Frame window is created by a program ; a normal window is created.
Canvas
• not part of hierarchy.
• Canvas encapsulates a blank window upon which you can draw.
Working with Frame Windows
Frame’s Constructors
• Frame( )
• creates a standard window that does not contain a title.
• Frame(String title)
• creates a window with the title specified by title.
Setting the Window’s Dimensions
• void setSize(int newWidth, int newHeight)
• void setSize(Dimension newSize)
• set the dimensions of the window
• Dimension getSize( )
• obtain the current size of a window
Hiding and Showing a Window
• After a frame window has been created, it will not be visible until you
call setVisible( )
void setVisible(boolean visibleFlag)
-> visible if true otherwise hidden
Setting a Window’s Title
• void setTitle(String newTitle)
Closing a Frame Window
• Remove window from the screen by calling setVisible(false), when it is
closed.
• If Frame window is a child window, to intercept window-close event,
• implement windowClosing( ) method of the WindowListener interface.
• Inside windowClosing( ), remove the window from the screen.
import java.awt.Frame;
public class SampleFrame extends Frame {
SampleFrame(String title) {
super( );
this.setTitle(title);
this.setSize(300,200);
this.setVisible(true);
}
public static void main(String args[ ]) {
SampleFrame sf = new SampleFrame(“My Window”);
}
}
Displaying Information within a Window
Working with Graphics
• All graphics are drawn relative to a window.
• The origin of each window is at the top-left corner and is 0,0
• Coordinates are specified in pixels.
• All output to a window takes place through a graphics context.
• A graphics context is encapsulated by the Graphics class and is
obtained in 2 ways
• It is passed to an applet when one of its various methods, such as paint( ) or
update( ) is called.
• It is returned by the getGraphics( ) method of Component.
• The Graphics class defines a number of drawing functions.
Drawing Lines
• Lines are drawn by means of the drawLine( ) method.
void drawLine(int startX, int startY, int endX, int endY)
import java.awt.*;
import java.applet.*;
/*
<applet code=“Lines.class”width=300 height=200>
</applet>
*/
public class Lines extends Applet {
public void paint(Graphics g) {
g.drawLine(0,0,100,100);
g.drawLine(40,25,250,180);
}
}
Drawing Rectangles
• void drawRect(int top, int left, int width, int height)
• void fillRect(int top, int left, int width, int height)
• top, left -> upper-left corner of the rectangle
• width, height -> dimensions of the rectangle
Rounded Rectangles
• 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)
eg) g.drawRect(10,10,60,50);
g.fillRect(10,10,60,50);
g.drawRoundRect(10,10,60,50,15,15);
Drawing Ellipses and Circles
• 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 drawn from startAngle through the angular distance specified by
sweepAngle.
• Angles are specified in degrees; zero degrees is on the horizontal, at 3’0 clock
position.
• The arc is drawn counter clockwise if sweepAngle is positive and clockwise if
sweepAngle is negative.
eg. g.drawArc(10,40,70,70,0,75)
g.fillArc(10,40,70,70,0,75)
Working with Color
• Color is encapsulated by the Color class.
• Color defines several constants to specify a number of common
colors.
• We can create our own colors, using one of the Color constructors.
Color( int red, int green, int blue)
Color( int rgbValue)
Color( float red, float green, float blue)
-> these values must be between 0 and 255.
eg. new Color(255, 100, 100); //light red
Color Methods
Using Hue, Saturation and Brightness
• The Hue-Saturation-Brightness(HSB) color model is an alternative to
Red-Green-Blue(RGB) for specifying particular colors.
• Hue
• is a wheel of Color
• specified with a number between 0.0 and 1.0
• colors approximately red, orange, yellow, green, blue, indigo and violet.
• Saturation
• scale ranging from 0.0 to 1.0 representing light pastels to intense hues.
• Brightness
• range from 0.0 to 1.0 where 1 is bright white and 0 is black.
• Color supplies two methods to convert between RGB and HSB
static int HSBtoRGB(float hue, float saturation, float brightness)
static float[ ] RGBtoHSB(int red, int green, int blue, float values[ ])
• RGBtoHSB( ) returns a float array of HSB values corresponding to RGB
integers
• the array contains the hue at index 0, saturation at index 1, and brightness at
index 2.
getRed( ), getGreen( ), getBlue( )
• To obtain the red, green and blue components of a color independently
Eg. int getRed( )
int getGreen( )
int getBlue( )
getRGB( )
• To obtain a packed RGB representation of a color.
eg. int getRGB( )