1 / 75

AWT

AWT. Window Fundamentals. Window Fundamentals. Component 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 .

elmo
Download Presentation

AWT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AWT

  2. Window Fundamentals

  3. Window Fundamentals • Component 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. • A Component object is responsible for remembering the current foreground and background colors and the currently selected text font.

  4. Window Fundamentals Container • 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

  5. Window Fundamentals Panel • A Panel may be thought of as a recursively nestable, concrete screen component. • Panel is the superclass for Applet. When screen output is directed to an applet, it is drawn on the surface of a Panel object. • In essence, a Panel is a window that does not contain a title bar, menu bar, or border.

  6. Window Fundamentals Window • The Window class creates a top-level window • Generally, Window objects are not created directly. • Instead, a subclass of Window called Frame.

  7. Window Fundamentals Frame • Frame is a subclass of Window • It has a title bar, menu bar, borders, and resizing corners. • If you create a Frame object from within an applet, it will contain a warning message, such as “Java Applet Window,” to the user that an applet window has been created. • This message warns users that the window was started by an applet.

  8. Frames • Frame( ) • Frame(String title)

  9. Frames import java.awt.*; import java.awt.event.*; class MyFrame extends Frame { MyFrame(String s) { super(s) } public void paint(Graphics g) { g.drawString("MCA Department",100,50); }

  10. Frames public static void main(String args[]) { MyFrame f = new MyFrame(“VESIT”); f.setSize(200,100); f.setVisible(true); } }

  11. Frames // Create a child frame window from within an applet. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="AppletFrame" width=300 height=50> </applet> */ class MyFrame extends Frame { MyFrame() { addWindowListener(new MyWindowAdapter(this)); }

  12. Frames public void paint(Graphics g) { g.drawString("Hello world",100,50); } } class MyWindowAdapter extends WindowAdapter { MyFrame f1; public MyWindowAdapter(MyFrame f1) { this.f1 = f1; } public void windowClosing(WindowEvent we) { f1.setVisible(false); } }

  13. Frames public class AppletFrame1 extends Applet { MyFrame f; public void init() { f = new MyFrame(); f.setSize(250, 250); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } public void paint(Graphics g) { g.drawString("This is in applet window", 10, 20); }

  14. Frames

  15. Color class Color(intred, intgreen, intblue) •  void setColor(Color newColor) •  Color getColor( ) import java.awt.*; import java.applet.*; /* <applet code=“ColorDemo" width=300 height=200> </applet> */ public class ColorDemo extends Applet { public void paint(Graphics g) { g.setColor(Color.green); g.drawString(“Hello World”, 50,50); } }

  16. Graphics Class • 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.

  17. Graphics Primitives • For drawing geometric shapes, texts • An abstract class • the extended class must override paint() Line RoundRectangle Polygon Oval Rectangle Arc

  18. Drawing Lines void drawLine(int startX, int startY, int endX, int endY) public void paint(Graphics g) { g.setColor(Color.blue); g.drawLine(0, 0, 100, 100); g.drawLine(0, 100, 100, 0); } (x1,y1) (x2,y2)

  19. Drawing Rectangles void drawRect(int top, int left, int width, int height) void fillRect(int top, int left, int width, int height) public void paint(Graphics g) { g.setColor(Color.pink); g.drawRect(10, 10, 60, 50); g.fillRect(100, 10, 60, 50); }

  20. Drawing Rounded Rectangles 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) xDiam: diameter of the rounding arc along the X axis yDiam: diameter of the rounding arc along the Y axis public void paint(Graphics g) { g.setColor(Color.yellow); g.drawRoundRect(190, 10, 60, 50, 15, 15); g.fillRoundRect(70, 90, 140, 100, 30, 40); }

  21. Drawing Ellipses and Circles • void drawOval(int top, int left, int width, int height) • void fillOval(int top, int left, int width, int height) • ellipse is drawn within a bounding rectangle specified by top,left , width • and height. • To draw a circle, specify a square as the bounding rectangle public void paint(Graphics g) { g.setColor(Color.blue); g.drawOval(10, 10, 75, 50); g.fillOval(100, 10, 50, 50); }

  22. Drawing Arcs • void drawArc(inttop, intleft, intwidth, intheight, • intstartAngle, intsweepAngle) • void fillArc(inttop, intleft, intwidth, intheight, • intstartAngle, intsweepAngle) • arc is bounded by the rectangle specified by top, left , width • and height. • The arc is drawn from startAnglethrough the angular distance • specified by sweepAngle.

  23. Drawing Arcs public void paint(Graphics g) { g.setColor(Color.orange); g.drawArc(10, 40, 70, 70, 0, 75); g.fillArc(100, 40, 70, 70, 0, 75); }

  24. Drawing Polygons void drawPolygon(int x[ ], int y[ ], int numPoints) void fillPolygon(int x[ ], int y[ ], int numPoints) public void paint(Graphics g) { g.setColor(Color.green); int xs[] = {161,161,185,209,185,161}; int ys[] = {310,334,358,334,310,310}; g.fillPolygon(xs,ys,6); }

  25. Font class Font(String fontName, int fontStyle, int pointSize) fontStyl: Font.PLAIN, Font.BOLD, and Font.ITALIC. To combine styles: Font.BOLD | Font.ITALIC void setFont(Font fontObj)

  26. Font class public void paint(Graphics g) { g.setColor(Color.blue); Font f = new Font(“TimesRoman”,Font.BOLD,20); g.setFont(f); g.drawString(“Hello World”, 50,50); }

  27. Component Canvas Scrollbar Button Checkbox Label List Choice TextComponent TextArea TextField Component Container Panel Window Dialog FileDialog Frame MenuComponent MenuItem Menu Using AWT Components

  28. Using AWT Components • Component add(Component compObj) • void remove(Component obj)

  29. Labels • Label( ) • Label(String str) • void setText(String str) • String getText( )

  30. Labels public void init() { Label one = new Label("One"); Label two = new Label("Two"); Label three = new Label("Three"); add(one); add(two); add(three); }

  31. Buttons • Button( ) • Button(String str) • void setLabel(String str) • String getLabel( )

  32. Buttons public void init() { Button yes = new Button("Yes"); Button no = new Button("No"); Button maybe = new Button("Undecided"); add(yes); add(no); add(maybe); }

  33. Buttons : Event Handling import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="ButtonDemo" width=250 height=150> </applet> */ public class ButtonDemo extends Applet implements ActionListener { String msg = ""; Button yes, no, maybe;

  34. Buttons : Event Handling public void init() { yes = new Button("Yes"); no = new Button("No"); maybe = new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); }

  35. Buttons : Event Handling public void actionPerformed(ActionEvent ae) { msg = ae.getActionCommand(); repaint(); } public void paint(Graphics g) { g.drawString("Pressed: "+msg, 6, 100); } }

  36. Buttons : Event Handling

  37. Checkboxes • Checkbox( ) • Checkbox(String str) • Checkbox(String str, boolean on) • Checkbox(String str, CheckboxGroup cbGroup, boolean on) • boolean getState( ) • void setState(boolean on) • String getLabel( ) • void setLabel(String str)

  38. Checkboxes: Event Handling public class CheckboxDemo extends Applet implements ItemListener { String msg = ""; Checkbox c, cpp, java; public void init() { c = new Checkbox("C", null, true); cpp = new Checkbox("C++"); java = new Checkbox("JAVA"); add(c); add(cpp); add(java);

  39. Checkboxes: Event Handling c.addItemListener(this); cpp.addItemListener(this); java.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); }

  40. Checkboxes: Event Handling public void paint(Graphics g) { msg = c.getLabel()+ " : " + c.getState(); g.drawString(msg, 6, 100); msg = cpp.getLabel()+ " : " + cpp.getState(); g.drawString(msg, 6, 120); msg = java.getLabel()+ " : " + java.getState(); g.drawString(msg, 6, 140); } }

  41. Checkboxes: Event Handling

  42. CheckboxGroup • Checkbox getSelectedCheckbox( ) • void setSelectedCheckbox(Checkbox which)

  43. CheckboxGroup public class CBGroup extends Frame { public CBGroup() { CheckboxGroup cbg = new CheckboxGroup(); Checkbox cb1 = new Checkbox(“C”, cbg,false); Checkbox cb2 = new Checkbox(“JAVA”, cbg, true); add(cb1); add(cb2); add(cb3); } … }

  44. Choices void add(String name) : To add a selection to the list String getSelectedItem( ) int getSelectedIndex( )

  45. Choices public class ColorChoice extends Applet implements ItemListener { Choice c1; Color color; public void init() { c1 = new Choice(); c1.addItem("Red"); c1.addItem("Green"); c1.addItem("Blue"); c1.addItemListener(this); add(c1); }

  46. Choices public void itemStateChanged(ItemEvent ie) { if(c1.getSelectedItem().equals("Red")) color = Color.red; else if(c1.getSelectedItem().equals("Green")) color = Color.green; else color = Color.blue; repaint(); } public void paint(Graphics g) { setBackground(color); } }

  47. Lists • List( ) • List(int numRows) • List(int numRows, boolean multipleSelect) • void add(String name) • void add(String name, int index) • String getSelectedItem( ) • int getSelectedIndex( )

  48. Lists public void init() { List list = new List(); list.add("Hydrogen"); list.add("Helium"); list.add("Carbon"); list.add("Oxygen"); add(list); }

  49. TextField • TextField( ) • TextField(int numChars) • TextField(String str) • TextField(String str, int numChars) • String getSelectedText( )

  50. TextArea • TextArea( ) • TextArea(int numLines, int numChars) • TextArea(String str) • TextArea(String str, int numLines, int numChars) • void append(String str)

More Related