Presentation is loading. Please wait.

Presentation is loading. Please wait.

AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.

Similar presentations


Presentation on theme: "AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e."— Presentation transcript:

1 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components uses the resources of system. The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc. AWT Classes

2 Windows Fundamentals 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. Much of the functionality of these windows is derived from their parent classes. Thus, a description of the class hierarchies relating to these two classes is fundamental to their understanding.

3 The class hierarchy for Panel and Frame

4 Component Component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. For examples buttons, checkboxes, list and scrollbars of a graphical user interface. Container Container object is a component that can contain other components. Components added to a container are tracked in a list. The order of the list will define the components' front-to- back stacking order within the container. If no index is specified when adding a component to a container, it will be added to the end of the list. Panel Panel provides space in which an application can attach any other components, including other panels.

5 Window Window is a rectangular area which is displayed on the screen. In different window we can execute different program and display different data. Window provide us with multitasking environment. A window must have either a frame, dialog, or another window defined as its owner when it's constructed. Frame A Frame is a top-level window with a title and a border. The size of the frame includes any area designated for the border. Frame encapsulates window. It and has a title bar, menu bar, borders, and resizing corners. Canvas Canvas component represents a blank rectangular area of the screen onto which the application can draw. Application can also trap input events from the use from that blank area of Canvas component.

6 Working with Frame Windows Two of Frame’s constructors: –Frame( ) –Frame(String title) The first form creates a standard window that does not contain a title. The second form creates a window with the title specified by title. Setting the Window’s Dimensions void setSize(int newWidth, int newHeight) void setSize(Dimension newSize) Dimension getSize( ) - obtain the current size of a window

7 Hiding and Showing a Window After a frame window has been created, it will not be visible until setVisible( ) is called. –void setVisible(boolean visibleFlag) Setting a Window’s Title –void setTitle(String newTitle) Closing a Frame Window –setVisible(false) –windowClosing( )method of the WindowListener interface Examples: AppletFrame.java WindowEvents.java

8 Working with Graphics 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 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 color, which is black by default. When a graphics object is drawn that exceeds the dimensions of the window, output is automatically clipped.

9 Drawing Lines void drawLine(int startX, int startY, int endX, int endY) Eg: Lines.java Drawing Rectangles 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 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) Drawing Polygons void drawPolygon(intx[ ], inty[ ], int numPoints) void fillPolygon(intx[ ], inty[ ], int numPoints) Eg: HourGlass.java

10 Sizing Graphics getSize( ) - returns the dimensions of the window encapsulated within a Dimension object. Eg. ResizeMe.java

11 Working with Colors - portable, device-independent fashion eg: ColorDemo.java Color defines several constants to specify a number of common colors. Own color creation. Three commonly used forms are shown here: –Color(int red, int green, int blue) –Color(int rgbValue) –Color(float red, float green, float blue) –Eg: new Color(255, 100, 100); Color Methods –Using Hue, Saturation, and Brightness static int HSBtoRGB(float hue, float saturation, float brightness) static float[ ] RGBtoHSB(int red, int green, int blue, float values[ ]) int getRed( ) int getGreen( ) int getBlue( ) - returns the RGB color component found in the invoking Color object in the lower 8 bits of an integer. int getRGB( ) Setting the Current Graphics Color –void setColor(Color newColor) –Color getColor( )

12 Setting the Paint Mode eg: XOR.java The paint mode determines how objects are drawn in a window. New output to a window overwrites any preexisting contents. void setXORMode(Color xorColor) The advantage of XOR mode is that the new object is always guaranteed to be visible no matter what color the object is drawn over. To return to overwrite mode, call setPaintMode(),void setPaintMode( )

13 Working with Fonts The AWT provides flexibility by –abstracting font-manipulation operations and –allowing for dynamic selection of 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.

14 Determining the Available Fonts eg: ShowFonts.java –String[ ] getAvailableFontFamilyNames( ) –Font[ ] getAllFonts( ) –static GraphicsEnvironment getLocalGraphicsEnvironment( ) Creating and Selecting a Font eg SampleFonts.java –Font(String fontName, int fontStyle, int pointSize) Font style: Font.PLAIN,Font.BOLD, andFont.ITALIC. –void setFont(Font fontObj) Obtaining Font Information eg: FontInfo.java –Font getFont( )

15 Managing Text output using FontMetrics FontMetrics defines several methods that help you manage text output. –Displaying Multiple Lines of Text –Centering Text –Multiline Text Alignment


Download ppt "AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e."

Similar presentations


Ads by Google