Chapter 12 Advanced GUIs and Graphics Java Programming




















































- Slides: 52

Chapter 12: Advanced GUIs and Graphics Java Programming: From Problem Analysis to Program Design, Fourth Edition Java Programming: From Problem Analysis to Program Design,

Chapter Objectives s s s s Learn about applets. Explore the class Graphics. Learn about the class Font. Explore the class Color. 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, Second Edition 2

Inheritance Hierarchy of GUI Classes Java Programming: From Problem Analysis to Program Design, Second Edition 3

Constructors and Methods of the class Component Java Programming: From Problem Analysis to Program Design, Second Edition 4

Constructors and Methods of the class Component Java Programming: From Problem Analysis to Program Design, Second Edition 5

Constructors and Methods of the class Component Java Programming: From Problem Analysis to Program Design, Second Edition 6

Constructors and Methods of the class Component Java Programming: From Problem Analysis to Program Design, Second Edition 7

Applets s A Java program that is embedded within a Web page and executed by a Web browser. s Create an applet by extending the class JApplet. s class JApplet is contained in package javax. swing. Java Programming: From Problem Analysis to Program Design, Second Edition 8

Applets Java Programming: From Problem Analysis to Program Design, Second Edition 9

Applets Java Programming: From Problem Analysis to Program Design, Second Edition 10

Applets s No main method. s Methods init, start, and paint guaranteed to be invoked in sequence. s To develop an applet: s Override any/all of the methods above. Java Programming: From Problem Analysis to Program Design, Second Edition 11

Applet Methods s init method: s Initializes variables. s Gets data from user. s Places various GUI components. s paint method: s Performs output. Java Programming: From Problem Analysis to Program Design, Second Edition 12

Skeleton of a Java Applet import java. awt. Graphics; import javax. swing. JApplet; public class Welcome. Applet extends JApplet { } Java Programming: From Problem Analysis to Program Design, Second Edition 13

Applet Displaying Welcome Message //Welcome Applet import java. awt. Graphics; import javax. swing. JApplet; public class Welcome. Applet extends JApplet { public void paint(Graphics g) { super. paint(g); //Line 1 g. draw. String("Welcome to Java Programming" , 30); //Line 2 } } Java Programming: From Problem Analysis to Program Design, Second Edition 14

HTML to Run Applet Java Programming: From Problem Analysis to Program Design, Second Edition 15

class Font s Shows text in different fonts. s Contained in package java. awt. s Available fonts: s Serif/Sans Serif s Monospaced s Dialog/Dialog. Input s Arguments for constructor: s String specifying the font face name. s int value specifying font style. s int value specifying font size. s Expressed in points (72 points = 1 inch). Java Programming: From Problem Analysis to Program Design, Second Edition 16

class Font Java Programming: From Problem Analysis to Program Design, Second Edition 17

class Color s Shows text in different colors. s Changes background color of component. s Contained in package java. awt. Java Programming: From Problem Analysis to Program Design, Second Edition 18

class Color Java Programming: From Problem Analysis to Program Design, Second Edition 19

class Color Java Programming: From Problem Analysis to Program Design, Second Edition 20

class Color Java Programming: From Problem Analysis to Program Design, Second Edition 21

class Graphics s Provides methods for drawing items such as lines, ovals, and rectangles on the screen. s Contains methods to set the properties of graphic elements including clipping areas, fonts, and colors. s Contained in the package java. awt. Java Programming: From Problem Analysis to Program Design, Second Edition 22

class Graphics Java Programming: From Problem Analysis to Program Design, Second Edition 23

Constructors and Methods for the class Graphics Java Programming: From Problem Analysis to Program Design, Second Edition 24

Constructors and Methods for the class Graphics Java Programming: From Problem Analysis to Program Design, Second Edition 25

Constructors and Methods for the class Graphics Java Programming: From Problem Analysis to Program Design, Second Edition 26

Constructors and Methods for the class Graphics Java Programming: From Problem Analysis to Program Design, Second Edition 27

Differences Between Applets and GUI Applications s Applets s GUI applications s Derived from JApplet. s Class extends JFrame. s No main method. s Invokes main method. s Uses init method. s Uses constructors. s Displayed by HTML. s Uses method set. Visible. s Sets title in HTML. s Uses set. Title method. s Size set in HTML. s Uses method set. Size. s Applet closes when HTML doc closes. s Closes with Exit button. Java Programming: From Problem Analysis to Program Design, Second Edition 28

Converting a GUI Application to an Applet s Change JFrame to JApplet. s Change constructor to method init. s Remove method calls such as set. Visible, set. Title, set. Size. s Remove the method main. s If applicable, remove Exit button and all code associated with it (for example, action listener). Java Programming: From Problem Analysis to Program Design, Second Edition 29

Additional GUI Components s JText. Area s JCheck. Box s JRadio. Button s JCombo. Box s JList Java Programming: From Problem Analysis to Program Design, Second Edition 30

JText. Area s Can collect multiple lines of input from user. s Can display multiple lines of output. s Pressing Enter key separates lines of text. s Each line ends with newline character (n). s Derived from class JText. Component. Java Programming: From Problem Analysis to Program Design, Second Edition 31

JText. Area Java Programming: From Problem Analysis to Program Design, Second Edition 32

JText. Area Java Programming: From Problem Analysis to Program Design, Second Edition 33

JText. Area Example Java Programming: From Problem Analysis to Program Design, Second Edition 34

JCheck. Box s User selects from predefined values. s Example of a toggle button. s Clicking JCheck. Box generates item event. s Use interface Item. Listener and its abstract method item. State. Changed to handle event. Java Programming: From Problem Analysis to Program Design, Second Edition 35

JCheck. Box Java Programming: From Problem Analysis to Program Design, Second Edition 36

Constructors and Methods of class JCheck. Box Java Programming: From Problem Analysis to Program Design, Second Edition 37

Constructors and Methods of class JCheck. Box Java Programming: From Problem Analysis to Program Design, Second Edition 38

JRadio. Button s Created same way as check boxes. s Placed in content pane of applet. s Forces user to select only one radion button at a time. s You create a button group to group radio buttons. s Generates an Item. Event. s interface Item. Listener and method item. State. Changed used to handle events. Java Programming: From Problem Analysis to Program Design, Second Edition 39

JRadio. Button Java Programming: From Problem Analysis to Program Design, Second Edition 40

JRadio. Button Java Programming: From Problem Analysis to Program Design, Second Edition 41

JCombo. Box s Commonly known as a drop-down list. s Used to select an item from a list of possibilities. s Generates an Item. Event. s Event monitored by Item. Listener. s Item. Listener invokes method item. State. Changed. Java Programming: From Problem Analysis to Program Design, Second Edition 42

JCombo. Box Java Programming: From Problem Analysis to Program Design, Second Edition 43

JCombo. Box Java Programming: From Problem Analysis to Program Design, Second Edition 44

JList Java Programming: From Problem Analysis to Program Design, Second Edition 45

JList Java Programming: From Problem Analysis to Program Design, Second Edition 46

Layout Managers s Flow. Layout s Default layout manager. s Places components from left to right until no more items can be placed. s Can align each line left, center, or right. s Default alignment: LEFT. s Grid. Layout s Similar to Flow. Layout. s All rows (columns) have same number of components. s All components have the same size. Java Programming: From Problem Analysis to Program Design, Second Edition 47

Layout Managers s Border. Layout s Items placed into one of five specific regions: s NORTH/SOUTH s EAST/WEST s CENTER s NORTH and SOUTH components extend horizontally (completely span one edge to the other). s EAST and WEST components extend vertically between components in NORTH and SOUTH regions. s CENTER component expands to occupy any unused regions. Java Programming: From Problem Analysis to Program Design, Second Edition 48

Menus s Allow for various functions without cluttering GUI with too many components. s Can be attached to objects such as JFrame and JApplet (set. JMenu. Bar method). s To set a menu bar: private JMenu. Bar menu. MB = new JMenu. Bar(); set. JMenu. Bar(menu. MB); s Add menus to menu bar; add menu items to menu. s Order of menus added = order of appearance. Java Programming: From Problem Analysis to Program Design, Second Edition 49

Keyboard and Mouse Events Java Programming: From Problem Analysis to Program Design, Second Edition 50

Chapter Summary s Creating applets s class Font s class Graphics s class Color s Differences between applets and GUI applications s Converting GUI applications to applets Java Programming: From Problem Analysis to Program Design, Second Edition 51

Chapter Summary s GUI components: s JText. Area s JCheck. Box s JRadio. Button s Layout managers s Menus s Keyboard and mouse events Java Programming: From Problem Analysis to Program Design, Second Edition 52