AWT(abstract window toolkit)

·         The AWT contains numerous classes and methods that allow us to create and manage windows

·         AWT classes are mainly used in applets for creating stand-alone windows that run in a GUI environment

·         The AWT classes are contained in the java.awt package.

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 those derived from
    •  Panel, which is used by applets,
    •  and those derived from Frame, which creates a standard application window.
  • Component class: is At the top of the AWT hierarchy
    • is an abstract class that encapsulates all of the attributes of a visual component.
    • All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component.
    • It defines over a hundred public methods that are responsible for managing events, such as mouse and keyboard input, positioning and sizing the window, and repainting.

 

  • Container class: is a subclass of Component class
    • It has additional methods that allow other Component objects to be nested within it
    • A container is responsible for laying out any components that it contains
    • It does this through the use of various layout managers

The class hierarchy










Panel

·         The Panel class is a concrete subclass of Container

·         It doesn’t add any new methods;

·         it simply implements Container

·         Panel is the superclass for Applet.

·         When screen output is directed to an applet, it is drawn on the surface of a Panel object.

·         A Panel is a window that does not contain a title bar, menu bar, or border

Window

·         The Window class creates a top-level window

·         A top-level window is not contained within any other object; it sits directly on the desktop

·         Generally, we won’t create Window objects directly. Instead, we will use a subclass of Window called Frame

 

Frame

·         Frame encapsulates what is commonly thought of as a “window.”

·         It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners

·         When a Frame window is created by a stand-alone application rather than an applet, a normal window is created

 

Canvas

·         it is not part of the hierarchy for applet or frame windows

·         Canvas encapsulates a blank window upon which you can draw.

   

Working with Graphics

·         The Graphics class defines a number of drawing functions

·         A graphics context is encapsulated by the Graphics class and is obtained in two 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

 

Drawing Lines

Lines are drawn by means of the drawLine( ) method

Syntax:

void drawLine(int startX, int startY, int endX, int endY)

 program



Drawing Rectangles

The drawRect( ) -- outlined rectangle

void drawRect(int top, int left, int width, int height)

The fillRect( ) -- filled rectangle

void fillRect(int top, int left, int width, int height)

Note: The upper-left corner of the rectangle is at top,left.

 program



Drawing Ellipses and Circles

drawOval( )

void drawOval(int top, int left, int width, int height)

fillOval( )

void fillOval(int top, int left, int width, int height)

program




Drawing Polygons

drawPolygon( )

void drawPolygon(int x[ ], int y[ ], int numPoints)

fillPolygon( )

void fillPolygon(int x[ ], int y[ ], int numPoints)

Note: The polygon’s endpoints are specified by the coordinate pairs contained within the x and y arrays.


 program


 

 

 

Working with Color

Color is encapsulated by the Color class

3 commonly used forms

o   Color(int red, int green, int blue)

§  three integers that specify the color as a mix of red, green, and blue

§  Color(int rgbValue)

§  single integer that contains the mix of red, green, and blue packed into an integer.

o   Color(float red, float green, float blue)

§  takes three float values (between 0.0 and 1.0) that specify the relative mix of red, green, and blue.

Color Methods

      void setColor(Color newColor)

      Color getColor( )

A Color Demonstration Applet

import java.awt.*;

import java.applet.*;

/*< applet code="ColorDemo" width=300 height=200

</applet>*/

public class ColorDemo extends Applet {

// draw lines

 public void paint(Graphics g) {

Color c1 = new Color(255, 100, 100);

Color c2 = new Color(100, 255, 100);

Color c3 = new Color(100, 100, 255);

 g.setColor(c1);

g.drawLine(0, 0, 100, 100);

 g.drawLine(0, 100, 100, 0);

g.setColor(c2);

 g.drawLine(40, 25, 250, 180);

g.drawLine(75, 90, 400, 400);

g.setColor(c3);

g.drawLine(20, 150, 400, 40);

g.drawLine(5, 290, 80, 19);

 g.setColor(Color.red);

g.drawOval(10, 10, 50, 50);

 g.fillOval(70, 90, 140, 100

g.setColor(Color.blue);

g.drawOval(190, 10, 90, 30);

g.drawRect(10, 10, 60, 50);

 g.setColor(Color.cyan);

g.fillRect(100, 10, 60, 50);

g.drawRoundRect(190, 10, 60, 50, 15, 15);

}

 }

Working with Fonts

      The AWT supports multiple type fonts

      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 of font, such as Monospaced.

      The face name specifies a specific font, such as Courier Italic.

      Fonts are encapsulated by the Font class.

      Creating and Selecting a Font

      Font(String fontName, int fontStyle, int pointSize)

      fontName specifies the name of the desired font

      The style of the font is specified by fontStyle: Font.PLAIN, Font.BOLD, and Font.ITALIC.

      The size, in points, of the font is specified by pointSize.

      To use a font that you have created, you must select it using setFont( ):void setFont(Font fontObj)


No comments:

Post a Comment