12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting...

23
12/5/00 SEM107, Kamin & Reddy Review - 1 Events • Event types • Catching different event types • Getting information from components and events • Distinguishing between events of the same type

Transcript of 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting...

Page 1: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 1

Events

• Event types• Catching different event types• Getting information from components and

events• Distinguishing between events of the same

type

Page 2: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 2

The Java event model

• An event is any occurrence an applets might want to respond to, e.g. user clicks mouse on button, user moves mouse, user enters text into text field.

• Java event model is a method of allowing applets to respond to events.• The Java event model is based on classifying events into different types.

Page 3: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 3

Event types• We will consider four types of events, and explain how to write an applet to respond to each

kind. An applet can also respond to more than one type of event.• Major event types:

– Action events, e.g. button click– Item event, e.g. click check box– Mouse event, e.g. mouse button click– Mouse motion even, e.g. mouse moves in applet

Page 4: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 4

Action eventsDeclare applet: implements ActionListenerRegister component:

component.addActionListener(this);Required methods:

public void actionPerformed (ActionEvent e)

Action events are: user clicks on button; user hits enter key in text field.E.g. following applet respond to button clickby drawing a rectangle in a darker gray

Page 5: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 5

Mouse Motion Events Declare applet:

implements MouseMotionListener Register component: no component to register;

just write: addMouseMotionListener(this); Required methods:

public void mouseMoved (MouseEvent e) public void mouseDragged (MouseEvent e)

Page 6: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 6

Mouse event examplepublic class MouseApplet extends Applet implements MouseListener { int red = 255, green = 255, blue = 255; public void init () { addMouseListener(this); } public void paint (Graphics g) { g.setColor(new Color(red,green,blue)); g.fillRect(10,40,100,50); } public void mouseClicked (MouseEvent e) { red = red-10; green = green-10; blue = blue-10; repaint(); } public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e) {}}

Page 7: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 7

Classes and objects

• Every class has objects, or instances, created using new. Think of the class as an object-producing machine.

new

new

Button

TextField

Page 8: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 8

Example: Point3D

Three-dimensional points consist of three values, the x, y, and z coordinates.

new

Point3D

x

yz

x

yz

Page 9: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 9

Instance variables containing objects (cont.)

Client: Box3D box1 = new Box3D(new Point3D());

x

yz

box1

0 00

x

yz1 1

1

c1 c2

Page 10: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 10

Instance methods

Methods defined without static keyword are instance methods. Unlike class methods, instance methods can refer to instance variables.

class Point3D { double x, y, z; Point3D () { x=0; y=0; z=0; } void print () { System.out.print(x+”,”+y+”,”+z); }}

Page 11: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 11

Example: Appointment class (cont.)

class Appointment { int time; int duration; String description;

Appointment (int t, int d, String s) { time = t; duration = d; description = s; }}

Client:dr_appt = new Appointment(9*60+30, 60, “Dr. No”);

Page 12: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 12

Instance methods calling instance methods

• When an instance method im1 calls another instance method im2, it does not have to name a receiver. By default, the receiver of im2 will be the receiver of im1.boolean overlaps (Appointment appt) { return ((time <= appt.time && endingTime() > appt.time) || (appt.time <= time && appt.endingTime() > time));}

Page 13: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 13

“this”

• Methods can refer to the receiver as a whole - instead of just the receiver’s instance variables - by using variable this.

• In every instance method in a class C, “this” is implicitly declared to be a variable of type C.

Page 14: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 14

“this” (cont.)

• More important uses are when instance method needs to pass entire object to another method. E.g. rewrite overlaps:

• In applets, “register” component by: b.addActionListener(this);

boolean overlaps (Appointment appt) { if (time <= appt.time) return endingTime() > appt.time; else return appt.overlaps(this);}

Page 15: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 15

Private instance variables

Often, you don’t want clients of a class to be able to access its instance variables. To prevent them from doing so, declare the variables private.

class Appointment { private int time; private int duration; private String description; . . .}

Client: Appointment dr = new Appointment(...); ... dr.time ... // compile error!

Page 16: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 16

Why private instance variables?

An Appointment object represents an appointment at a particular time. There are many ways to represent appointments. Here are two alternatives:

class Appointment { private int time; private int endtime; ... int endingTime () { return endtime; } . . .

class Appointment { private int hour, minute; private int duration; String description; int endingTime () { return hour*60+minute+ duration; } . . .

Page 17: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 17

Iteration

• Traditional method of repeating statements (not using recursion)

loop control { --------------- ---------------}

repeat these statementszero or more times,controlled by loop control

Page 18: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 18

While loops

while (condition) statement

contains variablesthat are changed

in loop

repeat untilcondition

becomes true

Keep in mind that statement can be a compound statement.

Page 19: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 19

For loops

for (init; cond; incr) S

init;while (cond) { S incr;}

Page 20: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 20

Do-while loops

do Swhile (cond)

S;while (cond) S

Page 21: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 21

Aside: Rolling forever

• Can have applet that simply rolls eyes forever. However, can’t do it like this:

because it would tie up the browser.• Instead, need to relinquish control to the

browser occasionally.

public void paint(Graphics g) { int i; for (i=0; i<360; i = (i+1)%360) pair1.draw(g, 2*Math.PI*i/360.0); }

Page 22: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 22

Aside: Rolling forever (cont.)The new version of RollingEyesApplet:public class RollingEyesApplet6 extends Applet {

RollingEyes5 pair1; int lastangle = 0;

public void init () { pair1 = new RollingEyes5(200,100); }

public void paint(Graphics g) { pair1.draw(g, 2*Math.PI*lastangle/360.0); lastangle = (lastangle+1) % 360; repaint(); }}

Page 23: 12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

12/5/00 SEM107, Kamin & Reddy Review - 23

Example: Slower rolling eyes (cont.)

• public void paint(Graphics g) {• int i;• for (i=0; i<360; i = i+1) {• pair1.draw(g, 2*Math.PI*i/360.0);• try {• Thread.sleep(10);• } catch (InterruptedException t) {}• }• }

The highlighted portion creates a pause in themiddle of a computation, here adding a10 millisecond delay to each iteration of the loop.