Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing."— Presentation transcript:

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

2 12/5/00SEM107, Kamin & ReddyReview - 35 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.

3 12/5/00SEM107, Kamin & ReddyReview - 36 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

4 12/5/00SEM107, Kamin & ReddyReview - 37 Action events  Declare applet: implements ActionListener  Register 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 click by drawing a rectangle in a darker gray

5 12/5/00SEM107, Kamin & ReddyReview - 38 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)

6 12/5/00SEM107, Kamin & ReddyReview - 39 Mouse event example public 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) {} }

7 12/5/00SEM107, Kamin & ReddyReview - 40 Classes and objects Every class has objects, or instances, created using new. Think of the class as an object- producing machine. new Button TextField

8 12/5/00SEM107, Kamin & ReddyReview - 41 Example: Point3D Three-dimensional points consist of three values, the x, y, and z coordinates. new Point3D x y z x y z

9 12/5/00SEM107, Kamin & ReddyReview - 42 Instance variables containing objects (cont.) Client: Box3D box1 = new Box3D(new Point3D()); x y z box1 0 0 0 x y z 1 1 1 c1c2

10 12/5/00SEM107, Kamin & ReddyReview - 43 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); }

11 12/5/00SEM107, Kamin & ReddyReview - 44 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 12/5/00SEM107, Kamin & ReddyReview - 45 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)); }

13 12/5/00SEM107, Kamin & ReddyReview - 46 “ 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.

14 12/5/00SEM107, Kamin & ReddyReview - 47 “ 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); }

15 12/5/00SEM107, Kamin & ReddyReview - 48 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!

16 12/5/00SEM107, Kamin & ReddyReview - 49 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; }...

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

18 12/5/00SEM107, Kamin & ReddyReview - 51 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.

19 12/5/00SEM107, Kamin & ReddyReview - 52 For loops for ( init ; cond ; incr ) S init ; while ( cond ) { S incr ; } 

20 12/5/00SEM107, Kamin & ReddyReview - 53 Do-while loops do S while ( cond ) S ; while ( cond ) S 

21 12/5/00SEM107, Kamin & ReddyReview - 54 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); }

22 12/5/00SEM107, Kamin & ReddyReview - 55 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(); }

23 12/5/00SEM107, Kamin & ReddyReview - 56 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 the middle of a computation, here adding a 10 millisecond delay to each iteration of the loop.


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

Similar presentations


Ads by Google