Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using ...
Chapter Preview <ul><li>In this chapter we will: </li></ul><ul><li>introduce event driven programming </li></ul><ul><li>sh...
Abstract Windowing Toolkit (AWT) <ul><li>Part of the Java distribution, but not part of the Java language itself </li></ul...
Java Events <ul><li>Events  are occurrences outside of the program to which the program must respond (e.g. user key press)...
Creating Mouse Click Listeners <ul><li>Import the java.awt.event package </li></ul><ul><li>In the class header, add the wo...
Mouse Click Listener Template <ul><li>import java.awt.event.*; </li></ul><ul><li>… </li></ul><ul><li>public class  classna...
Finding Mouse Location <ul><li>Inside mouseClicked the argument e of type MouseEvent can be used to find the location of t...
Creating Mouse Motion Listeners <ul><li>Import the java.awt.event package </li></ul><ul><li>In the class header, add the w...
Mouse Motion Listener Template <ul><li>import java.awt.event.*; </li></ul><ul><li>… </li></ul><ul><li>public class  classn...
Mouse Motion <ul><li>mouseMoved  will be called each time the mouse makes a significantly large movement </li></ul><ul><li...
Listening to All Mouse Events <ul><li>Two catch all seven mouse events use the following class header </li></ul><ul><ul><l...
Upcoming SlideShare
Loading in …5
×

Java

664
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
664
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java

  1. 1. Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold
  2. 2. Chapter Preview <ul><li>In this chapter we will: </li></ul><ul><li>introduce event driven programming </li></ul><ul><li>show how a program can respond to mouse events (e.g. clicks and mouse movements) </li></ul><ul><li>demonstrate how to implement a listener interface </li></ul><ul><li>show how mouse events can be used to build highly interactive programs </li></ul>
  3. 3. Abstract Windowing Toolkit (AWT) <ul><li>Part of the Java distribution, but not part of the Java language itself </li></ul><ul><li>AWT is library of classes and methods used by Java programmers that supported are by the basic Java runtime libraries </li></ul><ul><li>Even though this chapter is labeled optional, it is not possible to be a complete Java programmer without understanding the AWT </li></ul>
  4. 4. Java Events <ul><li>Events are occurrences outside of the program to which the program must respond (e.g. user key press) </li></ul><ul><li>Event managers are methods that first catch the event </li></ul><ul><li>Listener classes send messages to an event manager requesting to be notified when a particular message occurs </li></ul><ul><li>Event managers send messages to listeners notifying them when a particular event occurs </li></ul>
  5. 5. Creating Mouse Click Listeners <ul><li>Import the java.awt.event package </li></ul><ul><li>In the class header, add the words implements MouseListener </li></ul><ul><li>Send the addMouseListerner(this) message to the mouse event manager (should be done in the constructor method) </li></ul><ul><li>Define methods mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited </li></ul>
  6. 6. Mouse Click Listener Template <ul><li>import java.awt.event.*; </li></ul><ul><li>… </li></ul><ul><li>public class classname implements MouseListener { </li></ul><ul><li>… </li></ul><ul><li>// include manager .addMouseListener(this); </li></ul><ul><li>// in some initialization method </li></ul><ul><li>… </li></ul><ul><li>public void mouseClicked (MouseEvent e) { … } </li></ul><ul><li>public void mousePressed (MouseEvent e) { … } </li></ul><ul><li>public void mouseReleased (MouseEvent e) { … } </li></ul><ul><li>public void mouseEntereded (MouseEvent e) { … } </li></ul><ul><li>public void mouseExited (MouseEvent e) { … } </li></ul><ul><li>} </li></ul>
  7. 7. Finding Mouse Location <ul><li>Inside mouseClicked the argument e of type MouseEvent can be used to find the location of the mouse cursor </li></ul><ul><li>The call e.getX( ) returns the value of the horizontal coordinate of the mouse cursor position </li></ul><ul><li>The call e.getY( ) returns the value of the vertical coordinate of the mouse cursor position </li></ul>
  8. 8. Creating Mouse Motion Listeners <ul><li>Import the java.awt.event package </li></ul><ul><li>In the class header, add the words implements MouseMotionListener </li></ul><ul><li>Send the message addMouseMotionListerner(this) to the mouse event manager (should be done in the constructor method) </li></ul><ul><li>Define methods mouseMoved and mouseDragged </li></ul>
  9. 9. Mouse Motion Listener Template <ul><li>import java.awt.event.*; </li></ul><ul><li>… </li></ul><ul><li>public class classname implements MouseMotionListener { </li></ul><ul><li>… </li></ul><ul><li>// include manager .addMouseListener(this); </li></ul><ul><li>// in some initialization method </li></ul><ul><li>… </li></ul><ul><li>public void mouseMoved (MouseEvent e) { … } </li></ul><ul><li>public void mouseDragged (MouseEvent e) { … } </li></ul><ul><li>} </li></ul>
  10. 10. Mouse Motion <ul><li>mouseMoved will be called each time the mouse makes a significantly large movement </li></ul><ul><li>mouseDragged will be called instead if th r e mouse button is pressed and the mouse makes a significantly large movement </li></ul>
  11. 11. Listening to All Mouse Events <ul><li>Two catch all seven mouse events use the following class header </li></ul><ul><ul><li>public class classname implements MouseListener, MouseMotionListener { </li></ul></ul><ul><li>You then need to define all seven mouse methods mouseMoved, mouseDragged, mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×