


















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
(^) A “main” file, called gui.py (^) See next slide for how to make it “main” (^) I’ll run it from the command line (^) Put in a ZIP file, along with any additional needed files (^) Name the ZIP file your_last_name .zip (^) Send to me in email, along with details on: (^) Anything special I need to do to run it (^) What platform you developed/tested it on (^) Anything else you think I should know (^) PROGRAM DUE MIDNIGHT TUESDAY 2/8/ (^) We’ll have another lab-heavy session next week also
(^) How do you make a “main” program? (^) Analog to void main() in C, public static void main() in Java (^) In Jython, the system variable name will be set to the string “main” in any file passed directly on the command line to Jython (^) Example: (^) if name == “main”: (^) sampler = SwingSampler() (^) Any code in this if statement will be run if you pass the file on the command line: (^) jython swing-sampler.py
(^) How do you get the name of the user running your program? (^) Useful in, e.g., a Chat program if you don’t want to require users to log in explicitly (^) Note: for testing, you probably want some way to override this, so that you can simulate multiple users on the same machine (^) import java.lang as lang (^) me = lang.System.getProperty(”user.name”) (^) Returns login name
(^) Probably the most used idiom for interactive systems (^) Why? Interactions with the real world (^) 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) (^) Also, the single biggest mind-shift away from doing simple “straight line” programs (^) A few canonical examples: (^) GUIs (responsive to mice, keyboard) (^) Systems that interact with hardware ( interrupts ) (^) Collaborative tools (multiple users, each doing their own thing)
(^) Fortunately, modularity is a good goal anyway (^) Break apart code into more manageable chunks (abstraction) (^) Keep the entanglements between chunks as simple as possible (encapsulation) (^) Corollary: keep as few things global as possible (^) Treat each chunk as a “black box” that does a simple thing, and does it well (information hiding) (^) Object-oriented programming is modularity on steroids (an oversimplification) (^) Modularity is important when even one person is working on it (^) Easier to conceptualize the entire system; chunk behavior into building blocks, etc. You can’t make complexity go away completely, but you can learn techniques to manage it!
(^) Asynchronous: things can happen at arbitrary times (^) Your program will probably have two types of code in it: (^) 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 (^) Some terminology: (^) 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, ...) (^) Your set-up code will install your various event handlers, so that the event dispatcher will know which ones to call (^) Much of your program’s logic will reside in the event handlers!
import javax.swing as swing def callback(event): print “Button was pressed: “, event window = swing.JFrame(”CS6452”) button = swing.JButton(”Press Me!”) button.actionPerformed=callback window.contentPane.add(button) window.pack() window.show() Results: Button was pressed: java.awt.event.ActionEvent[ACTION_PERFORMED,cm d=Press Me!,when=72985371,modifiers=Button1] on javax.swing.JButton[,0,0,87x29,layout=javax.swing.Overl ayLayout,alignmentX=0.0,alignmentY=0.5,border=apple. laf.AquaButtonBorder@eb1670,flags=296,maximumSize =,minimumSize=,preferredSize=,defaultIcon=,disabledIc on=,disabledSelectedIcon=,margin=javax.swing.plaf.Ins etsUIResource[top=3,left=14,bottom=3,right=14],paintBo rder=true,paintFocus=true,pressedIcon=,rolloverEnabled =false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon= ,text=Press Me!,defaultCapable=true]
(^) In Swing, events are generated based on user input (^) Mouse clicks, movement, release (^) Key presses, releases Combinations of all of the above (^) Each widget gets to define what constitutes an event for it, and how callbacks will be associated with it (^) button.actionPerformed (^) list.valueChanged (^) Any given widget may allow multiple kinds of callbacks to be associated with it (^) panel.mousePressed (^) panel.mouseReleased (^) panel.mouseClicked
(^) You can call your callbacks yourself (^) They’re just normal functions (^) Simulate what happens when user input occurs (^) Make sure you return quickly from your event handlers! (^) The program is waiting until you finish so that it can continue running (^) Common signs of a non-returning callback: (^) Program appears to freeze (^) Program window doesn’t redraw (^) Buttons become inactive
(^) Simple callbacks are a perfectly acceptable idiom; they’re the “baseline” of asynchronous programming (^) If you do much callback programming, though, you begin to notice some common patterns: (^) 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
(^) Last mouse-down position needs to be remembered until the next time the callback is invoked (^) Can’t save in a local variable, as it will be reset each time the callback is invoked (^) Option #1: keep all of this cross-callback information in global variables (^) Why is this a bad idea? (^) The information is specific to the drawing callbacks; nothing else should use it (^) By making it global, you increase program clutter, and the mental cycles needed to manage it Worse: you run the risk that someone (you?) will misunderstand what the global variables are for, and reuse them for something else
(^) The principle of data hiding: (^) Keep data as “close” to the behavior it controls as possible (^) Keep it inaccessible to everything else that doesn’t need to use it (^) The more of the inner workings of something you expose, the more likely it is to be used in the wrong way (^) Option #2: object-oriented programming provides a nice way to handle this: (^) 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