CS 162 Spring 2005 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CS 162 Spring 2005

Description:

g.drawString('hello world', rx, ry); Paint and mouse, cont. Class MyPanel extends Jpanel { private int rx = 0; private int ry = 0; class MouseSpy extends MouseAdapter ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 26
Provided by: timoth90
Category:
Tags: class | spring

less

Transcript and Presenter's Notes

Title: CS 162 Spring 2005


1
CS 162 - Spring 2005
  • Chapter 10 (12 in 2nd Edition)
  • Event Handlers

2
Events
  • Events are things like a mouse press, a timer, a
    window changing characteristics
  • Tracked by the Java run-time system
  • When detected, the program must be notified

3
How to Handle Events
  • Events have properties similar to those we
    discussed in Chapter 9
  • It is easy for the run-time system to tell when
    an event has occurred, but each application
    responds differently.
  • Solution we introduced in Chapter 9, interfaces
    and polymorphism

4
Problem of Binding Times Again
  • The windowing system was written by a programmer
    at Sun a long long time ago. No idea what a mouse
    press means to you
  • You write a new application today. You give
    meaning to mouse press, etc.
  • How does the windowing system, which does not
    know your application, make it easy for you to
    provide meaning ?

5
Polymorphism
  • Reminder meaning of Polymorphism. Poly many,
    Morph form, many forms.
  • A variable declared as one type (an interface
    type) but is in reality a different type (an
    application type).
  • For events, combined with the idea of Listeners

6
An Example, Mouse Listener
  • Here is the interface class MouseListener
  • public interface MouseListener
  • void mousePressed(MouseEvent event)
  • void mouseReleased(MouseEvent event)
  • void mouseClicked(MouseEvent event)
  • void mouseEntered(MouseEvent event)
  • void mouseExited(MouseEvent event)

7
Event Listener
  • User creates a class that implements
    MouseListener
  • The user hands this object to the run-time system
  • The runtime system "thinks" it is dealing with a
    MouseListener, in reality it is dealing with your
    new application class

8
Solving Binding Time Problem
  • The Windowing library knows that it is dealing
    with MouseListener. Works for any MouseListener.
    Written once long ago.
  • You create your new application today, give new
    meaning to MouseListener, but old Windowing
    system continues to work.

9
Creating the Application Listener
  • Public class MouseSpy implements MouseListener
  • public void mousePressed(MouseEvent event)
  • System.out.println("Mouse Pressed at "
    event.getX() " " event.getY())

10
Attaching the Listener
  • Public class MouseApplet extends Applet
  • public MouseApplet()
  • MouseListener listener new MouseSpy()
  • addMouseListener(listener)

11
Inner classes and Listeners
  • Often the listener is an inner class, so it can
    modify the surrounding application variables.
  • The MouseListener interface has many methods,
    often only interested in one or two, so there is
    an adapter named MouseAdapter.

12
MouseAdapter
  • Class MouseAdapter implements MouseListener
  • public void mousePressed(MouseEvent event)
  • public void mouseReleased(MouseEvent event)
  • public void mouseClicked(MouseEvent event)
  • public void mouseEntered(MouseEvent event)
  • public void mouseExited(MouseEvent event)
  • // notice each method has an empty implementation

13
Using MouseAdapter
  • You use inheritance to derive from MouseAdapter,
    and modify only what you want
  • class MouseSpy extends MouseAdapter
  • public void mouseClicked(MouseEvent event)

14
Graphical Applications
  • Normally you want to catch the event, do
    something, then update the graphical display
  • To illustrate this we need first a quick
    introduction to the Java graphical model
  • We will use applications, not applets

15
The Java Graphical model
  • The Java (2E) Graphical model is something like
    the following
  • You create a frame (a window with title and
    menus) which surrounds a Panel (a drawing
    surface).
  • The run-time system tells your panel to display
    by calling paintComponent.

16
Creating a Window
  • public class Life
  • public static void main(String args)
  • JFrame frame new JFrame()
  • frame.setTitle(Game of Life)
  • frame.setSize(400, 300)
  • frame.setDefaultCloseOperation(3) //
    EXIT_ON_CLOSE
  • JPanel board new MyPanel()
  • frame.setContentPane(board)
  • frame.show()

17
MyPanel - Polymorphism again
  • class MyPanel extends JPanel
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)

18
Combining with Mouse Events
  • Class MyPanel extends Jpanel
  • private int rx 0 private int ry 0
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • g.drawString("hello world", rx, ry)

19
Paint and mouse, cont.
  • Class MyPanel extends Jpanel
  • private int rx 0 private int ry 0
  • class MouseSpy extends MouseAdapter
  • public void mousePressed(MouseEvent event)
  • rx event.getX()
  • ry event.getY()
  • repaint() // VERY IMPORTANT

20
Repainting vs painting
  • Calling repaint tells the windowing system you
    want to repaint the window.
  • At some later point, the windowing system calls
    paint, then paintComponent.
  • You never call paint or paintComponent directly.

21
Connecting the two
  • Class MyPanel extends Jpanel
  • public MyPanel()
  • addMouseListener (new MouseSpy())

22
Sequence of Events
  • Creation of Panel causes creation of listener
  • Listener is added to set of listeners for the
    Panel
  • When mouse goes down, Listener is invoked, sets
    the x and y locations, calls repaint
  • Repaint tells run-time system to invoke paint,
    which calls paintComponent
  • Invokes Panel.paintComponent, and window is
    painted.

23
Event-Driven Programming
  • This style of programming is called event-driven
    programming
  • Puts the user in charge, the overall flow of
    control is directed by events, not by the program
    logic.
  • Hallmark of modern style graphical applications.

24
Event Driven vs non Event Driven Execution
  • Old style - program controls sequence of events,
    from time to time asks user for input or displays
    output
  • New Event Driven Style - program sits and waits
    for events. User controls flow of execution. When
    an event is detected, program reacts

25
Questions?
  • You will be doing GUI style programming in
    programming assignment 2.
  • If no other questions, then we will have the Anti
    Quiz.
Write a Comment
User Comments (0)
About PowerShow.com