110 likes | 230 Views
This document explores the fundamental differences between command-line programs, interactive command-line programs, and graphical user interface (GUI) applications in the context of event-driven programming. We highlight the characteristics of linear execution in non-interactive programs versus non-linear execution in interactive and GUI applications, emphasizing the importance of user input and event loops. Using practical examples, this overview discusses how to implement callbacks, listener classes, and event handling, providing a solid foundation for developers to design responsive and intuitive applications.
E N D
Events based Programming Chris North cs3724: HCI
Typical command line program • Non-interactive • Linear execution program: main() { code; code; code; code; code; code; code; code; code; code; code; code; }
Interactive command line program program: main() { decl data storage; initialization code; loop { get command; switch(command) { command1: code; command2: code; … } } } • User input commands • Non-linear execution • Unpredictable order • Much idle time
Typical GUI program GUI program: main() { decl data storage; initialization code; create GUI; register callbacks; main event loop; } Callback1() //button1 press { code; } Callback2() { code; } … • User input commands • Non-linear execution • Unpredictable order • Much idle time • Event callback procs
GUI Events App1 App2 mouseclick OK OK Cancel Cancel App2 code: OKbtn_click() { do stuff; } OKbtn_mouseover() { do more stuff; } CancelBtn_click() { do different stuff; } App1 event loop WindowSystem event loop App2 event loop whichapp? inputdevice whichcallback?
GUI program GUI program: Class{ main() { decl data storage; initialization code; create GUI objects; register listeners; } listener1() { do stuff; } listener2() { do stuff; } … • Event loop automaticin separate thread
Example Example: draw program MyDrawClass{ main() { DataStruct drawn_shapes; drawn_shapes.clear(); create Frame, Panel, buttons, … register listeners; } DrawPanel_listener_click() { drawn_shapes.add(new shape); } UndoButton_listener_click() { drawn_shapes.deleteLast(); } …
Listeners • Register with a component to receive events • Give component a ref to your Listener object • JButton1.addMouseListener(new myMouseListener) • Receive events from component • Component will call callback procs on your Listener object • myMouseListener.mouseClicked(event) click JButton1 1. addMouseListener( ) 2. mouseClicked( ) myMouse-Listener
Listener API • Listeners must inherit from Listener base classes • ActionListener, KeyListener, MouseListener, MouseMotionListener, WindowListener, … • Abstract base classes: xxxxListener • Stubbed base classes: xxxxAdapter • MouseListener: • mouseClicked(), mouseEntered(), mouseExited(), mousePressed(), mouseReleased()
Code button1 = new JButton(“press me”); myListener = new myListenClass; button1.addMouseListener(myListener); // extending a class (“subclassing”): class myListenClass extendsMouseAdapter { public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } } // OR “implementing an interface”: class myListenClass implements MouseListener { public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } … } An abstract base class (methods, no code)
Event objects • mouseClicked(MouseEvent e) • MouseEvent: • getX( ), getY( ), getClickCount( ), getSource( ), … • For each listener type: • Component.addxxxxListener( ) • xxxxListener abstract base class • xxxxAdapter stubbed base class • xxxxEvent