54
Chapter 13 Advanced GUIs and Graphics

Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Embed Size (px)

Citation preview

Page 1: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Chapter 13

Advanced GUIs and Graphics

Page 2: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Chapter Objectives

• Learn about applets

• Explore the class Graphics

• Learn about the class Font

• Explore the class Color

Page 3: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Chapter Objectives

• 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: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Inheritance Hierarchy of GUI Classes

Page 5: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of the class Component

Page 6: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of the class Component

Page 7: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of the class Component

Page 8: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of the class Container

Page 9: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 10: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Members of class JApplet

Page 11: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Members of class JApplet

Page 12: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Applets

• 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 13: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Applet Methods

• init Method– Initializes variables– Gets data from user– Places various GUI components

• paint Method– Performs output

Page 14: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Skeleton of a Java Applet

import java.awt.Graphics;import javax.swing.JApplet;

public class WelcomeApplet extends JApplet{

}

Page 15: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 16: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

HTML to Run Applet

Page 17: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 18: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of the class Font

Page 19: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

class Color

• Shows text in different colors

• Changes background color of component

• Contained in package java.awt

Page 20: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors of the class Color

Page 21: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Methods of the class Color

Page 22: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Methods of the class Color

Page 23: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constants Defined in the class Color

Page 24: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 25: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of the class Graphics

Page 26: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods for the class Graphics

Page 27: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods for the class Graphics

Page 28: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods for the class Graphics

Page 29: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 30: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 31: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Additional GUI Components

• JTextArea

• JCheckBox

• JRadioButton

• JComboBox

• JList

Page 32: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 33: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of class JTextArea

Page 34: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Methods Inherited by class JTextArea from Parent class

JTextComponent

Page 35: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

JTextArea Example

Page 36: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 37: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of class JCheckBox

Page 38: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of class JCheckBox

Page 39: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of class JCheckBox

Page 40: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

JRadioButton

• Created same way as check boxes • Placed in content pane of applet• Forces user to select only one radion 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 41: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of class JRadioButton

Page 42: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of class JRadioButton

Page 43: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors and Methods of class JRadioButton

Page 44: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 45: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors of class JComboBox

Page 46: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Applet with JCheckBox, JComboBox, and JRadioButton

Page 47: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Constructors of class JList

Page 48: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Methods of class JList

Page 49: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Layout Managers

• FlowLayout– Default layout manager– Places components from left to right 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 50: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Layout Managers

• BorderLayout– Items placed into one of 5 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 51: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

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 52: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Key and Mouse Events

Page 53: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Chapter Summary

• Creating Applets

• class Font

• class Graphics

• class Color

• Differences Between Applet and GUI Application

• Converting GUI Application to Applet

Page 54: Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class

Chapter Summary

• GUI Components– JTextArea– JCheckBox– JRadioButton

• Layout Managers

• Menus

• Key and Mouse Events