Download presentation
Presentation is loading. Please wait.
Published byDella Hall Modified over 9 years ago
1
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics
2
Java Programming: From Problem Analysis to Program Design, 4e2 Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class Color
3
Java Programming: From Problem Analysis to Program Design, 4e3 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
4
Java Programming: From Problem Analysis to Program Design, 4e4 Inheritance Hierarchy of GUI Classes
5
Java Programming: From Problem Analysis to Program Design, 4e5 Constructors and Methods of the class Component
6
Java Programming: From Problem Analysis to Program Design, 4e6 Constructors and Methods of the class Component (continued)
7
Java Programming: From Problem Analysis to Program Design, 4e7 Constructors and Methods of the class Component (continued)
8
Java Programming: From Problem Analysis to Program Design, 4e8 Constructors and Methods of the class Component (continued)
9
Java Programming: From Problem Analysis to Program Design, 4e9 Constructors and Methods of the class Container
10
Java Programming: From Problem Analysis to Program Design, 4e10 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
11
Java Programming: From Problem Analysis to Program Design, 4e11 Members of class JApplet
12
Java Programming: From Problem Analysis to Program Design, 4e12 Members of class Japplet (continued)
13
Java Programming: From Problem Analysis to Program Design, 4e13 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
14
Java Programming: From Problem Analysis to Program Design, 4e14 Applet Methods init Method –Initializes variables –Gets data from user –Places various GUI components paint Method –Performs output
15
Java Programming: From Problem Analysis to Program Design, 4e15 Skeleton of a Java Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { }
16
Java Programming: From Problem Analysis to Program Design, 4e16 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 }
17
Java Programming: From Problem Analysis to Program Design, 4e17 HTML to Run Applet
18
Java Programming: From Problem Analysis to Program Design, 4e18 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)
19
Java Programming: From Problem Analysis to Program Design, 4e19 Constructors and Methods of the class Font
20
Java Programming: From Problem Analysis to Program Design, 4e20 Constructors and Methods of the class Font (continued)
21
Java Programming: From Problem Analysis to Program Design, 4e21 Constructors and Methods of the class Font (continued)
22
Java Programming: From Problem Analysis to Program Design, 4e22 class Color Shows text in different colors Changes background color of component Contained in package java.awt
23
Java Programming: From Problem Analysis to Program Design, 4e23 Constructors of the class Color
24
Java Programming: From Problem Analysis to Program Design, 4e24 Constructors of the class Color (continued)
25
Java Programming: From Problem Analysis to Program Design, 4e25 Constants Defined in the class Color
26
Java Programming: From Problem Analysis to Program Design, 4e26 Constants Defined in the class Color (continued)
27
Java Programming: From Problem Analysis to Program Design, 4e27 Constants Defined in the class Color (continued)
28
Java Programming: From Problem Analysis to Program Design, 4e28 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
29
Java Programming: From Problem Analysis to Program Design, 4e29 Constructors and Methods of the class Graphics
30
Java Programming: From Problem Analysis to Program Design, 4e30 Constructors and Methods of the class Graphics (continued)
31
Java Programming: From Problem Analysis to Program Design, 4e31 Constructors and Methods of the class Graphics (continued)
32
Java Programming: From Problem Analysis to Program Design, 4e32 Constructors and Methods of the class Graphics (continued)
33
Java Programming: From Problem Analysis to Program Design, 4e33 Constructors and Methods of the class Graphics (continued)
34
Java Programming: From Problem Analysis to Program Design, 4e34 Constructors and Methods of the class Graphics (continued)
35
Java Programming: From Problem Analysis to Program Design, 4e35 Constructors and Methods of the class Graphics (continued)
36
Java Programming: From Problem Analysis to Program Design, 4e36 Constructors and Methods of the class Graphics (continued)
37
Java Programming: From Problem Analysis to Program Design, 4e37 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
38
Java Programming: From Problem Analysis to Program Design, 4e38 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)
39
Java Programming: From Problem Analysis to Program Design, 4e39 Additional GUI Components JTextArea JCheckBox JRadioButton JComboBox JList
40
Java Programming: From Problem Analysis to Program Design, 4e40 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
41
Java Programming: From Problem Analysis to Program Design, 4e41 JTextArea (continued)
42
Java Programming: From Problem Analysis to Program Design, 4e42 Methods Inherited by class JTextArea from Parent class JTextComponent
43
Java Programming: From Problem Analysis to Program Design, 4e43 JTextArea Example
44
Java Programming: From Problem Analysis to Program Design, 4e44 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
45
Java Programming: From Problem Analysis to Program Design, 4e45 Constructors and Methods of class JCheckBox
46
Java Programming: From Problem Analysis to Program Design, 4e46 Constructors and Methods of class JCheckBox (continued)
47
Java Programming: From Problem Analysis to Program Design, 4e47 Constructors and Methods of class JCheckBox (continued)
48
Java Programming: From Problem Analysis to Program Design, 4e48 Constructors and Methods of class JCheckBox (continued)
49
Java Programming: From Problem Analysis to Program Design, 4e49 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
50
Java Programming: From Problem Analysis to Program Design, 4e50 JRadioButton (continued)
51
Java Programming: From Problem Analysis to Program Design, 4e51 JRadioButton (continued)
52
Java Programming: From Problem Analysis to Program Design, 4e52 JRadioButton (continued)
53
Java Programming: From Problem Analysis to Program Design, 4e53 JRadioButton (continued)
54
Java Programming: From Problem Analysis to Program Design, 4e54 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
55
Java Programming: From Problem Analysis to Program Design, 4e55 Constructors of class JComboBox
56
Java Programming: From Problem Analysis to Program Design, 4e56 Applet with JCheckBox, JComboBox, and JRadioButton
57
Java Programming: From Problem Analysis to Program Design, 4e57 Constructors of class JList
58
Java Programming: From Problem Analysis to Program Design, 4e58 Constructors of class Jlist (continued)
59
Java Programming: From Problem Analysis to Program Design, 4e59 Constructors of class Jlist (continued)
60
Java Programming: From Problem Analysis to Program Design, 4e60 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
61
Java Programming: From Problem Analysis to Program Design, 4e61 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
62
Java Programming: From Problem Analysis to Program Design, 4e62 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
63
Java Programming: From Problem Analysis to Program Design, 4e63 Key and Mouse Events
64
Java Programming: From Problem Analysis to Program Design, 4e64 Chapter Summary Creating applets class Font class Graphics class Color Differences between applet and GUI application Converting GUI application to applet
65
Java Programming: From Problem Analysis to Program Design, 4e65 Chapter Summary (continued) GUI components –JTextArea –JCheckBox –JRadioButton Layout managers Menus Key and mouse events
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.