Graphical User Interface (GUI)

Similar documents
Graphical User Interface (GUI)

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!

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

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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

Graphical User Interfaces (GUIs)

Java Programming Lecture 6

CS 251 Intermediate Programming GUIs: Components and Layout

Java IDE Programming-I

Jonathan Aldrich Charlie Garrod

Introduction to concurrency and GUIs

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Graphical User Interfaces. Comp 152

Java: Graphical User Interfaces (GUI)

Introduction to the JAVA UI classes Advanced HCI IAT351

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

Programming graphics

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Window Interfaces Using Swing Objects

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Window Interfaces Using Swing Objects

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Computer Science 210: Data Structures. Intro to Java Graphics

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

UI Software Organization

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

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

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

Class 16: The Swing Event Model

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

CS 251 Intermediate Programming GUIs: Event Listeners

Graphical User Interface (GUI)

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

AP CS Unit 11: Graphics and Events

G51PRG: Introduction to Programming Second semester Applets and graphics

CSC207 Week 4. Larry Zhang

Visit for more.

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Windows and Events. created originally by Brian Bailey

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

Swing. By Iqtidar Ali

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

(Incomplete) History of GUIs

Human-Computer Interaction IS4300

Sri Vidya College of Engineering & Technology

Programming Languages and Techniques (CIS120)

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Packages: Putting Classes Together

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

GUI Software Architecture

Agenda. Container and Component

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

TYPES OF INTERACTORS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill

Programming Languages and Techniques (CIS120e)

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

Swing from A to Z Some Simple Components. Preface

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

Chapter 17 Creating User Interfaces

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

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

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

Graphical Applications

Lecture 5: Java Graphics

CSE 331 Software Design & Implementation

2IS45 Programming

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

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

GUI Components: Part 1

Systems Programming Graphical User Interfaces

User interfaces and Swing

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Chapter 12 Advanced GUIs and Graphics

GUI Event Handlers (Part I)

Lecture 9. Lecture

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

1.00 Lecture 14. Lecture Preview

Introduction to Computer Science I

CS 4300 Computer Graphics

Class 14: Introduction to the Swing Toolkit

Lecture 18 Java Graphics and GUIs

Java Applets This is not a Java course! (You re supposed to know about Java.)

Charlie Garrod Michael Hilton

COMPSCI 230. Software Design and Construction. Swing

WIMP Elements. GUI goo. What is WIMP?

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

CSE 331 Software Design & Implementation

Lecture 3: Java Graphics & Events

Introduction. Introduction

Graphical User Interfaces

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Part I: Learn Common Graphics Components

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

SINGLE EVENT HANDLING

CS 2113 Software Engineering

Programming Language Concepts: Lecture 8

Transcription:

Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1

Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware and software of different machines has different GUI capabilities Different operating systems implement the same GUI concepts differently 2

Abstract Window Toolkit (AWT) First released in 1995 with Sun s first Java release Thin abstract layer between the Java view of a user interface and the underlying hardware/software For instance AWT checkbox: maps to Windows checkbox on a Windows machine, maps to X checkbox on Unix machine Simple, but not hardware independent 3

Swing Built on top of AWT Coded in 1997 (Released Java 1.2) Name derived from code name because Sun programmers liked swing music Deeper interface less dependent on machine / operating system More complicated (18 packages, 737 classes as of JDK 8) 4

Example of Swing Class Hierarchy 5

JavaFX Enhancement(?) / Replacement for Swing Makes the creation of a GUI simpler Requires a GUI building tool Installation of a 3 rd party tool with different release levels, interactions with Eclipse, etc. Hides the details of the GUI from the coder Removes capability Can override the GUI builder with Swing to get that capability back if you know Swing Use if you want, but we teach Swing 6

Graphics Basics Your screen is a two dimensional array of pixels Think of each pixel as having an (x,y) coordinate The upper left hand side of the screen is (0,0) The X axis increases from left to right The Y axis increases from top to bottom 7

Binghamton (0,0) X (1920,0) Y (0,1080) 8

Single Pixel Each pixel is described by three numbers 0-255 Amount of Red Amount of Green Amount of Blue Higher numbers are brighter Black is 0,0,0 White is 255,255,255 9

Swing s Job Enable the programmer to describe what should go on the screen In some relatively high level, abstract way Translate the programmer description to pixels (render) Enable the user to interact with the screen Select an active window Move the graphical (mouse) cursor in that window Mouse button clicks and wheel motion Touch screen motions Keyboard key-press Enable the programmer to define responses to user actions 10

Swing Strategy Divide and Conquer Everything on the screen can be thought of as a rectangular area on the screen called a Component JComponent is an abstract class in Swing JComponent class manages things like: rendering the component Handle drag/drop Support key bindings Manage the application wide look and feel of the component Components may contain sub-components! 11

Swing Components in Containers Container is an abstract class which extends Component You can add or remove components to/from a container You can find the parent of a component (direct container) Swing will Lay out the contained component Containers have layout managers (more later) Components can give hints about how they should be laid out preferred, minimum, maximum size Alignment vs other in the X and Y directions Orientation See: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html 12

Top Level Components Designed to interact with underlying hardware, operating system, graphics packages, and application software Top level components are sub-classes of Container To create a new application window, use JFrame To create a sub-window (e.g. popup), use JDialog To create a sub-window within (say) a browser, use Japplet No border or title bar 13

Class Hierarchy (so far) Component Container JFrame JDialog JApplet 14

Class Hierarchy (in reality) awt.component awt.container awt.window awt.panel awt.frame awt.dialog applet.applet swing.jframe swing.jdialog swing.japplet 15

Text Based Components Like JLabel (simplest) String text field Rendered by bitmapping each character A font is an array of bitmaps - one for each Unicode character Different fonts look different Includes letter size and style e.g. italic, bold, etc. Fonts do not specify color of background or foreground Bit on implies foreground Bit off implies background 16

Investigating Rendering Swing renders or paints a component when requested to System Requests When the component is first made visible on the screen When the component is resized When the component was damaged and needs repair e.g. uncovered Program Requests The program changed the component and wants the user to see the change The component has a paint(graphics g) method This is invoked by swing whenever rendering is required Override the paint method to change how a component is rendered Don t invoke the paint method! Swing does that for you (repaint instead) 17

What s in a paint method? Paint uses the Graphics object passed as a parameter Graphics has methods to draw and fill things on the screen void drawline(intx1,int y1,int x2, int y2) void fillrect(int x,int y,int width, int height) void drawstring(string str, int x, int y) void drawimage(image img, int x, int y, ) Most of the time, we don t need to override paint the Swing classes already do that for us 18

Common Look and Feel Common look and feel information kept in the Graphics object Transform from user coordinates to pixel X and Y Clipping coordinates Boundaries Background/Foreground Colors Font Component has methods to modify this state information Unless you change something, you get your Container s state 19

The Event Loop GUI s are managed by an endless loop called the event loop Within this loop, Swing checks for user input If an event occurs, tasks are dispatched Events are user input, timer events, system events, etc The dispatched tasks can: Read the user input Update the screen Do calculations Dispatch future events Optionally exit the loop 20

Runnable Interface Requires a single method void run(); Used to indicate a class than can be run i.e. that has a run method that can be invoked Note that run takes no arguments, and returns no result. We don t care how run works, what it does, or whether it worked we just know we can run it until it finishes. 21

Conceptual View of the Event Loop while(1) { if (eventque.hasdata()) { newevent=eventque.getfirst(); newevent.run(); if (eventrequestedexit) break; } if (newevent=getevent()) eventque.addlast(newevent); } 22

BootStrapping the even loop class MyGui implements Runnable { public void run() { // Create and show GUI } } public static void main(string[] args) { MyGui gui = new MyGui(); javax.swing.swingutilities.invokelater(gui)); } 23

invokelater invokelater is a static method in the SwingUtilities class Adds it s argument to the end of the AWT event list Starts the AWT event loop All events in the AWT event loop must be Runnable When the AWT event handler finds an object on its event list, it invokes object.run() 24

Using Swing directly If a Swing component does everything you want it to, use it as it stands For instance, in helloworld, all we want to do is put up a window that says hello. We can use JFrame to create a frame, and Jlabel to create a label No extra work required 25

Common Swing Widgets JFrame top level frame JPane A rectangular container used for separate parts of a frame JButton a push button JCheckBox On/Off check box JRadioButton one of a set of buttons in which only one is on JComboBox Text field with choice pull-down JDialog Ask a pop-up question, get an answer and pop-down JTextField User editable text field and many more 26

Common Swing Widgets JFrame JButton JPanel JCheckBox JRadioButton JTextField 27

Extending Swing If we want to extend the capabilities of Swing components, we can create child classes Override the methods we want to change For instance, add a shape to a label See MyCircle.java See CircleDemo.java 28

Dynamic GUI s 29

Dynamic GUIs So far, our GUI s have been pretty static How do we React to a keypress or button press? Alter the GUI? Do more stuff? How do we prevent getting inundated by actions? Moving a cursor may generate thousands of (x,y) cursor positions Just grabbing a window and moving it can alter The window that s moving Each window it moves over 30

The CallBack Concept Joe: Hi there Jim Jim: Hi Joe Joe: How is the beach today seen any dolphins yet? Jim: Not yet but the sun is out and they should be here soon Joe: Hey, give me a call back when you see a dolphin! Jim: Sure thing talk later! [Click] [ Some time goes by ] [Rrring] Jim: Hey Joe, I just saw three dolphins swim by! 31

Callbacks Tell a library function you want a callback Terminology: Registering a callback Agreement on what triggers invocation of a callback function Agreement on what arguments are passed to the callback function Agreement on interpretation of returned value from callback 32

Swing Callback Implementation Components with actions have an ActionListener list ActionListener : an object which implements ActionListener requires the void actionperformed(actionevent e) method addactionlistenter(x) adds x to the ActionListener list x must implement ActionListener Registers a callback to x when an action occurs When an action occurs x.actionperformed(event) is invoked for each x in the ActionListener list 33

The ActionEvent Class Whenever an action (like a button press) occurs, Swing generates an ActionEvent The ActionEvent has information about what action occurred, and at what time An ActionEvent object is passed to an action listener via an argument to the actionperformed method Use this argument to determine which action occurred So a single actionperformed method can handle multiple buttons 34

The ActionCommand String Each action object has an Action Command String The Action Command String defaults to something useful E.g. for buttons, defaults to the label of the button Change the action command string using setactioncommand(x) The action command string is copied to an actionevent In your actionperformed method, manage actions by checking e.getactioncommand() Enables mapping buttons to actions (1->many or many->1) 35

Managing Actions from a Class Add implements ActionListener to your class Add actionperformed method to your class public void actionperformed(actionevent e) { } For each button: Invoke button.setactioncommand( <commandstring> ) Invoke button.addactionlistener(this) For each action command string: Add: if (e.getactioncommand().equals( commandstring )) { } to your actionperformed method 36

Action Processing 1. User clicks on button 2. Triggers an event in the event loop 3. Event triggers event processing code in Swing library 4. For each actionlistener, invoke listener.actionperformed(event) 5. You registered as a listener, so your actionperformed method is invoked 6. You check the actioncommand associated with the event to figure out which button called you 7. You perform whatever action is required in response to the button push 37

Example Button Managment See example code in Swing Tutorial under CircleDemo2.java 38

Much More We have barely scratched the surface Many sophisticated widgets in the Swing library Radio Buttons Combo Box Text File Select Menu Bar Popup Menu Progress Bar Scroll bar 39