CSCI E-70
Harvard University Extension School
Spring 2003
Graphical User Interface Programming in Java
Lecture notes 5
26 Feb 2003
- Threads
- Definition and Motivation
- execution context
- independent stack
- location in code (program counter)
- may be more fine grained than you think
- Java Classes and Interfaces
- java.lang.Thread object
- java.lang.Runnable interface
- subclass thread or use Runnable?
- using
- start
- ending: flagging, interrupting, joining
- timing: sleep, getting System time
- Image buffering
- Definition and Motivation
- paint onto off-screen (in-memory only) region
- reusable for later – faster performance
- free up AWT thread – better interactivity
- Java Classes and Interfaces
- java.awt.image.BufferedImage
- usual suspects: Graphics2D, AffineTransform
- Design Issues
- Textual refactoring
- Life Demo Button subclassing
- Differentiating & Layering models
- identifying orthogonal models
- JList: selection versus actual items
- more Swing examples to come (JTree/Table)
- CellLife/Map: display versus editing/controlling
- identifying subordinate relationships
- master model(s) contain(s) full application state
- generally built at program start-up
- subordinate Swing models fed by masters
- proxy object that delegates
- master model implements multiple interfaces
- synchronization made easy
- event propagates up to proper model
- automatically fans out and down to all listeners
- the more complex the app, the bigger the model hierarchy
- Semantic events
- Creating/deriving event class
- Creating/deriving listener interfaces
- Interested party registers self in constructor
- Often needs model for queries anyway
- caller can’t forget: must pass model in constructor
- Creating/Propagating events
- Mouse events
- In-place
- MouseListener methods: click, press, release
- MouseEvent properties: x, y, source
- MouseMotionEvent: move, drag
- handling state
- mapping to your objects
- Javadoc
- class level
- function level
- tags
- running javadoc
- official spec
- UML
- description/motivation
- language-independent software system specification
- visual rep good for same reason GUIs are good: visual!
- although: some people more verbal, may need written spec
- scales well: can describe system at any level of detail
- survives as programmers, languages, management change
- actively researched & industry-suppprted language
- the right drawing board: not a blank one
- check implementation changes against UML: consistent?
- avoid going back to square one when module design fails
- good first step
- make project managers happy (they love diagrams)
- can auto-generate skeletal code from sufficiently detailed UML
- “official” specification
- take with grain of salt: develop what makes sense to you/your team
- I don’t care about precise iconography (open arrows/opaque arrows)
- make a good legend, use consistent conventions
- inheritance diagrams: class level analysis
- also called Class Diagrams (type I of Static Structure Diagrams)
- who extends/ implements whom
- determinable from class declarations only
- what are the “things” in my code that get operated on?
- entity diagrams: instance-level analysis
- also called Object Diagrams (type II of Static Structure Diagrams)
- largely determined by instance variable declarations, method headers
- who creates whom
- who contains whom: composition/aggregation
- multiplicity: how many instances of Y per X
- 1: X may be entire app (i.e. Y is a singleton)
- 1: X may be a node (i.e. Y is a property)
- many (0..*): X may be a growable list (i.e. Y is a link)
- possibly: who communicates with whom (esp. at network nodes level)
- flow diagrams: method level analysis
- also called Sequence Diagrams
- determined by method bodies: low level
- hence: must be selective about salient flows
- nested for-loop that paints grid from model too obvious
- focus on runtime propagations through more than 2 methods
- flow should be a represent a semantic event: user visible
- shows “how it works”
- event handling
- trace user input to stable result
- trace network or timer event to update
- can abstract out underlying libraries’ flows
- format like flow chart but exclude non-method call logic
- flowchart: statement level analysis
- also called deployment diagrams (type II of Implementation Diagrams)
- determined by method statements: lowest level
- use to explicate complex algorithms
- show every state variable update
- show every decision point
- essentially diagrammatic pseudocode
- some find visual organization helpful, others don’t
Find-Best Pseudocode
Base Case 1:failure (or loop prevention)
point already in this traversal → RETURN
Base Case 2:success (or possible solution)
if this solution better than best known solution:
update best known rating (rating of this one)
update best known path (this path; careful to clone it)
RETURN
Recursive Case:
mark this point as traversed
for every point in reachable neighbors:
try adding point to path
update the current cost of path
RECURSE on that point
perform backtracking clean-up:
subtract the cost
remove the point
unmark this point (allow other superior traversals to go through it)
– Page 1 of 6 –