ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems.

Slides:



Advertisements
Similar presentations
Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
Advertisements

The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Event Handling Events and Listeners Timers and Animation.
ISE 582: Web Technology for Industrial Engineering University of Southern California DJE Dept of Industrial and Systems Engineering Lecture 7 JAVA Cup.
ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems.
ISE 582: Web Technology for Industrial Engineering University of Southern California Department of Industrial and Systems Engineering Lecture 5 JAVA Cup.
ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems.
ISE 582: Web Technology for Industrial Engineering University of Southern California DJE Dept of Industrial and Systems Engineering Lecture 9 JAVA Cup.
ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Io package as Java’s basic I/O system continue’d.
Java Implementation Software Construction Lecture 6.
Multiple Choice Solutions True/False a c b e d   T F.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
A Short Course in the Java Programming Language
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
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.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Chapter 19 Designing the GUI front-end: the Model-View-Controller pattern.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
CSE 219 Patterns in Programming More Design Patterns.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
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,
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
The Drawing program – Java Applets
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Session 22 Chapter 11: Implications of Inheritance.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Laboratory Study November, Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
Lesson 28: More on the GUI button, frame and actions.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
UMBC AN HONORS UNIVERSITY IN MARYLAND ON TO JAVA A Short Course in the Java Programming Language.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Observer Pattern Context:
Inheritance ITI1121 Nour El Kadri.
Examples of Classes & Objects
Software Development Java Classes and Methods
Object Oriented Programming using Java - Class Instance Variables
Architectural Patterns for Interactive Software
Anonymous Classes A short-cut for defining classes when you want to create only one object of the class.
Classes & Objects: Examples
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
OO Design with Inheritance
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Constructors, GUI’s(Using Swing) and ActionListner
Objects At Their Best— Java's Abstract Window Toolkit
Object-Oriented Programming: Classes, Inheritance, and Interfaces
Object-Oriented Programming: Classes, Inheritance, and Interfaces
Interfaces,Packages and Threads
Presentation transcript:

ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems Engineering Lecture 8 JAVA Cup 7: Model-View Design Winston & Narasimhan: Chapt 41, 42

7 November 2001Information Technology for IE2 GUI Design: Model-View Approach Class Types Relations Among Class Types Turning the Movie Class into an Observable Class Using the Observer A Meter Listener for MouseClicks RECAP

7 November 2001Information Technology for IE3 Class Types Model Classes: domain knowledge View Classes: information-display Observer / Listener (Adapter) Classes: model-view connections Application Class: –Creates model/view/observer/listener instances –Connects model->(observer)->view and view- >(listener)->model –Establish container-component relations among views

7 November 2001Information Technology for IE4 Relations Among Class Types Model View Application

7 November 2001Information Technology for IE5 Model: Movie Interface import java.io.*; public interface MovieInterface { // Setters public abstract void setScript (int i) ; public abstract void setActing (int i) ; public abstract void setDirection (int i) ; // Getters public abstract int getScript () ; public abstract int getActing () ; public abstract int getDirection () ; public abstract String getTitle () ; public abstract String getPoster () ; // Miscellaneous methods public abstract int rating () ; public abstract void changed () ; }

7 November 2001Information Technology for IE6 Making Movies Observable import java.util.*; public class Movie extends Observable implements MovieInterface { // Define instance variables: String title, poster; int script=5, acting=5, direction=5 // Define 3-parameter constructor: Movie (int s, int a, int d) // Define 4-parameter constructor: Movie (int s, int a, int d, String t) // Define 5-parameter constructor: Movie (int s, int a, int d, String t, String p) // Define setters: setScript, setActing, setDirection // Define getters: getScript, getActing, getDirection, getTitle, getPoster // Define rating method // Define changed method }

7 November 2001Information Technology for IE7 Notifying Observers Blah blah blah… // Define setters and getters: public void setScript(int s) { script = s; setChanged(); notifyObservers(); } etc. … Blah blah blah… // changed method defined here public void changed () { setChanged(); notifyObservers(); } Blah blah blah… Activates connected observers and reset notification apparatus Establishes that a change worthy of note has occurred

7 November 2001Information Technology for IE8 Professional Flourish Blah blah blah… private static int MIN=0, MAX=10; Blah blah blah… // changed method defined here public void setScript (int s) { if (script != s) { script = Math.max( Math.min(s,MAX), MIN ); setChanged(); notifyObservers; } Blah blah blah…

7 November 2001Information Technology for IE9 Creating Movie in the M.App Blah blah blah … public void setMeter (Meter m) {meter = m;} public Meter getMeter() {return meter;} public void setMovie (Movie m) {movie = m;} public Movie getMovie() {return movie;} public static void main (String argv []) { new MovieApplication("Movie Application"); } private Meter meter; private Movie movie; public MovieApplication(String title) { super(title); setMeter(new Meter(0, 30)); setMovie(new Movie (5, 5, 5, "On to Java")); getContentPane().add("Center", getMeter()); addWindowListener(new LocalWindowListener()); setSize(300, 100); show(); } etc… Application

7 November 2001Information Technology for IE10 On Demand Construction Blah blah blah … public void setMeter (Meter m) {meter = m;} public Meter getMeter() {if (meter==null) {setMeter(new Meter(0,30));} return meter;} public void setMovie (Movie m) {movie = m;} public Movie getMovie() {if (movie==null) {setMovie(new Movie (10,10,10,”Title”);} return movie;} public static void main (String argv []) { new MovieApplication("Movie Application"); } private Meter meter; private Movie movie; public MovieApplication(String title) { super(title); setMeter(new Meter(0, 30)); setMovie(new Movie (5, 5, 5, "On to Java")); getContentPane().add("Center", getMeter()); addWindowListener(new LocalWindowListener()); setSize(300, 100); show(); } etc… Application

7 November 2001Information Technology for IE11 Using the Observer Define an Observer class that implements the Observer interface Embed it in the MovieApplication class as a private local class Connect an Observer to a model using addObserver method Do the connecting in the setMovie method to ensure correct correspondence

7 November 2001Information Technology for IE12 The Local Observer Class Application Blah blah blah … public void setMovie (Movie m) { if(movie == m) {return;} if(movie instanceof Movie) {movie.deleteObservers();} if(m instanceof Movie) { movie = m; movie.addObserver(new LocalMovieObserver()); movie.changed(); }} private class LocalMovieObserver implements Observer { public void update (Observable observable, Object object) { getMeter().setValue(getMovie().rating()); getMeter().setTitle(getMovie().getTitle()); }}

7 November 2001Information Technology for IE13 A Listener for the Meter Application Blah blah blah … public void setMeter (Meter m) { meter = m; meter.addMouseListener(new LocalMeterListener()); } Blah blah blah … private class LocalMeterListener extends MouseAdapter { public void mouseClicked (MouseEvent e) { int x = e.getX(); int y = e.getY(); int v = (int) Math.round(getMeter().getValueAtCoordinates (x, y) / 3.0); getMovie().setScript(v); getMovie().setActing(v); getMovie().setDirection(v); }} Blah blah blah …

7 November 2001Information Technology for IE14 On MouseClick on Meter getX, getY, getValueAtCoordinates setScript, setChanged, notifyObservers setActing, setChanged, notifyObservers setDirection, setChanged, notifyObservers Update rating, setValue, repaint, paint Application MovieObserverListenerMeter

7 November 2001Information Technology for IE15 Standalone Observers / Listeners Goal: move Observers / Listeners into independent files Changes: –Establish a MovieApplication instance variable in the class –Include the application instance as an argument to the constructor –Assign the application instance variable in the constructor

7 November 2001Information Technology for IE16 Example: Old format import java.util.*; public class MovieObserver implements Observer { public void update (Observable observable, Object object) { getMeter().setValue(getMovie().rating()); // BUG! getMeter().setTitle(getMovie().getTitle()); // BUG! }

7 November 2001Information Technology for IE17 Example: New Format import java.util.*; public class MovieObserver implements Observer { private MovieApplication applet; public MovieObserver (MovieApplication a) { applet = a; } public void update (Observable observable, Object object) { applet.getMeter().setValue(applet.getMovie().rating()); applet.getMeter().setTitle(applet.getMovie().getTitle()); } In setMovie: movie.addObserver(new MovieObserver(this));

7 November 2001Information Technology for IE18 Similar Changes in MeterListener import java.awt.event.*; public class MeterListener extends MouseAdapter { MovieApplication applet; public MeterListener (MovieApplication a) { applet = a; } public void mouseClicked (MouseEvent e) { int x = e.getX(); int y = e.getY(); int v = (int) Math.round(applet.getMeter().getValueAtCoordinates (x, y) / 3.0 ); applet.getMovie().setScript(v); applet.getMovie().setActing(v); applet.getMovie().setDirection(v); }} In setMeter: meter.addMouseListener(new MeterListener(this));