Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 16 Creating User Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
1 Creating User Interfaces. 2 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
Java Programming Chapter 10 Graphical User Interfaces.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Java Programming: Guided Learning with Early Objects Chapter 8 Applications of Arrays (Sorting and Searching) and Strings.
Java Applets.
Java Applets.
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Chapter 7 Creating User Interfaces
Java Applets.
Java Applets.
Graphical User Interface
Chapter 7-3 (Book Chapter 14)
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Advanced GUIs and Graphics
Presentation transcript:

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

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

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

Java Programming: From Problem Analysis to Program Design, 4e4 Inheritance Hierarchy of GUI Classes

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

Java Programming: From Problem Analysis to Program Design, 4e6 Constructors and Methods of the class Component (continued)

Java Programming: From Problem Analysis to Program Design, 4e7 Constructors and Methods of the class Component (continued)

Java Programming: From Problem Analysis to Program Design, 4e8 Constructors and Methods of the class Component (continued)

Java Programming: From Problem Analysis to Program Design, 4e9 Constructors and Methods of the class Container

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

Java Programming: From Problem Analysis to Program Design, 4e11 Members of class JApplet

Java Programming: From Problem Analysis to Program Design, 4e12 Members of class Japplet (continued)

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

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

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 { }

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 }

Java Programming: From Problem Analysis to Program Design, 4e17 HTML to Run Applet

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)

Java Programming: From Problem Analysis to Program Design, 4e19 Constructors and Methods of the class Font

Java Programming: From Problem Analysis to Program Design, 4e20 Constructors and Methods of the class Font (continued)

Java Programming: From Problem Analysis to Program Design, 4e21 Constructors and Methods of the class Font (continued)

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

Java Programming: From Problem Analysis to Program Design, 4e23 Constructors of the class Color

Java Programming: From Problem Analysis to Program Design, 4e24 Constructors of the class Color (continued)

Java Programming: From Problem Analysis to Program Design, 4e25 Constants Defined in the class Color

Java Programming: From Problem Analysis to Program Design, 4e26 Constants Defined in the class Color (continued)

Java Programming: From Problem Analysis to Program Design, 4e27 Constants Defined in the class Color (continued)

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

Java Programming: From Problem Analysis to Program Design, 4e29 Constructors and Methods of the class Graphics

Java Programming: From Problem Analysis to Program Design, 4e30 Constructors and Methods of the class Graphics (continued)

Java Programming: From Problem Analysis to Program Design, 4e31 Constructors and Methods of the class Graphics (continued)

Java Programming: From Problem Analysis to Program Design, 4e32 Constructors and Methods of the class Graphics (continued)

Java Programming: From Problem Analysis to Program Design, 4e33 Constructors and Methods of the class Graphics (continued)

Java Programming: From Problem Analysis to Program Design, 4e34 Constructors and Methods of the class Graphics (continued)

Java Programming: From Problem Analysis to Program Design, 4e35 Constructors and Methods of the class Graphics (continued)

Java Programming: From Problem Analysis to Program Design, 4e36 Constructors and Methods of the class Graphics (continued)

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

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)

Java Programming: From Problem Analysis to Program Design, 4e39 Additional GUI Components JTextArea JCheckBox JRadioButton JComboBox JList

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

Java Programming: From Problem Analysis to Program Design, 4e41 JTextArea (continued)

Java Programming: From Problem Analysis to Program Design, 4e42 Methods Inherited by class JTextArea from Parent class JTextComponent

Java Programming: From Problem Analysis to Program Design, 4e43 JTextArea Example

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

Java Programming: From Problem Analysis to Program Design, 4e45 Constructors and Methods of class JCheckBox

Java Programming: From Problem Analysis to Program Design, 4e46 Constructors and Methods of class JCheckBox (continued)

Java Programming: From Problem Analysis to Program Design, 4e47 Constructors and Methods of class JCheckBox (continued)

Java Programming: From Problem Analysis to Program Design, 4e48 Constructors and Methods of class JCheckBox (continued)

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

Java Programming: From Problem Analysis to Program Design, 4e50 JRadioButton (continued)

Java Programming: From Problem Analysis to Program Design, 4e51 JRadioButton (continued)

Java Programming: From Problem Analysis to Program Design, 4e52 JRadioButton (continued)

Java Programming: From Problem Analysis to Program Design, 4e53 JRadioButton (continued)

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

Java Programming: From Problem Analysis to Program Design, 4e55 Constructors of class JComboBox

Java Programming: From Problem Analysis to Program Design, 4e56 Applet with JCheckBox, JComboBox, and JRadioButton

Java Programming: From Problem Analysis to Program Design, 4e57 Constructors of class JList

Java Programming: From Problem Analysis to Program Design, 4e58 Constructors of class Jlist (continued)

Java Programming: From Problem Analysis to Program Design, 4e59 Constructors of class Jlist (continued)

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

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

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

Java Programming: From Problem Analysis to Program Design, 4e63 Key and Mouse Events

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

Java Programming: From Problem Analysis to Program Design, 4e65 Chapter Summary (continued) GUI components –JTextArea –JCheckBox –JRadioButton Layout managers Menus Key and mouse events