Events based Programming

11
Events based Programming Chris North cs3724: HCI

description

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() { - PowerPoint PPT Presentation

Transcript of Events based Programming

Page 1: Events based Programming

Events based Programming

Chris North

cs3724: HCI

Page 2: Events based Programming

Typical command line program

• Non-interactive

• Linear execution

program:

main(){

code;code;code;code;code;code;code;code;code;code;code;code;

}

Page 3: Events based Programming

Interactive command line program

• User input commands

• Non-linear execution

• Unpredictable order

• Much idle time

program:

main(){

decl data storage;initialization code;

loop{

get command;switch(command){

command1:code;

command2:code;

…}

}}

Page 4: Events based Programming

Typical GUI programGUI 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

Page 5: Events based Programming

GUI Events

WindowSystem

eventloop

App1

OK

Cancel

App2 code:

OKbtn_click() { do stuff;}OKbtn_mouseover(){ do more stuff;}CancelBtn_click(){ do different stuff;}

mouseclick

inputdevice

App1

eventloop

App2

eventloop

whichapp?

whichcallback?

App2

OK

Cancel

Page 6: Events based Programming

GUI programGUI program:

Class{main(){

decl data storage;initialization code;

create GUI objects;register listeners;

}

listener1(){ do stuff;}listener2(){ do stuff;}…

• Event loop automaticin separate thread

Page 7: Events based Programming

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();}…

Page 8: Events based Programming

Listeners

1. Register with a component to receive events• Give component a ref to your Listener object

• JButton1.addMouseListener(new myMouseListener)

2. Receive events from component• Component will call callback procs

on your Listener object

• myMouseListener.mouseClicked(event)

JButton1

myMouse-Listener

click

2. mouseClicked( )1. addMouseListener( )

Page 9: Events based Programming

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()

Page 10: Events based Programming

Code

button1 = new JButton(“press me”);myListener = new myListenClass;button1.addMouseListener(myListener);

// extending a class (“subclassing”):class myListenClass extends MouseAdapter {

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)

Page 11: Events based Programming

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