














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















Week 3
(^) See last week’s slides for how to make it “main” (^) I’ll run it from the command line
(^) Anything special I need to do to run it (^) What platform you developed/tested it on (^) Anything else you think I should know
(^) Must be prepared to respond to events external to your program (^) You don’t know when these might occur (^) They may come from multiple sources (a user, remote users, sensors, hardware devices)
(^) GUIs (responsive to mice, keyboard) (^) Systems that interact with hardware ( interrupts ) (^) Collaborative tools (multiple users, each doing their own thing)
(^) Try to do everything “in line” in one flow of control (^) Works only for trivial problems (^) How would you do an “in line” program that needs to respond to multiple event sources? (^) N.B.: It’s actually possible. In fact, it’s one of the ways that asynchronous programming works “under the hood.” We’ll talk about it later in the semester.
(^) Set-up code, that gets the initial windows on the screen, does initialization, etc. (^) A collection of program chunks that respond to particular types of events that occur
(^) An event is some external occurrence (^) The asynchronously-callable program chunks are event handlers (^) An event dispatcher is the thing that calls your event handlers; it is typically provided by the system (language, library, OS, ...)
(^) These are just functions that will be invoked when an event occurs (^) Typically, they take a predefined set of arguments (what event happened, etc.) (^) They are parts of your program that get called back when something happens
(^) Mouse clicks, movement, release (^) Key presses, releases (^) Combinations of all of the above
(^) button.actionPerformed (^) list.valueChanged
(^) panel.mousePressed (^) panel.mouseReleased (^) panel.mouseClicked
(^) button.actionPerformed ActionEvent (^) source: the widget that generated the event (^) timestamp: when the event occurred (^) modifiers: which keys were held down when the event occurred (^) list.valueChanged ListSelectionEvent (^) firstIndex: first index of changed item (^) lastIndex: last index of changed item (^) To get specific details of any given event type, look at the Java documentation (http://java.sun.com/j2se/1.5.0/docs/api/) or ask me or the TA
(^) Often need to share some data across several related callbacks (^) Often need to keep track of what happened the last time you ran the callback (^) There’s a group of variables and related functions that are used only by the callback
import javax.swing as swing startx = 0 starty = 0 def pressCallback(event): global startx, starty startx = event.x starty = event.y def releaseCallback(event): global startx, starty graphics = event.source.graphics graphics.drawLine(startx, starty, event.x, event.y) if name == "main": frame = swing.JFrame("Simple Drawing Program") canvas = swing.JPanel() canvas.preferredSize = (400, 400) frame.contentPane.add(canvas) frame.pack() frame.show() canvas.mousePressed = pressCallback canvas.mouseReleased = releaseCallback
(^) Keep data as “close” to the behavior it controls as possible (^) Keep it inaccessible to everything else that doesn’t need to use it
(^) Each handler is an object that contains whatever information is necessary for it to execute properly (^) Internal state is not visible outside the handler object (^) Well-designed objects will allow the user to use them only in the way they were intended
import javax.swing as swing class Draw: def init(self): frame = swing.JFrame("Simple Drawing Program") canvas = swing.JPanel() canvas.preferredSize = (400, 400) frame.contentPane.add(canvas) frame.pack() frame.show() eventHandler = EventHandler() canvas.mousePressed = eventHandler.pressCallback canvas.mouseReleased = eventHandler.releaseCallback class EventHandler: def pressCallback(self, event): self.startx = event.x self.starty = event.y def releaseCallback(self, event): graphics = event.source.graphics graphics.drawLine(self.startx, self.starty, event.x, event.y) if name == "main": draw = Draw()
(^) All of this information can be grouped together into a DrawingWindow object (^) One DrawingWindow object per open window (^) No need to make the information needed by it global
(^) The user list, history, etc., could be grouped into a Chat object (^) One Chat object per ongoing chat (^) No need to make all of this information global
(^) User clicks “New Chat” button in GUI (^) Callback creates a new Chat object to represent the details of that chat def newChat(event): chat = Chat() chat.users = [me, buddyList.selectedValue]