Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.

Similar presentations


Presentation on theme: "CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based."— Presentation transcript:

1 CSI 3125, Preliminaries, page 1 AWT

2 CSI 3125, Preliminaries, page 2 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. The java.awt package provides classes for AWT API such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

3 CSI 3125, Preliminaries, page 3 Java AWT Hierarchy

4 CSI 3125, Preliminaries, page 4 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.

5 CSI 3125, Preliminaries, page 5 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.

6 CSI 3125, Preliminaries, page 6 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

7 CSI 3125, Preliminaries, page 7 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 setLocation( ), setSize( ), or setBounds( ) methods defined by Component.

8 CSI 3125, Preliminaries, page 8 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.

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

10 CSI 3125, Preliminaries, page 10 Frame Window setSize( ) method is used to set the dimensions of the window. void setSize(int newWidth, int newHeight) After a frame window has been created, it will not be visible until you call setVisible( ). void setVisible(boolean visibleFlag) setTitle( ) used to set title void setTitle(String newTitle)

11 CSI 3125, Preliminaries, page 11 Frame Window prog import java.awt.*; class frame1 extends Frame { frame1() { setSize(500,500); setVisible(true); } public static void main(String args[]){ frame1 f=new frame1(); }

12 CSI 3125, Preliminaries, page 12 Frame Window Closing a Frame Window By calling setVisible(false). Implement the windowClosing( ) method of the WindowListener interface. Inside windowClosing( ), call setVisible(false)

13 CSI 3125, Preliminaries, page 13 Frame Closing Prog import java.awt.*; import java.awt.event.*; class frame1 extends Frame { frame1() { addWindowListener(new My()); setSize(500,500); setVisible(true); } class My extends WindowAdapter { public void windowClosing(WindowEvent me) { System.out.println("Closed"); setVisible(false); } public static void main(String args[]){ frame1 f=new frame1(); } }

14 CSI 3125, Preliminaries, page 14 Working with Graphics Drawing Lines Lines are drawn by means of the drawLine( ) method, void drawLine(int startX, int startY, int endX, int endY) Drawing Rectangles The drawRect( ) and fillRect( ) methods display an outlined and filled rectangle void drawRect(int top, int left, int width, int height) void fillRect(int top, int left, int width, int height)

15 CSI 3125, Preliminaries, page 15 Working with Graphics To draw a rounded rectangle, use drawRoundRect( ) or fillRoundRect( ), both 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 void drawOval(int top, int left, int width, int height) void fillOval(int top, int left, int width, int height)

16 CSI 3125, Preliminaries, page 16 Working with Graphics Drawing Arcs Arcs can be drawn with drawArc( ) and fillArc( ) 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)

17 CSI 3125, Preliminaries, page 17 Working with Graphics Drawing Polygons It is possible to draw arbitrarily shaped figures using drawPolygon( ) and fillPolygon( ), void drawPolygon(int x[ ], int y[ ], int numPoints) void fillPolygon(int x[ ], int y[ ], int numPoints)

18 CSI 3125, Preliminaries, page 18 Working with Graphics Prog import java.applet.*; import java.awt.*; public class awt2 extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillRoundRect(100,100,200,200,30,40); int x[]={10,20,30,100,40,300,550}; int y[]={2,44,55,66,77,345,123}; g.drawPolygon(x,y,7); } /* */

19 CSI 3125, Preliminaries, page 19 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 rgbValue) 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, 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 newRed = (0xff000000 | (0xc0 << 16) | (0x00 << 8) | 0x00); Color darkRed = new Color(newRed); The third constructor, Color(float, float, float), takes three float values (between 0.0 and 1.0) that specify

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

21 CSI 3125, Preliminaries, page 21 Working with Fonts Font f1 = new Font(String fontName, int fontStyle, int fontSize); 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.setFont(font);

22 CSI 3125, Preliminaries, page 22 Working with Fonts import java.applet.*; import java.awt.*; /* */ public class M53 extends Applet { public void paint(Graphics g) { Font font = new Font("Arial", Font.BOLD,20); g.setFont(font); g.drawString("popo",50,50); } }

23 CSI 3125, Preliminaries, page 23 Working with Fonts import java.applet.*; import java.awt.*; /* */ public class M53 extends Applet { public void paint(Graphics g) { Font f1 = new Font("Arial", 2,20); g.setFont(f1); g.drawString("Font Name: " + f1.getFontName(), 15, 80); g.drawString("Font Style: " + f1.getStyle(), 15, 100); g.drawString("Font Size: " + f1.getSize(), 15, 120); g.drawString("isBold(): " + f1.isBold(), 15, 140); g.drawString("isItalic(): " + f1.isItalic(), 15, 160); g.drawString("popo",50,50); } }

24 CSI 3125, Preliminaries, page 24 AWT

25 CSI 3125, Preliminaries, page 25 AWT


Download ppt "CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based."

Similar presentations


Ads by Google