9781111530532 ppt ch12
TRANSCRIPT
![Page 1: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/1.jpg)
Java Programming: From Problem Analysis to Program Design, 5e
Chapter 12Advanced GUIs and Graphics
![Page 2: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/2.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 2
Chapter Objectives
• Learn about applets
• Explore the class Graphics• Learn about the class Font• Explore the class Color
![Page 3: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/3.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 3
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
![Page 4: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/4.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 4
Inheritance Hierarchy of GUI Classes
![Page 5: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/5.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 5
Constructors and Methods of the class Component
![Page 6: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/6.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 6
Constructors and Methods of the class Component (continued)
![Page 7: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/7.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 7
Constructors and Methods of the class Component (continued)
![Page 8: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/8.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 8
Constructors and Methods of the class Component (continued)
![Page 9: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/9.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 9
Constructors and Methods of the class Container
![Page 10: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/10.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 10
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
![Page 11: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/11.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 11
Members of class JApplet
![Page 12: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/12.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 12
Members of class Japplet (continued)
![Page 13: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/13.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 13
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
![Page 14: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/14.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 14
Applet Methods
• init method– Initializes variables– Gets data from user– Places various GUI components
• paint method– Performs output
![Page 15: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/15.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 15
Skeleton of a Java Applet
import java.awt.Graphics;import javax.swing.JApplet;
public class WelcomeApplet extends JApplet{
}
![Page 16: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/16.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 16
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 }}
![Page 17: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/17.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 17
HTML to Run Applet
![Page 18: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/18.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 18
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)
![Page 19: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/19.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 19
Constructors and Methods of the class Font
![Page 20: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/20.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 20
Constructors and Methods of the class Font (continued)
![Page 21: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/21.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 21
Constructors and Methods of the class Font (continued)
![Page 22: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/22.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 22
class Color
• Shows text in different colors
• Changes background color of component
• Contained in package java.awt
![Page 23: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/23.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 23
Constructors of the class Color
![Page 24: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/24.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 24
Constructors of the class Color (continued)
![Page 25: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/25.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 25
Constants Defined in the class Color
![Page 26: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/26.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 26
Constants Defined in the class Color (continued)
![Page 27: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/27.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 27
Constants Defined in the class Color (continued)
![Page 28: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/28.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 28
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
![Page 29: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/29.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 29
Constructors and Methods of the class Graphics
![Page 30: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/30.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 30
Constructors and Methods of the class Graphics (continued)
![Page 31: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/31.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 31
Constructors and Methods of the class Graphics (continued)
![Page 32: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/32.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 32
Constructors and Methods of the class Graphics (continued)
![Page 33: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/33.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 33
Constructors and Methods of the class Graphics (continued)
![Page 34: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/34.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 34
Constructors and Methods of the class Graphics (continued)
![Page 35: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/35.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 35
Constructors and Methods of the class Graphics (continued)
![Page 36: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/36.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 36
Constructors and Methods of the class Graphics (continued)
![Page 37: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/37.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 37
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
![Page 38: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/38.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 38
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)
![Page 39: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/39.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 39
Additional GUI Components
• JTextArea• JCheckBox• JRadioButton• JComboBox• JList
![Page 40: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/40.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 40
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
![Page 41: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/41.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 41
JTextArea (continued)
![Page 42: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/42.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 42
Methods Inherited by class JTextArea from Parent
class JTextComponent
![Page 43: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/43.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 43
JTextArea Example
![Page 44: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/44.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 44
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
![Page 45: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/45.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 45
Constructors and Methods of class JCheckBox
![Page 46: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/46.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 46
Constructors and Methods of class JCheckBox (continued)
![Page 47: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/47.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 47
Constructors and Methods of class JCheckBox (continued)
![Page 48: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/48.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 48
Constructors and Methods of class JCheckBox (continued)
![Page 49: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/49.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 49
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
![Page 50: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/50.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 50
JRadioButton (continued)
![Page 51: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/51.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 51
JRadioButton (continued)
![Page 52: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/52.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 52
JRadioButton (continued)
![Page 53: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/53.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 53
JRadioButton (continued)
![Page 54: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/54.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 54
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
![Page 55: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/55.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 55
Constructors of class JComboBox
![Page 56: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/56.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 56
Applet with JCheckBox, JComboBox, and JRadioButton
![Page 57: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/57.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 57
Constructors of class JList
![Page 58: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/58.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 58
Constructors of class Jlist (continued)
![Page 59: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/59.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 59
Constructors of class Jlist (continued)
![Page 60: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/60.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 60
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
![Page 61: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/61.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 61
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
![Page 62: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/62.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 62
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
![Page 63: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/63.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 63
Key and Mouse Events
![Page 64: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/64.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 64
Chapter Summary
• Creating applets• class Font• class Graphics• class Color• Differences between applet and GUI
application
• Converting GUI application to applet
![Page 65: 9781111530532 ppt ch12](https://reader035.vdocuments.mx/reader035/viewer/2022062312/555c3d43d8b42a2c068b4b18/html5/thumbnails/65.jpg)
Java Programming: From Problem Analysis to Program Design, 5e 65
Chapter Summary (continued)
• GUI components– JTextArea– JCheckBox– JRadioButton
• Layout managers
• Menus
• Key and mouse events