Events Event types Catching different event types Getting

  • Slides: 23
Download presentation
Events • Event types • Catching different event types • Getting information from components

Events • Event types • Catching different event types • Getting information from components and events • Distinguishing between events of the same type 12/5/00 SEM 107, Kamin & Reddy 34

The Java event model • An event is any occurrence an applets might want

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. 12/5/00 SEM 107, Kamin & Reddy 35

Event types • We will consider four types of events, and explain how to

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 12/5/00 SEM 107, Kamin & Reddy 36

Action events Declare applet: implements Action. Listener Register component: component. add. Action. Listener(this); Required

Action events Declare applet: implements Action. Listener Register component: component. add. Action. Listener(this); Required methods: public void action. Performed (Action. Event e) Action events are: user clicks on button; user hits enter key in text field. E. g. following applet respond to button click by drawing a rectangle in a darker gray 12/5/00 SEM 107, Kamin & Reddy 37

Mouse Motion Events Declare applet: implements Mouse. Motion. Listener Register component: no component to

Mouse Motion Events Declare applet: implements Mouse. Motion. Listener Register component: no component to register; just write: add. Mouse. Motion. Listener(this); Required methods: public void mouse. Moved (Mouse. Event e) public void mouse. Dragged (Mouse. Event e) 12/5/00 SEM 107, Kamin & Reddy 38

Mouse event example public class Mouse. Applet extends Applet implements Mouse. Listener { int

Mouse event example public class Mouse. Applet extends Applet implements Mouse. Listener { int red = 255, green = 255, blue = 255; public void init () { add. Mouse. Listener(this); } public void paint (Graphics g) { g. set. Color(new Color(red, green, blue)); g. fill. Rect(10, 40, 100, 50); } public void mouse. Clicked (Mouse. Event e) { red = red-10; green = green-10; blue = blue-10; repaint(); } public void mouse. Pressed (Mouse. Event e) {} public void mouse. Released (Mouse. Event e) {} public void mouse. Entered (Mouse. Event e) {} public void mouse. Exited (Mouse. Event e) {} } 12/5/00 SEM 107, Kamin & Reddy 39

Classes and objects • Every class has objects, or instances, created using new. Think

Classes and objects • Every class has objects, or instances, created using new. Think of the class as an objectproducing machine. Button new Text. Field new 12/5/00 SEM 107, Kamin & Reddy 40

Example: Point 3 D Three-dimensional points consist of three values, the x, y, and

Example: Point 3 D Three-dimensional points consist of three values, the x, y, and z coordinates. x z y Point 3 D new x z y 12/5/00 SEM 107, Kamin & Reddy 41

Instance variables containing objects (cont. ) Client: box 1 Box 3 D box 1

Instance variables containing objects (cont. ) Client: box 1 Box 3 D box 1 = new Box 3 D(new Point 3 D()); c 1 c 2 x 1 z 1 y 1 x 0 z 0 y 0 12/5/00 SEM 107, Kamin & Reddy 42

Instance methods Methods defined without static keyword are instance methods. Unlike class methods, instance

Instance methods Methods defined without static keyword are instance methods. Unlike class methods, instance methods can refer to instance variables. class Point 3 D { double x, y, z; Point 3 D () { x=0; y=0; z=0; } void print () { System. out. print(x+”, ”+y+”, ”+z); } } 12/5/00 SEM 107, Kamin & Reddy 43

Example: Appointment class (cont. ) class Appointment { int time; int duration; String description;

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”); 12/5/00 SEM 107, Kamin & Reddy 44

Instance methods calling instance methods • When an instance method im 1 calls another

Instance methods calling instance methods • When an instance method im 1 calls another instance method im 2, it does not have to name a receiver. By default, the receiver of im 2 will be the receiver of im 1. boolean overlaps (Appointment appt) { return ((time <= appt. time && ending. Time() > appt. time) || (appt. time <= time && appt. ending. Time() > time)); } 12/5/00 SEM 107, Kamin & Reddy 45

“this” • Methods can refer to the receiver as a whole - instead of

“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. 12/5/00 SEM 107, Kamin & Reddy 46

“this” (cont. ) • More important uses are when instance method needs to pass

“this” (cont. ) • More important uses are when instance method needs to pass entire object to another method. E. g. rewrite overlaps: boolean overlaps (Appointment appt) { if (time <= appt. time) return ending. Time() > appt. time; else return appt. overlaps(this); } • In applets, “register” component by: b. add. Action. Listener(this); 12/5/00 SEM 107, Kamin & Reddy 47

Private instance variables Often, you don’t want clients of a class to be able

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; . . . } Appointment dr = new Appointment(. . . ); . . . dr. time. . . // compile error! Client: 12/5/00 SEM 107, Kamin & Reddy 48

Why private instance variables? An Appointment object represents an appointment at a particular time.

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 ending. Time () { return endtime; }. . . 12/5/00 class Appointment { private int hour, minute; private int duration; String description; int ending. Time () { return hour*60+minute+ duration; }. . . SEM 107, Kamin & Reddy 49

Iteration • Traditional method of repeating statements (not using recursion) loop control { --------------}

Iteration • Traditional method of repeating statements (not using recursion) loop control { --------------} 12/5/00 repeat these statements zero or more times, controlled by loop control SEM 107, Kamin & Reddy 50

While loops while (condition) statement contains variables that are changed in loop repeat until

While loops while (condition) statement contains variables that are changed in loop repeat until condition becomes true Keep in mind that statement can be a compound statement. 12/5/00 SEM 107, Kamin & Reddy 51

For loops for (init; cond; incr) S 12/5/00 SEM 107, Kamin & Reddy init;

For loops for (init; cond; incr) S 12/5/00 SEM 107, Kamin & Reddy init; while (cond) { S incr; } 52

Do-while loops do S while (cond) 12/5/00 SEM 107, Kamin & Reddy S; while

Do-while loops do S while (cond) 12/5/00 SEM 107, Kamin & Reddy S; while (cond) S 53

Aside: Rolling forever • Can have applet that simply rolls eyes forever. However, can’t

Aside: Rolling forever • Can have applet that simply rolls eyes forever. However, can’t do it like this: public void paint(Graphics g) { int i; for (i=0; i<360; i = (i+1)%360) pair 1. draw(g, 2*Math. PI*i/360. 0); } because it would tie up the browser. • Instead, need to relinquish control to the browser occasionally. 12/5/00 SEM 107, Kamin & Reddy 54

Aside: Rolling forever (cont. ) The new version of Rolling. Eyes. Applet: public class

Aside: Rolling forever (cont. ) The new version of Rolling. Eyes. Applet: public class Rolling. Eyes. Applet 6 extends Applet { Rolling. Eyes 5 pair 1; int lastangle = 0; public void init () { pair 1 = new Rolling. Eyes 5(200, 100); } public void paint(Graphics g) { pair 1. draw(g, 2*Math. PI*lastangle/360. 0); lastangle = (lastangle+1) % 360; repaint(); } } 12/5/00 SEM 107, Kamin & Reddy 55

Example: Slower rolling eyes (cont. ) • • public void paint(Graphics g) { int

Example: Slower rolling eyes (cont. ) • • public void paint(Graphics g) { int i; for (i=0; i<360; i = i+1) { pair 1. draw(g, 2*Math. PI*i/360. 0); try { Thread. sleep(10); } catch (Interrupted. Exception t) {} portion creates a pause in the • The highlighted } middle of a computation, here adding a • } 10 millisecond delay to each iteration of the loop. 12/5/00 SEM 107, Kamin & Reddy 56