Events and listeners - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Events and listeners

Description:

Definition: An event is an object thrown in response to a user of programmatic action Definition: A listener is a series of methods that executes in response to an events – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 15
Provided by: harveyd
Learn more at: http://cs.sou.edu
Category:

less

Transcript and Presenter's Notes

Title: Events and listeners


1
Events and listeners
  • Definition An event is an object thrown in
    response to a user of programmatic action
  • Definition A listener is a series of methods
    that executes in response to an events
  • Definition An adapter is a class that has
    default listener methods
  • Implementation
  • Implement the listener interface or extend an
    adapter class
  • Create listener methods appropriately
  • Register the listener to a component or a
    listener manager
  • Note An alternate implementation is to create an
    anonymous listener object passed as an argument
    when registering.

2
Actionlistener example
  • Listen for button clicks
  • public class MyPanel extends JPanel
    implements ActionListener
  • Jbutton b
  • public MyPanel() JButton b new
    JButton(hello)
  • add(b)
  • b.addActionListener(this)
  • public void actionPerformed(ActionEvent e)
  • if (e.getSource() instanceof JButton)
  • JButton x (JButton)(e.getSource())
    System.out.println (its a
    x.getText() button)

Note A single listener can repond to events from
multiple objects
3
Key Listener
  • Purpose
  • Listen for user keyboard entries
  • Limitation the component must have focus
  • Implementation
  • Create keyPressed, keyTyped, and keyReleased
    methods if using the implements KeyListener
    interface
  • Create only the methods needed if using a
    KeyAdapter
  • Register the listener with the component

4
Anonymous Keylistener example
  • class KeyPanel extends JPanel
  • private int x100, y100 String keyChar x'
  • public KeyPanel()
  • setFocusable(true) // Doesnt work for
    JApplets
  • addKeyListener(new KeyAdapter() public
    void keyPressed(KeyEvent e) switch
    (e.getKeyCode()) case KeyEvent.VK_DOWN
    y10 break
  • case KeyEvent.VK_UP y-10 break
  • case KeyEvent.VK_LEFT x-10 break
  • case KeyEvent.VK_RIGHT x10 break
  • default keyChar e.getKeyChar()
  • repaint() )
  • public void paintComponent(Graphics g)
    super.paintComponent(g) g.drawString(keyChar,x,y)

5
KeyEventdispatcher
  • Differences from KeyListener objects
  • Dont need to worry about whether a component has
    focus
  • Monitors keyboard events before they are sent to
    components with focus
  • The listener method is dispatchKeyEvent, not
    keyPressed
  • Register the Listener with the system
    KeyboardFocusManager
  • Useful for applets, because applet JPanels wont
    get focus
  • Implementation
  • KeyboardFocusManager manager
    KeyboardFocusManager.getCurrentKeyboardFocusManage
    r()
  • manager.addKeyEventDispatcher(new
    KeyEventDispatcher()
  • public boolean dispatchKeyEvent(KeyEvent
    event)
  • / code goes here
  • return true )

6
Other listeners available
  • Mouse Listener Events Mouse operations
  • public void mousePressed(MouseEvent e)
  • public void mouseReleased(MouseEvent e)
  • public void mouseEntered(MouseEvent e)
  • public void mouseExited(MouseEvent e)
  • public void mouseClicked(MouseEvent e)
  • Mouse Motion Listener Movements of the mouse
  • public void mouseDragged(MouseEvent e)
  • public void mouseMoved(MouseEvent e)
  • Item Listener State of an object changed
  • Public void itemStateChanged(ItemEvent e)
  • Even more FocusListener, WindowListener, etc.

7
threads
  • Definition A path of execution through a process
  • Any Class can be a thread if it
  • implements runnable
  • provides a run method (public void run())
  • A thread starts by calling start(), which in turn
    calls run()
  • A thread ends when the run method exits
  • Be careful
  • Threads all run concurrently order of execution
    is not known
  • One thread can step on variables set in another
    thread

8
The timer class
  • public class MovingPanel extends JPanel
    implements ActionListener
  • private String message Welcome to Java
  • private int x0, y 20
  • public MovingPanel() Timer timernew
    Timer(1000,this)
  • timer.start()
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • g.drawString(message, x, y)
  • public void actionPerformed(ActionEvent e) if
    (xgtgetWidth() x -20
  • x5
  • if (ygtgetHeight() 20) y 20
  • y5
  • repaint()

9
Multidimensional Arrays
0 1 2 3
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
3 13 14 15 16
  • int data new int44, count 1
  • for (int row0 rowltdata.length row)for (int
    col0 colltdatarow.length col)
    datarowcol count
  • OR
  • int data 1,2,3,4, 5,6,7,8,
    9,10,11,12, 13,14,15,16
  • for (int row0 rowltdata.length row)
  • for (int col0 colltdatarow.length col)
    System.out.printf("4d", datarowcol)
  • System.out.println()

10
Ragged Array
Definition Different rows have different numbers
of columnt
  • count1
  • int values new int4, cols 2, 4,
    1,3, count 1
  • for (int row0 rowltvalues.length row)
  • valuesrow new intcolsrow
  • for (int col0 colltvaluesrow.length
    col)
  • valuesrowcol count
  • for (int row0 rowltvalues.length row)
  • for (int col0 colltvaluesrow.length col)
  • System.out.printf("4d",
    valuesrowcol) System.out.println()

11
Closest pair problem
  • public double distance(Point first, Point second)
  • double delta1 Math.pow(first.x second.x,
    2)
  • double delta2 Math.pow(first.y second.y),
    2
  • return Math.sqrt(delta1 delta2)
  • Consider a two dimensional array, data
  • Problem Find the pair of points closest together
    using the above method.

12
Application for the class project
  • Moving sprites in four dimensions
  • int directions 0,1, 1,0, 0,-1,
    -1,0
  • Moving sprites in eight directions
  • int directions 0,2, 1,1, 2,0,
    1,-1, 0,-2, -1, -1, 0,-2, -1,1
  • Moving sprites in sixteen directions
  • int directions 4, 0, 3, 1, 2,
    2, 1, 3, 0, 4, -1, 3, -2, 2, -3,
    1, -4, 0, -3,-1, -2,-2, -1,-3,
    0,-4, 1,-3, 2,-2, 3,-1
  • Questions
  • What is the relation between the directions and
    the one that is half away in the array?
  • How would we extend this to twice the size?

13
Another possible game application
  • Divide the panel into row and column cells.
  • Define rules for what happens if predators or
    avatars intersect the various cells
  • Examples of possible game rules
  • Move them automatically to another place
  • Have the game shift to another room
  • Give the players points or create additional
    avatars
  • Kill the avatar that hits that particular cells
  • The rules for avatars may vary from that of
    predators

14
Higher dimensional array
  • First dimension month
  • Second dimension day
  • Third dimension hour
  • Fourth dimension Type of measurement0
    temperature, 1 humidity
  • Symbolic constants public final static int T0,
    H1
  • Questions
  • To what does the following refer?int x3, y4,
    z5, k32, m50dataxyzT 32
  • dataxyzH 50
  • How do we instantiate such an array?
Write a Comment
User Comments (0)
About PowerShow.com