Presentation is loading. Please wait.

Presentation is loading. Please wait.

Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Similar presentations


Presentation on theme: "Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen."— Presentation transcript:

1

2 Window Fundamentals 2

3  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 1. Frame( ) 2. Frame(String title) void setSize(int newWidth, int newHeight)To set the dimensions of the window void setVisible(boolean visibleFlag) void setTitle(String newTitle)Setting a Window’s 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.*; /* */ 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(int red, int green, int blue) 1. void setColor(Color newColor) 2. Color getColor( ) import java.awt.*; import java.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 17 Graphics Primitives 4 For drawing geometric shapes, texts 4 An abstract class –the extended class must override paint() Oval Rectangle Arc Line RoundRectangle Polygon

18 18 Drawing Lines 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) void drawLine(int startX, int startY, int endX, int endY)

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

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 21 Drawing Ellipses and Circles public void paint(Graphics g) { g.setColor(Color.blue); g.drawOval(10, 10, 75, 50); g.fillOval(100, 10, 50, 50); } 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

22 Drawing Arcs 1.void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle) 2.void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)  arc is bounded by the rectangle specified by top, left, width and height.  The arc is drawn from startAngle through 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 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); } void drawPolygon(int x[ ], int y[ ], int numPoints) void fillPolygon(int x[ ], int y[ ], int numPoints)

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 27 Using AWT Components  Component  Canvas  Scrollbar  Button  Checkbox  Label  List  Choice  TextComponent  TextArea  TextField  Component –Container Panel Window –Dialog »FileDialog –Frame  MenuComponent –MenuItem Menu

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( ) 31

32 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.*; /* */ 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 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 44 Choices 1.void add(String name) : To add a selection to the list 2.String getSelectedItem( ) 3.int getSelectedIndex( )

45 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 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)

51 MenuBar, Menu, & MenuItem  Menu bar contains one or more Menu objects.  Each Menu object contains a list of MenuItem objects.  Each MenuItem object represents something that can be selected by the user.

52 MenuBar, Menu, & MenuItem MenuBar mb = new MenuBar(); setMenuBar(mb); // Create menu A Menu a = new Menu("A"); mb.add(a); MenuItem a1 = new MenuItem("A1"); MenuItem a2 = new MenuItem("A2"); MenuItem a3 = new MenuItem("A3"); a.add(a1); a.add(a2); a.add(a3);

53 MenuBar, Menu, & MenuItem // Create menu B Menu b = new Menu("B"); mb.add(b); MenuItem b1 = new MenuItem("B1"); MenuItem b2 = new MenuItem("B2"); b.add(b1); b.add(b2);

54 MenuBar, Menu, & MenuItem // Create sub-menu for B3 Menu b3 = new Menu("B3"); b.add(b3); MenuItem b31 = new MenuItem("B31"); MenuItem b32 = new MenuItem("B32"); b3.add(b31); b3.add(b32);

55 55 Arranging components  Every Container has a layout manager  The default layout for a Panel is FlowLayout  An Applet is a Panel therefore, the default layout for a Applet is FlowLayout  The layout manager is set by the setLayout( ) method. void setLayout(LayoutManager layoutObj)

56 Layout of Components  FlowLayout –left to right & top down  BorderLayout –north, south, west, east & center  CardLayout –stack of panels  GridLayout –tabular form –rows & columns

57 57 FlowLayout  Use add(component); to add to a component when using a FlowLayout  Components are added left-to-right  If no room, a new row is started  FlowLayout is convenient but often ugly

58 58 FlowLayout 1. FlowLayout( )  the default layout, which centers components and leaves five pixels of space between each component. 2. FlowLayout(int how)  specify how each line is aligned.  Valid values for how are as follows: 1.FlowLayout.LEFT 2. FlowLayout.CENTER 3. FlowLayout.RIGHT 3. FlowLayout(int how, int horz, int vert)  horz and vert the horizontal and vertical space left between components

59 59 FlowLayout import java.awt.*; import java.applet.*; public class FlowLayoutExample extends Applet { public void init () { setLayout (new FlowLayout ()); add (new Button ("One")); add (new Button ("Two")); add (new Button ("Three")); add (new Button ("Four")); add (new Button ("Five")); add (new Button ("Six")); } }

60 BorderLayout  BorderLayout( )  BorderLayout(int horz, int vert)  BorderLayout defines the following constants that specify the regions: 1. BorderLayout.CENTER 2. BorderLayout.SOUTH 3. BorderLayout.EAST 4. BorderLayout.WEST 5. BorderLayout.NORTH

61 61 BorderLayout  At most five components can be added void add(Component compObj, Object region)  If you want more components, add a Panel, then add components to it. add (new Button("NORTH"), BorderLayout.NORTH);

62 62 BorderLayout public void init() { setLayout (new BorderLayout ()); add (new Button ("NORTH"), BorderLayout.NORTH); add (new Button ("SOUTH"), BorderLayout.SOUTH); add (new Button ("EAST"), BorderLayout.EAST); add (new Button ("WEST"), BorderLayout.WEST); add (new Button ("CENTER"), BorderLayout.CENTER); }

63 63 BorderLayout import java.awt.*; import java.applet.*; public class BorderLayoutExample extends Applet { public void init () { setLayout (new BorderLayout()); add(new Button("One"), BorderLayout.NORTH); add(new Button("Two"), BorderLayout.WEST); add(new Button("Three"), BorderLayout.CENTER); add(new Button("Four"), BorderLayout.EAST); add(new Button("Five"), BorderLayout.SOUTH); add(new Button("Six"), BorderLayout.SOUTH); }

64 64 Using a Panel Panel p = new Panel(); add (p, BorderLayout.SOUTH); p.add (new Button ("Button 1")); p.add (new Button ("Button 2"));

65 65 GridLayout  The GridLayout manager divides the container up into a given number of rows and columns: GridLayout(int rows, int columns )  All sections of the grid are equally sized and as large as possible

66 66 GridLayout import java.awt.*; import java.applet.*; public class GridLayoutExample extends Applet { public void init () { setLayout(new GridLayout(2, 3)); add(new Button("One")); add(new Button("Two")); add(new Button("Three")); add(new Button("Four")); add(new Button("Five")); } }

67 CardLayout  The CardLayout class can store several different layouts.  Each layout can be thought of as being on a separate index card in a deck that can be shuffled so that any card is on top at a given time.  Useful for user interfaces that can be dynamically enabled and disabled upon user input.  User can keep other layouts hidden, ready to be activated when needed.

68 CardLayout  CardLayout( )  CardLayout(int horz, int vert)

69 CardLayout : How to Use?  The cards are typically held in an object of type Panel.  The cards that form the deck are also typically objects of type Panel.  This panel must have CardLayout selected as its layout manager.  Thus, Create a panel that contains the deck and a panel for each card in the deck.

70 CardLayout : How to Use?  Next, add components to the appropriate panel toform each card.  Add these panels to the Deck panel for which CardLayout is the layout manager.  Finally, add Deck panel to the window.  Provide some way for the user to select between cards, common approach is to include one push button for each card in the deck.

71 CardLayout: Example

72 public class CardLayoutDemo extends Applet implements ActionListener { Checkbox Win98, winNT, solaris, mac; Panel osCards; CardLayout cardLO; Button Win, Other; public void init() { Win = new Button("Windows"); Other = new Button("Other"); add(Win); add(Other);

73 CardLayout: Example cardLO = new CardLayout(); osCards = new Panel(); osCards.setLayout(cardLO); // set Deck panel layout to card layout Win98 = new Checkbox("Windows 98", null, true); winNT = new Checkbox("Windows NT/2000"); solaris = new Checkbox("Solaris"); mac = new Checkbox("MacOS"); // add Windows check boxes to a card panel Panel winPan = new Panel(); winPan.add(Win98); winPan.add(winNT);

74 CardLayout: Example // Add other OS check boxes to a card panel Panel otherPan = new Panel(); otherPan.add(solaris); otherPan.add(mac); // add panels to card deck panel osCards.add(winPan, "Windows"); osCards.add(otherPan, "Other"); // add cards to main applet panel add(osCards); // register to receive action events Win.addActionListener(this); Other.addActionListener(this); }

75 CardLayout: Example public void actionPerformed(ActionEvent ae) { if(ae.getSource() == Win) cardLO.show(osCards, "Windows"); else cardLO.show(osCards, "Other"); }


Download ppt "Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen."

Similar presentations


Ads by Google