Sunday, September 22, 2013

Programming in Java: AWT classes 02

Working with Graphics, Colours and Fonts

Working with Graphics
The AWT supports a rich assortment of graphics methods. All graphics are drawn relative to a window. This can be the main window of an applet, a child window of an applet, or a stand-alone application window. The origin of each window is at the top-left corner and is (0,0). Coordinates are specified in pixels.
The Graphics class defines a number of drawing functions. Each shape can be drawn edge-only or filled. Objects are drawn and filled in the currently selected graphics colour, which is black by default. When a graphics object is drawn that exceeds the dimensions of the window, output is automatically clipped. Let’s take a look at several of the drawing methods.

Drawing Lines
Lines are drawn by means of the drawLine() method, shown here:
void drawLine(int x1, int y1, int x2, int y2)
It displays a line in the current drawing colour that begins at (x1, y1) and ends at (x2, y2).

Drawing Rectangles
The drawRect() and fillRect() methods display an outlined and filled rectangle, respectively. The signature is:
void drawRect(int top, int left, int width, int height)
void fillRect(int top, int left, int width, int height)
The upper-left corner of the rectangle is at top, left. The dimensions of the rectangle are specified by width and height.
To draw a rounded rectangle, use drawRoundRect() or fillRoundRect(), The signature is:
void drawRoundRect(int top, int left, int width, int height, int xArc, int yArc)
void fillRoundRect(int top, int left, int width, int height, int xArc, int yArc)
A rounded rectangle has rounded corners. The upper-left corner of the rectangle is at top, left. The dimensions of the rectangle are specified by width and height. The diameter of the rounding arc along the X axis is specified by xArc. The diameter of the rounding arc along the Y axis is specified by yArc.

Drawing Ellipses and Circles
To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ). The signature is:
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)
The ellipse is drawn within a bounding rectangle whose upper-left 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
Arcs can be drawn with drawArc( ) and fillArc( ) methods. The signature is:
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 the rectangle whose upper-left corner is specified by top, left and whose width and height are specified by width and height. 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 the three o’clock position. The arc is drawn counter-clockwise if sweepAngle is positive, and clockwise if sweepAngle is negative. Therefore, to draw an arc from twelve o’clock to six o’clock, the start angle would be 90 and the sweep angle 180.

Drawing Polygons
It is possible to draw arbitrarily shaped figures using drawPolygon( ) and fillPolygon( ). The signature is:
void drawPolygon(int x[ ], int y[ ], int numPoints)
void fillPolygon(int x[ ], int y[ ], int numPoints)

The polygon’s endpoints are specified by the coordinate pairs contained within the x and y arrays. The number of points defined by x and y is specified by numPoints.

Working with colour
Java supports colour in a portable, device-independent fashion. The AWT colour system allows you to specify any colour you want. It then finds the best match for that colour, given the limits of the display hardware currently executing your program or applet. Colour is encapsulated by the Color class.
You can also create your own colours, using one of the colour constructors. Three commonly used forms are:
Color(int red, int green, int blue)
Color(int rgbValue)
Color(float red, float green, float blue)
The first constructor takes three integers that specify the colour as a mix of red, green, and blue. These values must be between 0 and 255
The second colour 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.
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.
Once you have created a colour, you can use it to set the foreground and/or background colour by using the setForeground( ) and setBackground( ) methods.

Color Constatnts
The class Color defines the constants shown here that can be used to specify colours:

Color.black
Color.magenta
Color.blue
Color.orange
Color.cyan
Color.pink
Color.darkGray
Color.red
Color.gray
Color.white
Color.green
Color.yellow
Color.lightGray



Color Methods
You can obtain the red, green, and blue components of a colour independently using the methods shown below:
int getRed( )
int getGreen( )
int getBlue( )
Each of these methods returns the RGB colour component found in the invoking Color object in the lower 8 bits of an integer.
To obtain a packed, RGB representation of a colour, use getRGB( ), shown here:
int getRGB( )
The return value is organized as described earlier.

Setting the Current Graphics Color
By default, graphics objects are drawn in the current foreground colour. You can change this colour by calling the Graphics method setColor( ):
void setColor(Color newColor)
Here, newColor specifies the new drawing colour. You can obtain the current colour by calling getColor(), as shown:
Color getColor( )

Working with fonts
The AWT supports multiple type fonts. The AWT provides flexibility by abstracting font-manipulation operations and allowing for dynamic selection of fonts. Fonts have a family name and a face name. The family name is the general name of the font, such as Courier. The face name specifies a specific font, such as Courier Italic. Fonts are encapsulated by the Font class.

Font variables
The Font class defines the following variables:
String name: Name of the font
float pointSize: Size of the font in points
int size: Size of the font in points
int style: Font style

Font methods
The most commonly used methods defined by Font are listed below:
Method
Description
static Font decode(String str)
Returns a font given its name.
boolean equals(Object FontObj)
Returns true if the invoking object contains the same font as that specified by FontObj. Otherwise, it returns false.
String getFamily( )
Returns the name of the font family to which the invoking font belongs.
String getFontName()
Returns the face name of the invoking font.
int getSize( )
Returns the size, in points, of the invoking font.
int getStyle( )
Returns the style values of the invoking font.
boolean isBold( )
Returns true if the font includes the BOLD style value. Otherwise, false is returned.
boolean isItalic( )
Returns true if the font includes the ITALIC style value. Otherwise, false is returned.
boolean isPlain( )
Returns true if the font includes the PLAIN style value. Otherwise, false is returned.
String toString( )
Returns the string equivalent of the invoking font.

Determining the Available Fonts
When working with fonts, often we need to know which fonts are available on your machine. To obtain this information, we can use the getAvailableFontFamilyNames( ) method defined by the GraphicsEnvironment class as shown here:
String[ ] getAvailableFontFamilyNames( )
This method returns an array of strings that contains the names of the available font families. In addition, the getAllFonts( ) method is defined by the GraphicsEnvironment class as shown:
Font[ ] getAllFonts( )
This method returns an array of Font objects for all of the available fonts. Since these methods are members of GraphicsEnvironment, we need a GraphicsEnvironment reference to call them. We can obtain this reference by using the getLocalGraphicsEnvironment() static method, which is defined by GraphicsEnvironment as shown:
static GraphicsEnvironment getLocalGraphicsEnvironment( )

Creating and Selecting a Font
To select a new font, we 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. The name can be specified using either the logical or face name. All Java environments will support the following fonts: Dialog, DialogInput, Sans Serif, Serif, and Monospaced. Dialog is the font used by your system’s dialog boxes. Dialog is also the default if we don’t explicitly set a font. We can also use any other fonts supported by your particular environment. The style of the font is specified by fontStyle. It may consist of one or more of these three constants: Font.PLAIN, Font.BOLD, and Font.ITALIC.
To combine styles, OR them together. For example, Font.BOLD | Font.ITALIC specifies a bold, italics style.
The size, in points, of the font is specified by pointSize.
To use a font that we have created, we must select it using, which is defined by Component. as:
void setFont(Font fontObj)
Here, fontObj is the object that contains the desired font.

Obtaining Font Information
Suppose we want to obtain information about the currently selected font. To do this, we must first get the current font by calling getFont( ). This method is defined by the Graphics class, as shown:
Font getFont( )

Once we have obtained the currently selected font, we can retrieve information about it using various methods defined by Font.

No comments:

Post a Comment