Chapter 12 Advanced GUIs and Graphics

Similar documents
Graphical User Interface (GUI) and Object- Oriented Design (OOD)

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Sri Vidya College of Engineering & Technology

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Programming graphics

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Part 3: Graphical User Interface (GUI) & Java Applets

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Introduction to the JAVA UI classes Advanced HCI IAT351

CS 251 Intermediate Programming GUIs: Components and Layout

Java Programming Lecture 6

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

Unit 6: Graphical User Interface

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Swing. By Iqtidar Ali

Window Interfaces Using Swing Objects

Chapter 3 - Introduction to Java Applets

Contents Chapter 1 Introduction to Programming and the Java Language

Computer Science 210: Data Structures. Intro to Java Graphics

Graphic User Interfaces. - GUI concepts - Swing - AWT

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Chapter 6: Graphical User Interfaces

BASICS OF GRAPHICAL APPS

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

CS111: PROGRAMMING LANGUAGE II

Graphical User Interfaces. Comp 152

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

2110: GUIS: Graphical User Interfaces

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

Window Interfaces Using Swing Objects

JFrame In Swing, a JFrame is similar to a window in your operating system

Graphical User Interfaces (GUIs)

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Chapter 7: A First Look at GUI Applications

Chapter 17 Creating User Interfaces

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Summary Chapter 25 GUI Components: Part 2

Systems Programming Graphical User Interfaces

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

GUI in Java TalentHome Solutions

Graphical User Interfaces in Java - SWING

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

GUI, Events and Applets from Applications, Part III

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Layouts and Components Exam

CHAPTER 2. Java Overview

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

Graphical User Interface (GUI)

User interfaces and Swing

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Dr. Hikmat A. M. AbdelJaber

Introduction. Introduction

Java. GUI building with the AWT

Java IDE Programming-I

To gain experience using GUI components and listeners.

Lecture 18 Java Graphics and GUIs

Swing/GUI Cheat Sheet

Applet. 1. init (): called once by the applet containers when an applet is loaded for execution.

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

CSC System Development with Java Introduction to Java Applets Budditha Hettige

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Packages: Putting Classes Together

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Agenda. Container and Component

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16]

Lecture 5: Java Graphics

EVENTS, EVENT SOURCES AND LISTENERS

JAVA. A. Zakiuddin Ahmed. M.Sc.,M.Phil M.S.(Edn), PGDCSM, Professor in Computer Science, P. Rizwan Ahmed. MCA., M.Sc.,M.A., M.Phil.

Graphical User Interfaces

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

CSC 1214: Object-Oriented Programming

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

CSE 331 Software Design & Implementation

Introduction to Computer Science I

CS 3331 Advanced Object-Oriented Programming Final Exam

Java Applets / Flash

Chapter 7 Applets. Answers

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

JAVA NOTES GRAPHICAL USER INTERFACES

PIC 20A GUI with swing

Adding Buttons to StyleOptions.java

Graphical Applications

Transcription:

Chapter 12 Advanced GUIs and Graphics Chapter Objectives Learn about applets Explore the class Graphics Learn about the classfont Explore the classcolor Java Programming: From Problem Analysis to Program Design, 5e 2 1

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, 5e 3 Inheritance Hierarchy of GUI Classes Java Programming: From Problem Analysis to Program Design, 5e 4 2

Constructors and Methods of the class Component Java Programming: From Problem Analysis to Program Design, 5e 5 Constructors and Methods of the classcomponent (continued) Java Programming: From Problem Analysis to Program Design, 5e 6 3

Constructors and Methods of the classcomponent (continued) Java Programming: From Problem Analysis to Program Design, 5e 7 Constructors and Methods of the classcomponent (continued) Java Programming: From Problem Analysis to Program Design, 5e 8 4

Constructors and Methods of the class Container Java Programming: From Problem Analysis to Program Design, 5e 9 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 classjapplet contained in package javax.swing Java Programming: From Problem Analysis to Program Design, 5e 10 5

Members of class JApplet Java Programming: From Problem Analysis to Program Design, 5e 11 Members of class Japplet (continued) Java Programming: From Problem Analysis to Program Design, 5e 12 6

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, 5e 13 init method Applet Methods Initializes variables Gets data from user Places various GUI components paint method Performs output Java Programming: From Problem Analysis to Program Design, 5e 14 7

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, 5e 15 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, 5e 16 8

HTML to Run Applet Java Programming: From Problem Analysis to Program Design, 5e 17 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, 5e 18 9

Constructors and Methods of the class Font Java Programming: From Problem Analysis to Program Design, 5e 19 Constructors and Methods of the class Font (continued) Java Programming: From Problem Analysis to Program Design, 5e 20 10

Constructors and Methods of the class Font (continued) Java Programming: From Problem Analysis to Program Design, 5e 21 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, 5e 22 11

Constructors of the class Color Java Programming: From Problem Analysis to Program Design, 5e 23 Constructors of the class Color (continued) Java Programming: From Problem Analysis to Program Design, 5e 24 12

Constants Defined in the class Color Java Programming: From Problem Analysis to Program Design, 5e 25 Constants Defined in the class Color (continued) Java Programming: From Problem Analysis to Program Design, 5e 26 13

Constants Defined in the class Color (continued) Java Programming: From Problem Analysis to Program Design, 5e 27 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, 5e 28 14

Constructors and Methods of the class Graphics Java Programming: From Problem Analysis to Program Design, 5e 29 Constructors and Methods of the classgraphics (continued) Java Programming: From Problem Analysis to Program Design, 5e 30 15

Constructors and Methods of the classgraphics (continued) Java Programming: From Problem Analysis to Program Design, 5e 31 Constructors and Methods of the classgraphics (continued) Java Programming: From Problem Analysis to Program Design, 5e 32 16

Constructors and Methods of the classgraphics (continued) Java Programming: From Problem Analysis to Program Design, 5e 33 Constructors and Methods of the classgraphics (continued) Java Programming: From Problem Analysis to Program Design, 5e 34 17

Constructors and Methods of the classgraphics (continued) Java Programming: From Problem Analysis to Program Design, 5e 35 Constructors and Methods of the classgraphics (continued) Java Programming: From Problem Analysis to Program Design, 5e 36 18

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, 5e 37 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, 5e 38 19

Additional GUI Components JTextArea JCheckBox JRadioButton JComboBox JList Java Programming: From Problem Analysis to Program Design, 5e 39 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, 5e 40 20

JTextArea (continued) Java Programming: From Problem Analysis to Program Design, 5e 41 Methods Inherited by class JTextArea from Parent class JTextComponent Java Programming: From Problem Analysis to Program Design, 5e 42 21

JTextArea Example Java Programming: From Problem Analysis to Program Design, 5e 43 JCheckBox User selects from predefined values Example of a toggle button Clicking JCheckBox generates item event Use interfaceitemlistener and its abstract method itemstatechanged to handle event Java Programming: From Problem Analysis to Program Design, 5e 44 22

Constructors and Methods of class JCheckBox Java Programming: From Problem Analysis to Program Design, 5e 45 Constructors and Methods of class JCheckBox (continued) Java Programming: From Problem Analysis to Program Design, 5e 46 23

Constructors and Methods of class JCheckBox (continued) Java Programming: From Problem Analysis to Program Design, 5e 47 Constructors and Methods of class JCheckBox (continued) Java Programming: From Problem Analysis to Program Design, 5e 48 24

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 interfaceitemlistener and method itemstatechanged used to handle events Java Programming: From Problem Analysis to Program Design, 5e 49 JRadioButton (continued) Java Programming: From Problem Analysis to Program Design, 5e 50 25

JRadioButton (continued) Java Programming: From Problem Analysis to Program Design, 5e 51 JRadioButton (continued) Java Programming: From Problem Analysis to Program Design, 5e 52 26

JRadioButton (continued) Java Programming: From Problem Analysis to Program Design, 5e 53 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, 5e 54 27

Constructors of class JComboBox Java Programming: From Problem Analysis to Program Design, 5e 55 Applet with JCheckBox, JComboBox, and JRadioButton Java Programming: From Problem Analysis to Program Design, 5e 56 28

Constructors of class JList Java Programming: From Problem Analysis to Program Design, 5e 57 Constructors of class Jlist (continued) Java Programming: From Problem Analysis to Program Design, 5e 58 29

Constructors of class Jlist (continued) Java Programming: From Problem Analysis to Program Design, 5e 59 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, 5e 60 30

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, 5e 61 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, 5e 62 31

Key and Mouse Events Java Programming: From Problem Analysis to Program Design, 5e 63 Creating applets class Font Chapter Summary class Graphics class Color Differences between applet and GUI application Converting GUI application to applet Java Programming: From Problem Analysis to Program Design, 5e 64 32

Chapter Summary (continued) GUI components JTextArea JCheckBox JRadioButton Layout managers Menus Key and mouse events Java Programming: From Problem Analysis to Program Design, 5e 65 33