Chapter 12 - Advanced GUIs and Graphics
Upcoming SlideShare
Loading in...5
×
 

Chapter 12 - Advanced GUIs and Graphics

on

  • 502 views

 

Statistics

Views

Total Views
502
Views on SlideShare
502
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Chapter 12 - Advanced GUIs and Graphics Chapter 12 - Advanced GUIs and Graphics Presentation Transcript

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