Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to...

11
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

Transcript of Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to...

Page 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,

Chapter 11Java 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

Page 2: 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,

Chapter Preview

In this chapter we will:• introduce event driven programming• show how a program can respond to mouse

events (e.g. clicks and mouse movements)• demonstrate how to implement a listener

interface• show how mouse events can be used to build

highly interactive programs

Page 3: 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,

Abstract Windowing Toolkit (AWT)

• Part of the Java distribution, but not part of the Java language itself

• AWT is library of classes and methods used by Java programmers that supported are by the basic Java runtime libraries

• Even though this chapter is labeled optional, it is not possible to be a complete Java programmer without understanding the AWT

Page 4: 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,

Java Events

• Events are occurrences outside of the program to which the program must respond (e.g. user key press)

• Event managers are methods that first catch the event

• Listener classes send messages to an event manager requesting to be notified when a particular message occurs

• Event managers send messages to listeners notifying them when a particular event occurs

Page 5: 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,

Creating Mouse Click Listeners

1. Import the java.awt.event package2. In the class header, add the words

implements MouseListener3. Send the addMouseListerner(this)

message to the mouse event manager (should be done in the constructor method)

4. Define methods mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited

Page 6: 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,

Mouse Click Listener Template

import java.awt.event.*;…public class classname implements MouseListener { … // include manager.addMouseListener(this); // in some initialization method … public void mouseClicked (MouseEvent e) { … } public void mousePressed (MouseEvent e) { … } public void mouseReleased (MouseEvent e) { … } public void mouseEntereded (MouseEvent e) { … } public void mouseExited (MouseEvent e) { … } }

Page 7: 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,

Finding Mouse Location

• Inside mouseClicked the argument e of type MouseEvent can be used to find the location of the mouse cursor

• The call e.getX( ) returns the value of the horizontal coordinate of the mouse cursor position

• The call e.getY( ) returns the value of the vertical coordinate of the mouse cursor position

Page 8: 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,

Creating Mouse Motion Listeners

1. Import the java.awt.event package2. In the class header, add the words

implements MouseMotionListener3. Send the message

addMouseMotionListerner(this) to the mouse event manager (should be done in the constructor method)

4. Define methods mouseMoved and mouseDragged

Page 9: 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,

Mouse Motion Listener Template

import java.awt.event.*;

public class classname implements MouseMotionListener {

// include manager.addMouseListener(this);

// in some initialization method

public void mouseMoved (MouseEvent e) { … }

public void mouseDragged (MouseEvent e) { … }

}

Page 10: 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,

Mouse Motion

• mouseMoved will be called each time the mouse makes a significantly large movement

• mouseDragged will be called instead if thre mouse button is pressed and the mouse makes a significantly large movement

Page 11: 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,

Listening to All Mouse Events

• Two catch all seven mouse events use the following class headerpublic class classname implements MouseListener, MouseMotionListener {

• You then need to define all seven mouse methods mouseMoved, mouseDragged, mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited