Software Design 9.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, should be easy to use and.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Introduction to Java 2 Programming
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Event Handling Events and Listeners Timers and Animation.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Object-Oriented Analysis and Design
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.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
GUI Programming in Java
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Software Construction Lecture 10 Frameworks
Welcome to CIS 083 ! Events CIS 068.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
Applets and Frames CS 21a: Introduction to Computing I First Semester,
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Software Design 4.1 Tell, Don't Ask l Tell objects what you want them to do, do not ask questions about state, make a decision, then tell them what to.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Field Trip #19 Animations with Java By Keith Lynn.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Layout Managers Arranges and lays out the GUI components on a container.
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 Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
CPS 108/ola.1 From XP to javax.swing.* l What parts of embracing change can we embrace in 108?  Evolutionary design, small releases, iterative enhancement.
Software Design 5.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, but easy to use and hide.
UID – Event Handling and Listeners Boriana Koleva
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Project JETT/Duke 1 javax.swing, events, and GUIs l GUI programming requires processing events  There’s no visible loop in the program  Wire up/connect.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Chapter 9: Graphical User Interfaces
A First Look at GUI Applications
Lecture 27 Creating Custom GUIs
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
MVC overview Model, View, Controller is MVC
Presentation transcript:

Software Design 9.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, should be easy to use and hide l We're talking about software design  Not HCI or user-interface design or human factors…  However, compare winamp to iTunes l How do we design GUIs  Programming, drag-and-drop, …  How do we program/connect GUIs?

Software Design 9.2 javax.swing, events, and GUIs l GUI programming requires processing events  There’s no visible loop in the program  Wire up/connect widgets, some generate events, some process events Pressing this button causes the following to happen  We want to do practice “safe wiring”, meaning? l We need to put widgets together, what makes a good user-interface? What makes a bad user-interface?  How do we lay widgets out, by hand? Using a GUI- builder? Using a layout manager? l How do we cope with widget complexity?

Software Design 9.3 JComponent/Container/Component l The java.awt package was the original widget package, it persists as parent package/classes of javax.swing widgets  Most widgets are JComponents (subclasses), to be used they must be placed in a Container The former is a swing widget, the latter awt, why? l A Container is also a Component, but not all Containers are JComponents (what?)  JFrame is often the “big container” that holds all the GUI widgets, we’ll use this and JApplet (awt counterparts are Frame and Applet)  A Jpanel is a JComponent that is also a Container Holds JComponents, for example and is holdable as well

Software Design 9.4 What do Containers do? l A Container is a Component, so it’s possible for a Container to “hold itself”? Where have we seen this? “You want to represent part-whole hierarchies of objects. You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly”. l Composite pattern solves the problem. Think tree, linked list: leaf, composite, component  What about parent references?  What about child references? l In java, a parent is responsible for painting its children  For “paint” think draw, arrange, manage, …

Software Design 9.5 Widget layout l A Layout Manager “decides” how widgets are arranged in a Container  In a JFrame, we use the ContentPane for holding widgets/components, not the JFrame itself  Strategy pattern: “related classes only differ in behavior, configure a class with different behaviors… you need variants of an algorithm reflecting different constraints…context forwards requests to strategy, clients create strategy for the context” Context == JFrame/container, Strategy == Layout l Layouts: Border, Flow, Grid, GridBag, Spring, …  I’ll use Border, Flow, Grid in my code

Software Design 9.6 BorderLayout (see Browser.java) l Default for the JFrame contentpane l Provides four areas, center is “main area” for resizing l Recursively nest for building complex (yet simple) GUIs BorderLayout.CENTER for adding components  Some code uses “center”, bad idea (bugs?) CENTER NORTH SOUTH WESTWEST EASTEAST

Software Design 9.7 Action and other events l Widgets generate events, these events are processed by event listeners  Different types of events for different scenarios: press button, release button, drag mouse, press mouse button, release mouse button, edit text in field, check radio button, …  Some widgets “fire” events, some widgets “listen” for events l To process events, add a listener to the widget, when the widget changes, or fires, its listeners are automatically notified.  Observer/Observable (related to MVC) pattern

Software Design 9.8 Adding Listeners l In lots of code you’ll see that the Container widget is the listener, so pressing a button or selecting a menu is processed by the Container/Frame’s actionPerformed method  All ActionListeners have an actionPerformed method, is this interface/implements or inheritance/extends?  Here’s some “typical” code, why is this bad? void actionPerformed(ActionEvent e) { if (e.getSource() == thisButton) … else if (e.getSource() == thatMenu)… }

Software Design 9.9 A GUI object can be its own client l Occasionally a GUI will be a listener of events it generates  Simple, but not extendable  Inner classes can be the listeners, arguably the GUI is still listening to itself, but … Encapsulating the listeners in separate classes is better l Client (nonGUI) objects cannot access GUI components  Properly encapsulated JTextField, for example, responds to aGui.displayText(), textfield not accessible to clients  If the GUI is its own client, it shouldn’t access textfield Tension: simplicity vs. generality l Don’t wire widgets together directly or via controller that manipulates widgets directly  Eventual trouble when GUI changes

Software Design 9.10 Using inner/anonymous classes l For each action/event that must be processed, create an object to do the processing  Command pattern: parameterize object by an action to perform, queue up requests to be executed at different times, support undo  There is a javax.swing Event Queue for processing events, this is the hidden while loop in event processing GUI programs l The inner class can be named, or it can be created “anonymously”  For reuse in other contexts, sometimes naming helpful  Anonymous classes created close to use, easy to read (arguable to some)

Software Design 9.11 Listeners l Events propagate in a Java GUI as part of the event thread  Don’t manipulate GUI components directly, use the event thread  Listeners/widgets register themselves as interested in particular events Events go only to registered listeners, can be forwarded/consumed ActionListener, KeyListener, ItemListener, MouseListener, MouseMotionListener, …, see java.awt.event.*  Isolate listeners as separate classes, mediators between GUI, Controller, Application  Anonymous classes can help here too

Software Design 9.12 Listeners and Adapters l MouseListener has five methods, KeyListener has three  What if we’re only interested in one, e.g., key pressed or mouse pressed? As interface, we must implement all methods as no-ops As adapter we need only implement what we want l Single inheritance can be an annoyance in this situation  Can only extend one class, be one adapter, … l What about click/key modifiers, e.g., shift/control/left/both  Platform independent, what about Mac?

Software Design 9.13 From Browser/Memory to OOGA l Can you design a game architecture before designing a game?  Which games should you write, do you need to have full- blown implementations?  What’s enough to start? l Draw (on paper) a picture of the GUI  Generate scenarios, use-cases: what happens when user clicks here, what happens when user enters text in this box, what happens when game is over  Rough-out the components, prototype a GUI  Connect some/all components, make it possible to refactor l It’s hard to make a framework without making a frame

Software Design 9.14 MVC overview l Model, View, Controller is MVC  Model stores and updates state of application Example: calculator, what's the state of a GUI-calculator?  When model changes it notifies its views Example: pressing a button on calculator, what happens?  The controller interprets commands, forwards them appropriately to model (usually not to view) Example: code for calculator that reacts to button presses  Controller isn't always a separate class, often part of GUI-based view in M/VC l MVC is a fundamental design pattern : solution to a problem at a general level, not specific code per se

Software Design 9.15 MVC in in the jpuzzle suite l Sliding puzzle game allows users to move pieces near a blank piece to recreate an original image  See PuzzleGui, PuzzleController, PuzzleView, PuzzleModel, PuzzleApplet, PuzzleMove l The model “knows” the location of the pieces  Determines if move is legal  Makes a move (records it) and updates views  Supports move undo l View shows a board and information, e.g., undo possible  See PuzzleView interface, implemented by application and applet

Software Design 9.16 Puzzle MVC: Controller perspective l In this example, the PuzzleController is a middleman, all communication between views and model via controller  Sometimes a middleman class isn’t a good idea, extra layer of code that might not be needed  Often in MVC model communicates with multiple views, but communication to model via controller l In this example one controller holds all the views and executes commands in all views on behalf of model  Model only calls showBoard in controller  Some of the “intelligence” is in controller, arguably should be in model l Controller is a candidate for refactoring

Software Design 9.17 Controller and Commands l Use-case for making a move:  Move generated, e.g., by button-click  Move forwarded to model (by controller) If move is made, then undo state set to true in views If move is made, then views update board display l Use-case for undoing a move  Undo generated, e.g., by button or menu choice  Undo forwarded to model (by controller) If undo changes board, views will be updated If future undo not possible, undo state set to false l ShowBoard and Undo are both Command classes  Command implemented using hook/template method

Software Design 9.18 Hook method and Template Pattern l Client code (Controller) calls a command’s execute method, all Commands have such a method  Execute always has a view parameter  Execute has optional other parameter for information l The execute method is the same in every Command, forwards to the hook method  Subclasses of ViewCommand implement hook in application specific way Showing board calls appropriate method in view Undo calls appropriate method in view

Software Design 9.19 Lower level JButton particulars l PuzzleGui class has panel/grid of buttons for display  Pressing button causes action (via controller)  Button is displayable, but doesn’t have label If button had label, it would be automatically shown  Instead, use setActionCommand to store command Retrieved by getActionCommand, but not a label  Button has icon, automatically displayed l The Icon interface (swing) implemented by ImageIcon  See PlainPuzzleIcon and ImagePuzzleIcon  An Icon doesn’t typically grow, but in this application we want it to resize when app is resized

Software Design 9.20 What about a “real game” l How to make this a game? What are use cases/scenarios? l Shuffle pieces initially, pick random piece near blank and move it, repeat 5, 10, 20 times [degree of difficulty]  How does this facilitate auto-solve?  What about showing the numbered tiles as a hint when just image puzzle used by client l Auto-complete, we can undo string of moves, track all  Coalesce moves that are redundant  Recognize previous state of board and go back