AWT CSI 3125 Preliminaries page 1 AWT Java

  • Slides: 25
Download presentation
AWT CSI 3125, Preliminaries, page 1

AWT CSI 3125, Preliminaries, page 1

AWT • Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or

AWT • Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. • Java AWT components are platformdependent i. e. components are displayed according to the view of operating system. • The java. awt package provides classes for AWT API such as Text. Field, Label, Text. Area, Radio. Button, Check. Box, Choice, List etc. CSI 3125, Preliminaries, page 2

Java AWT Hierarchy CSI 3125, Preliminaries, page 3

Java AWT Hierarchy CSI 3125, Preliminaries, page 3

AWT • • • Window Class The two most common windows are Panel, which

AWT • • • Window Class The two most common windows are Panel, which is used by applets, And Frame, which creates a standard window. Much of the functionality of these windows is derived from their parent classes. CSI 3125, Preliminaries, page 4

AWT • Component class • At the top of the AWT hierarchy is the

AWT • Component class • At the top of the AWT hierarchy is the Component class. • Component is an abstract class that encapsulates all of the attributes of a visual component that are displayed on the screen • 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. • A Component object is responsible for remembering the current foreground and background colors and the currently selected text font. CSI 3125, Preliminaries, page 5

AWT • Container class • The Container class is a subclass of Component. •

AWT • Container class • The Container class is a subclass of Component. • A container is responsible for laying out (that • is, positioning) any components that it contains. • It does this through the use of various layout managers CSI 3125, Preliminaries, page 6

AWT • Panel class • The Panel class is a subclass of Container. •

AWT • Panel class • The Panel class is a subclass of Container. • It doesn’t add any new methods; it simply implements Container. • Panel is a window that does not contain a title bar, menu bar, or border. • Other components can be added to a Panel object by its add( ) method (inherited from Container). • Once these components have been added, you can position and resize them manually using the set. Location( ), set. Size( ), or set. Bounds( ) methods defined by Component. CSI 3125, Preliminaries, page 7

AWT • Frame class • Frame encapsulates what is commonly thought of as a

AWT • Frame class • 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 program rather than an applet, a normal window is created. CSI 3125, Preliminaries, page 8

Frame Window Frame’s constructors: Frame( ) Frame(String title) The first form creates a standard

Frame Window 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. • • CSI 3125, Preliminaries, page 9

Frame Window • set. Size( ) method is used to set the dimensions of

Frame Window • set. Size( ) method is used to set the dimensions of the window. • void set. Size(int new. Width, int new. Height) • After a frame window has been created, it will not be visible until you call set. Visible( ). • void set. Visible(boolean visible. Flag) • set. Title( ) used to set title • void set. Title(String new. Title) CSI 3125, Preliminaries, page 10

Frame Window prog • • • import java. awt. *; class frame 1 extends

Frame Window prog • • • import java. awt. *; class frame 1 extends Frame { frame 1() { set. Size(500, 500); set. Visible(true); } public static void main(String args[]){ frame 1 f=new frame 1(); } } CSI 3125, Preliminaries, page 11

Frame Window • Closing a Frame Window • By calling set. Visible(false). • Implement

Frame Window • Closing a Frame Window • By calling set. Visible(false). • Implement the window. Closing( ) method of the Window. Listener interface. • Inside window. Closing( ), call set. Visible(false) CSI 3125, Preliminaries, page 12

 • • • • • Frame Closing Prog import java. awt. *; import

• • • • • Frame Closing Prog import java. awt. *; import java. awt. event. *; class frame 1 extends Frame { frame 1() { add. Window. Listener(new My()); set. Size(500, 500); set. Visible(true); } class My extends Window. Adapter { public void window. Closing(Window. Event me) { System. out. println("Closed"); set. Visible(false); } } public static void main(String args[]){ frame 1 f=new frame 1(); } } CSI 3125, Preliminaries, page 13

Working with Graphics • Drawing Lines • Lines are drawn by means of the

Working with Graphics • Drawing Lines • Lines are drawn by means of the draw. Line( ) method, • void draw. Line(int start. X, int start. Y, int end. X, int end. Y) • Drawing Rectangles • The draw. Rect( ) and fill. Rect( ) methods display an outlined and filled rectangle • void draw. Rect(int top, int left, int width, int height) • void fill. Rect(int top, int left, int width, int height) CSI 3125, Preliminaries, page 14

Working with Graphics • To draw a rounded rectangle, use draw. Round. Rect( )

Working with Graphics • To draw a rounded rectangle, use draw. Round. Rect( ) or fill. Round. Rect( ), both • void draw. Round. Rect(int top, int left, int width, int height, int x. Diam, int y. Diam) • void fill. Round. Rect(int top, int left, int width, int height, int x. Diam, int y. Diam) • Drawing Ellipses • void draw. Oval(int top, int left, int width, int height) • void fill. Oval(int top, int left, int width, int height) CSI 3125, Preliminaries, page 15

Working with Graphics • Drawing Arcs • Arcs can be drawn with draw. Arc(

Working with Graphics • Drawing Arcs • Arcs can be drawn with draw. Arc( ) and fill. Arc( ) • void draw. Arc(int top, int left, int width, int height, int start. Angle, int sweep. Angle) • void fill. Arc(int top, int left, int width, int height, int start. Angle, int sweep. Angle) CSI 3125, Preliminaries, page 16

Working with Graphics • Drawing Polygons • It is possible to draw arbitrarily shaped

Working with Graphics • Drawing Polygons • It is possible to draw arbitrarily shaped figures using draw. Polygon( ) and fill. Polygon( ), • void draw. Polygon(int x[ ], int y[ ], int num. Points) • void fill. Polygon(int x[ ], int y[ ], int num. Points) CSI 3125, Preliminaries, page 17

Working with Graphics Prog • • • • import java. applet. *; import java.

Working with Graphics Prog • • • • import java. applet. *; import java. awt. *; public class awt 2 extends Applet { public void paint(Graphics g) { g. set. Color(Color. red); g. fill. Round. Rect(100, 200, 30, 40); int x[]={10, 20, 30, 100, 40, 300, 550}; int y[]={2, 44, 55, 66, 77, 345, 123}; g. draw. Polygon(x, y, 7); } } /*<applet code=awt 2 width=500 height=500></applet>*/ CSI 3125, Preliminaries, page 18

Working with Colors • • • • Color class have 3 constructors Using this

Working with Colors • • • • Color class have 3 constructors Using this we can define our own colors Color(int red, int green, int blue) Color(int rgb. Value) Color(float red, float green, float blue) The first constructor takes three integers that specify the color as a mix of red, green, and blue. These values must be between 0 and 255, as in this example: new Color(255, 100); // light red. The second color 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. Here is an example of this constructor: int new. Red = (0 xff 000000 | (0 xc 0 << 16) | (0 x 00 << 8) | 0 x 00); Color dark. Red = new Color(new. Red); The third constructor, Color(float, float), takes three float values (between 0. 0 and 1. 0) that specify CSI 3125, Preliminaries, page 19

Working with Colors • Once you have created a color, you can use it

Working with Colors • Once you have created a color, you can use it to set the foreground and/or background color by using the set. Foreground( ) and set. Background( ) methods • eg • set. Background(new Color(200, 100, 230) ); • set. Foreground(new Color(200, 100, 230) ) CSI 3125, Preliminaries, page 20

Working with Fonts • Font f 1 = new Font(String font. Name, int font.

Working with Fonts • Font f 1 = new Font(String font. Name, int font. Style, int font. Size); • To give the font style as an integer value, the Font class comes with 3 symbolic constants. • public final static int PLAIN = 0; public final static int BOLD = 1; public final static int ITALIC = 2; • Font font = new Font("Arial", Font. BOLD, 20); • OR • Font font = new Font("Arial", Font. BOLD+ Font. ITALIC, 20); • OR • g. set. Font(font); CSI 3125, Preliminaries, page 21

Working with Fonts • • • • import java. applet. *; import java. awt.

Working with Fonts • • • • import java. applet. *; import java. awt. *; /*<applet code="M 53" width=200 height=100> </applet>*/ public class M 53 extends Applet { public void paint(Graphics g) { Font font = new Font("Arial", Font. BOLD, 20); g. set. Font(font); g. draw. String("popo", 50); } } CSI 3125, Preliminaries, page 22

Working with Fonts • • • • • import java. applet. *; import java.

Working with Fonts • • • • • import java. applet. *; import java. awt. *; /*<applet code="M 53" width=200 height=100> </applet>*/ public class M 53 extends Applet { public void paint(Graphics g) { Font f 1 = new Font("Arial", 2, 20); g. set. Font(f 1); g. draw. String("Font Name: " + f 1. get. Font. Name(), 15, 80); g. draw. String("Font Style: " + f 1. get. Style(), 15, 100); g. draw. String("Font Size: " + f 1. get. Size(), 15, 120); g. draw. String("is. Bold(): " + f 1. is. Bold(), 15, 140); g. draw. String("is. Italic(): " + f 1. is. Italic(), 15, 160); g. draw. String("popo", 50); } } CSI 3125, Preliminaries, page 23

AWT CSI 3125, Preliminaries, page 24

AWT CSI 3125, Preliminaries, page 24

AWT CSI 3125, Preliminaries, page 25

AWT CSI 3125, Preliminaries, page 25