Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.

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, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©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.
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.
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 --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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,
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
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:

Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition

Java Programming: From Problem Analysis to Program Design, Second Edition2 Chapter Objectives  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 Edition3 Inheritance Hierarchy of GUI Classes

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

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

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

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

Java Programming: From Problem Analysis to Program Design, Second Edition8 Applets  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 is contained in package javax.swing.

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

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

Java Programming: From Problem Analysis to Program Design, Second Edition11 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.

Java Programming: From Problem Analysis to Program Design, Second Edition12 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, Second Edition13 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, Second Edition14 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, Second Edition15 HTML to Run Applet

Java Programming: From Problem Analysis to Program Design, Second Edition16 class Font  Shows text in different fonts.  Contained in package java.awt.  Available fonts:  Serif/Sans Serif  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, Second Edition17 class Font

Java Programming: From Problem Analysis to Program Design, Second Edition18 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, Second Edition19 class Color

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

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

Java Programming: From Problem Analysis to Program Design, Second Edition22 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 areas, fonts, and colors.  Contained in the package java.awt.

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

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

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

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

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

Java Programming: From Problem Analysis to Program Design, Second Edition28 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, Second Edition29 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 and all code associated with it (for example, action listener).

Java Programming: From Problem Analysis to Program Design, Second Edition30 Additional GUI Components  JTextArea  JCheckBox  JRadioButton  JComboBox  JList

Java Programming: From Problem Analysis to Program Design, Second Edition31 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, Second Edition32 JTextArea

Java Programming: From Problem Analysis to Program Design, Second Edition33 JTextArea

Java Programming: From Problem Analysis to Program Design, Second Edition34 JTextArea Example

Java Programming: From Problem Analysis to Program Design, Second Edition35 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, Second Edition36 JCheckBox

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

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

Java Programming: From Problem Analysis to Program Design, Second Edition39 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.

Java Programming: From Problem Analysis to Program Design, Second Edition40 JRadioButton

Java Programming: From Problem Analysis to Program Design, Second Edition41 JRadioButton

Java Programming: From Problem Analysis to Program Design, Second Edition42 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, Second Edition43 JComboBox

Java Programming: From Problem Analysis to Program Design, Second Edition44 JComboBox

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

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

Java Programming: From Problem Analysis to Program Design, Second Edition47 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.

Java Programming: From Problem Analysis to Program Design, Second Edition48 Layout Managers  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, Second Edition49 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, Second Edition50 Keyboard and Mouse Events

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

Java Programming: From Problem Analysis to Program Design, Second Edition52 Chapter Summary  GUI components:  JTextArea  JCheckBox  JRadioButton  Layout managers  Menus  Keyboard and mouse events