0
Java Programming: From Problem Analysis to Program Design, 5e Chapter 12 Advanced GUIs and Graphics
Chapter Objectives <ul><li>Learn about applets  </li></ul><ul><li>Explore the  class  Graphics </li></ul><ul><li>Learn abo...
Chapter Objectives (continued) <ul><li>Learn to use additional layout managers </li></ul><ul><li>Become familiar with more...
Inheritance Hierarchy of GUI Classes Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class  Component Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class   Component  (continued) Java Programming: From Problem Analysis to Program Design,...
Constructors and Methods of the  class   Component  (continued) Java Programming: From Problem Analysis to Program Design,...
Constructors and Methods of the  class   Component  (continued) Java Programming: From Problem Analysis to Program Design,...
Constructors and Methods of the  class   Container Java Programming: From Problem Analysis to Program Design, 5e
Applets <ul><li>Applet: a Java program that is embedded within a Web page and executed by a Web browser </li></ul><ul><li>...
Members of  class  JApplet Java Programming: From Problem Analysis to Program Design, 5e
Members of  class  Japplet  (continued)  Java Programming: From Problem Analysis to Program Design, 5e
Applets (continued) <ul><li>No  main  method </li></ul><ul><li>Methods  init ,  start , and  paint  guaranteed to be invok...
Applet Methods <ul><li>init  method </li></ul><ul><ul><li>Initializes variables </li></ul></ul><ul><ul><li>Gets data from ...
Skeleton of a Java Applet Java Programming: From Problem Analysis to Program Design, 5e import  java.awt.Graphics; import ...
Applet Displaying Welcome Message Java Programming: From Problem Analysis to Program Design, 5e //Welcome Applet import  j...
HTML to Run Applet Java Programming: From Problem Analysis to Program Design, 5e
class  Font <ul><li>Shows text in different fonts </li></ul><ul><li>Contained in package java.awt </li></ul><ul><li>Availa...
Constructors and Methods of the  class  Font Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class  Font  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class  Font  (continued) Java Programming: From Problem Analysis to Program Design, 5e
class  Color <ul><li>Shows text in different colors </li></ul><ul><li>Changes background color of component </li></ul><ul>...
Constructors of the  class  Color Java Programming: From Problem Analysis to Program Design, 5e
Constructors of the  class  Color  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constants Defined in the  class  Color Java Programming: From Problem Analysis to Program Design, 5e
Constants Defined in the  class  Color  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constants Defined in the  class  Color  (continued) Java Programming: From Problem Analysis to Program Design, 5e
class   Graphics <ul><li>Provides methods for drawing items such as lines, ovals, and rectangles on the screen </li></ul><...
Constructors and Methods of the  class   Graphics Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class   Graphics  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class   Graphics  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class   Graphics  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class   Graphics  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class   Graphics  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class   Graphics  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of the  class   Graphics  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Differences between Applets and GUI Applications <ul><li>Applets  </li></ul><ul><ul><li>Derived from  JApplet </li></ul></...
Converting a GUI Application to an Applet <ul><li>Change  JFrame  to  JApplet   </li></ul><ul><li>Change constructor to me...
Additional GUI Components <ul><li>JTextArea </li></ul><ul><li>JCheckBox </li></ul><ul><li>JRadioButton </li></ul><ul><li>J...
JTextArea <ul><li>Can collect multiple lines of input from user </li></ul><ul><li>Can display multiple lines of output </l...
JTextArea  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Methods Inherited by  class  JTextArea  from Parent  class   JTextComponent Java Programming: From Problem Analysis to Pro...
JTextArea  Example Java Programming: From Problem Analysis to Program Design, 5e
JCheckBox <ul><li>User selects from predefined values </li></ul><ul><li>Example of a toggle button </li></ul><ul><li>Click...
Constructors and Methods of  class  JCheckBox Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of  class  JCheckBox  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of  class  JCheckBox  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors and Methods of  class  JCheckBox  (continued) Java Programming: From Problem Analysis to Program Design, 5e
JRadioButton <ul><li>Created same way as check boxes  </li></ul><ul><li>Placed in content pane of applet </li></ul><ul><li...
JRadioButton  (continued) Java Programming: From Problem Analysis to Program Design, 5e
JRadioButton  (continued) Java Programming: From Problem Analysis to Program Design, 5e
JRadioButton  (continued) Java Programming: From Problem Analysis to Program Design, 5e
JRadioButton  (continued) Java Programming: From Problem Analysis to Program Design, 5e
JComboBox <ul><li>Commonly known as a drop-down list   </li></ul><ul><li>Used to select an item from a list of possibiliti...
Constructors of  class  JComboBox Java Programming: From Problem Analysis to Program Design, 5e
Applet with  JCheckBox ,  JComboBox , and  JRadioButton Java Programming: From Problem Analysis to Program Design, 5e
Constructors of  class  JList Java Programming: From Problem Analysis to Program Design, 5e
Constructors of  class  Jlist  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Constructors of  class  Jlist  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Layout Managers <ul><li>FlowLayout </li></ul><ul><ul><li>Default layout manager </li></ul></ul><ul><ul><li>Places componen...
Layout Managers (continued) <ul><li>BorderLayout </li></ul><ul><ul><li>Items placed into one of five specific regions </li...
Menus <ul><li>Allow for various functions without cluttering GUI with too many components </li></ul><ul><li>Can be attache...
Key and Mouse Events Java Programming: From Problem Analysis to Program Design, 5e
Chapter Summary <ul><li>Creating applets </li></ul><ul><li>class  Font </li></ul><ul><li>class   Graphics </li></ul><ul><l...
Chapter Summary (continued) <ul><li>GUI components </li></ul><ul><ul><li>JTextArea </li></ul></ul><ul><ul><li>JCheckBox </...
Upcoming SlideShare
Loading in...5
×

9781111530532 ppt ch12

608

Published on

Published in: Technology, News & Politics
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
608
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "9781111530532 ppt ch12"

  1. 1. Java Programming: From Problem Analysis to Program Design, 5e Chapter 12 Advanced GUIs and Graphics
  2. 2. Chapter Objectives <ul><li>Learn about applets </li></ul><ul><li>Explore the class Graphics </li></ul><ul><li>Learn about the class Font </li></ul><ul><li>Explore the class Color </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  3. 3. Chapter Objectives (continued) <ul><li>Learn to use additional layout managers </li></ul><ul><li>Become familiar with more GUI components </li></ul><ul><li>Learn how to create menu-based programs </li></ul><ul><li>Explore how to handle key and mouse events </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  4. 4. Inheritance Hierarchy of GUI Classes Java Programming: From Problem Analysis to Program Design, 5e
  5. 5. Constructors and Methods of the class Component Java Programming: From Problem Analysis to Program Design, 5e
  6. 6. Constructors and Methods of the class Component (continued) Java Programming: From Problem Analysis to Program Design, 5e
  7. 7. Constructors and Methods of the class Component (continued) Java Programming: From Problem Analysis to Program Design, 5e
  8. 8. Constructors and Methods of the class Component (continued) Java Programming: From Problem Analysis to Program Design, 5e
  9. 9. Constructors and Methods of the class Container Java Programming: From Problem Analysis to Program Design, 5e
  10. 10. Applets <ul><li>Applet: a Java program that is embedded within a Web page and executed by a Web browser </li></ul><ul><li>Create an applet by extending the class JApplet </li></ul><ul><li>class JApplet contained in package javax.swing </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  11. 11. Members of class JApplet Java Programming: From Problem Analysis to Program Design, 5e
  12. 12. Members of class Japplet (continued) Java Programming: From Problem Analysis to Program Design, 5e
  13. 13. Applets (continued) <ul><li>No main method </li></ul><ul><li>Methods init , start , and paint guaranteed to be invoked in sequence </li></ul><ul><li>To develop an applet: </li></ul><ul><ul><li>Override any/all of the methods above </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 5e
  14. 14. Applet Methods <ul><li>init method </li></ul><ul><ul><li>Initializes variables </li></ul></ul><ul><ul><li>Gets data from user </li></ul></ul><ul><ul><li>Places various GUI components </li></ul></ul><ul><li>paint method </li></ul><ul><ul><li>Performs output </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 5e
  15. 15. Skeleton of a Java Applet Java Programming: From Problem Analysis to Program Design, 5e import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { }
  16. 16. Applet Displaying Welcome Message Java Programming: From Problem Analysis to Program Design, 5e //Welcome Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { public void paint(Graphics g) { super .paint(g); //Line 1 g.drawString( &quot; Welcome to Java Programming &quot; , 30, 30); //Line 2 } }
  17. 17. HTML to Run Applet Java Programming: From Problem Analysis to Program Design, 5e
  18. 18. class Font <ul><li>Shows text in different fonts </li></ul><ul><li>Contained in package java.awt </li></ul><ul><li>Available fonts </li></ul><ul><ul><li>Serif/SanSerif </li></ul></ul><ul><ul><li>Monospaced </li></ul></ul><ul><ul><li>Dialog/DialogInput </li></ul></ul><ul><li>Arguments for constructor </li></ul><ul><ul><li>String specifying the Font face name </li></ul></ul><ul><ul><li>int value specifying Font style </li></ul></ul><ul><ul><li>int value specifying Font size </li></ul></ul><ul><ul><ul><li>Expressed in points (72 points = 1 inch) </li></ul></ul></ul>Java Programming: From Problem Analysis to Program Design, 5e
  19. 19. Constructors and Methods of the class Font Java Programming: From Problem Analysis to Program Design, 5e
  20. 20. Constructors and Methods of the class Font (continued) Java Programming: From Problem Analysis to Program Design, 5e
  21. 21. Constructors and Methods of the class Font (continued) Java Programming: From Problem Analysis to Program Design, 5e
  22. 22. class Color <ul><li>Shows text in different colors </li></ul><ul><li>Changes background color of component </li></ul><ul><li>Contained in package java.awt </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  23. 23. Constructors of the class Color Java Programming: From Problem Analysis to Program Design, 5e
  24. 24. Constructors of the class Color (continued) Java Programming: From Problem Analysis to Program Design, 5e
  25. 25. Constants Defined in the class Color Java Programming: From Problem Analysis to Program Design, 5e
  26. 26. Constants Defined in the class Color (continued) Java Programming: From Problem Analysis to Program Design, 5e
  27. 27. Constants Defined in the class Color (continued) Java Programming: From Problem Analysis to Program Design, 5e
  28. 28. class Graphics <ul><li>Provides methods for drawing items such as lines, ovals, and rectangles on the screen </li></ul><ul><li>Contains methods to set the properties of graphic elements including clipping area, fonts, and colors </li></ul><ul><li>Contained in the package java.awt </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  29. 29. Constructors and Methods of the class Graphics Java Programming: From Problem Analysis to Program Design, 5e
  30. 30. Constructors and Methods of the class Graphics (continued) Java Programming: From Problem Analysis to Program Design, 5e
  31. 31. Constructors and Methods of the class Graphics (continued) Java Programming: From Problem Analysis to Program Design, 5e
  32. 32. Constructors and Methods of the class Graphics (continued) Java Programming: From Problem Analysis to Program Design, 5e
  33. 33. Constructors and Methods of the class Graphics (continued) Java Programming: From Problem Analysis to Program Design, 5e
  34. 34. Constructors and Methods of the class Graphics (continued) Java Programming: From Problem Analysis to Program Design, 5e
  35. 35. Constructors and Methods of the class Graphics (continued) Java Programming: From Problem Analysis to Program Design, 5e
  36. 36. Constructors and Methods of the class Graphics (continued) Java Programming: From Problem Analysis to Program Design, 5e
  37. 37. Differences between Applets and GUI Applications <ul><li>Applets </li></ul><ul><ul><li>Derived from JApplet </li></ul></ul><ul><ul><li>No main method </li></ul></ul><ul><ul><li>Uses init method </li></ul></ul><ul><ul><li>Displayed by HTML </li></ul></ul><ul><ul><li>Sets title in HTML </li></ul></ul><ul><ul><li>Size set in HTML </li></ul></ul><ul><ul><li>Applet closes when HTML doc closes </li></ul></ul><ul><li>GUI applications </li></ul><ul><ul><li>Class extends JFrame </li></ul></ul><ul><ul><li>Invokes main method </li></ul></ul><ul><ul><li>Uses constructors </li></ul></ul><ul><ul><li>Uses method setVisible </li></ul></ul><ul><ul><li>Uses setTitle method </li></ul></ul><ul><ul><li>Uses method setSize </li></ul></ul><ul><ul><li>Closes with Exit button </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 5e
  38. 38. Converting a GUI Application to an Applet <ul><li>Change JFrame to JApplet </li></ul><ul><li>Change constructor to method init </li></ul><ul><li>Remove method calls such as setVisible , setTitle , setSize </li></ul><ul><li>Remove the method main </li></ul><ul><li>If applicable, remove Exit button/all code associated with it (e.g., action listener) </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  39. 39. Additional GUI Components <ul><li>JTextArea </li></ul><ul><li>JCheckBox </li></ul><ul><li>JRadioButton </li></ul><ul><li>JComboBox </li></ul><ul><li>JList </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  40. 40. JTextArea <ul><li>Can collect multiple lines of input from user </li></ul><ul><li>Can display multiple lines of output </li></ul><ul><li>Pressing Enter key separates lines of text </li></ul><ul><li>Each line ends with newline character ā€˜nā€™ </li></ul><ul><li>Derived from class JTextComponent </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  41. 41. JTextArea (continued) Java Programming: From Problem Analysis to Program Design, 5e
  42. 42. Methods Inherited by class JTextArea from Parent class JTextComponent Java Programming: From Problem Analysis to Program Design, 5e
  43. 43. JTextArea Example Java Programming: From Problem Analysis to Program Design, 5e
  44. 44. JCheckBox <ul><li>User selects from predefined values </li></ul><ul><li>Example of a toggle button </li></ul><ul><li>Clicking JCheckBox generates item event </li></ul><ul><li>Use interface ItemListener and its abstract method itemStateChanged to handle event </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  45. 45. Constructors and Methods of class JCheckBox Java Programming: From Problem Analysis to Program Design, 5e
  46. 46. Constructors and Methods of class JCheckBox (continued) Java Programming: From Problem Analysis to Program Design, 5e
  47. 47. Constructors and Methods of class JCheckBox (continued) Java Programming: From Problem Analysis to Program Design, 5e
  48. 48. Constructors and Methods of class JCheckBox (continued) Java Programming: From Problem Analysis to Program Design, 5e
  49. 49. JRadioButton <ul><li>Created same way as check boxes </li></ul><ul><li>Placed in content pane of applet </li></ul><ul><li>Forces user to select only one radio button at a time </li></ul><ul><li>You create a button group to group radio buttons </li></ul><ul><li>Generates an ItemEvent </li></ul><ul><li>interface ItemListener and method itemStateChanged used to handle events </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  50. 50. JRadioButton (continued) Java Programming: From Problem Analysis to Program Design, 5e
  51. 51. JRadioButton (continued) Java Programming: From Problem Analysis to Program Design, 5e
  52. 52. JRadioButton (continued) Java Programming: From Problem Analysis to Program Design, 5e
  53. 53. JRadioButton (continued) Java Programming: From Problem Analysis to Program Design, 5e
  54. 54. JComboBox <ul><li>Commonly known as a drop-down list </li></ul><ul><li>Used to select an item from a list of possibilities </li></ul><ul><li>Generates an ItemEvent </li></ul><ul><li>Event monitored by ItemListener </li></ul><ul><li>ItemListener invokes method itemStateChanged </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  55. 55. Constructors of class JComboBox Java Programming: From Problem Analysis to Program Design, 5e
  56. 56. Applet with JCheckBox , JComboBox , and JRadioButton Java Programming: From Problem Analysis to Program Design, 5e
  57. 57. Constructors of class JList Java Programming: From Problem Analysis to Program Design, 5e
  58. 58. Constructors of class Jlist (continued) Java Programming: From Problem Analysis to Program Design, 5e
  59. 59. Constructors of class Jlist (continued) Java Programming: From Problem Analysis to Program Design, 5e
  60. 60. Layout Managers <ul><li>FlowLayout </li></ul><ul><ul><li>Default layout manager </li></ul></ul><ul><ul><li>Places components from left to right, center by default, until no more items can be placed </li></ul></ul><ul><ul><li>Can align each line left, center, or right </li></ul></ul><ul><ul><li>Default alignment: LEFT </li></ul></ul><ul><li>GridLayout </li></ul><ul><ul><li>Similar to FlowLayout </li></ul></ul><ul><ul><li>All rows (columns) have same number of components </li></ul></ul><ul><ul><li>All components have the same size </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 5e
  61. 61. Layout Managers (continued) <ul><li>BorderLayout </li></ul><ul><ul><li>Items placed into one of five specific regions </li></ul></ul><ul><ul><ul><li>NORTH/SOUTH </li></ul></ul></ul><ul><ul><ul><li>EAST/WEST </li></ul></ul></ul><ul><ul><ul><li>CENTER </li></ul></ul></ul><ul><ul><li>NORTH and SOUTH components extend horizontally (completely span one edge to the other) </li></ul></ul><ul><ul><li>EAST and WEST components extend vertically between components in NORTH and SOUTH regions </li></ul></ul><ul><ul><li>CENTER component expands to occupy any unused regions </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 5e
  62. 62. Menus <ul><li>Allow for various functions without cluttering GUI with too many components </li></ul><ul><li>Can be attached to objects such as JFrame and JApplet ( setJMenuBar method) </li></ul><ul><li>To set a menu bar: </li></ul><ul><ul><li>private JMenuBar menuMB = </li></ul></ul><ul><ul><li>new JMenuBar(); </li></ul></ul><ul><ul><li>setJMenuBar(menuMB); </li></ul></ul><ul><li>Add menus to menu bar; add menu items to menu </li></ul><ul><li>Order of menus added = Order of appearance </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  63. 63. Key and Mouse Events Java Programming: From Problem Analysis to Program Design, 5e
  64. 64. Chapter Summary <ul><li>Creating applets </li></ul><ul><li>class Font </li></ul><ul><li>class Graphics </li></ul><ul><li>class Color </li></ul><ul><li>Differences between applet and GUI application </li></ul><ul><li>Converting GUI application to applet </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  65. 65. Chapter Summary (continued) <ul><li>GUI components </li></ul><ul><ul><li>JTextArea </li></ul></ul><ul><ul><li>JCheckBox </li></ul></ul><ul><ul><li>JRadioButton </li></ul></ul><ul><li>Layout managers </li></ul><ul><li>Menus </li></ul><ul><li>Key and mouse events </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

Ɨ