1 / 23

Events

Events. Event types Catching different event types Getting information from components and events Distinguishing between events of the same type. The Java event model.

senta
Download Presentation

Events

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Events • Event types • Catching different event types • Getting information from components and events • Distinguishing between events of the same type SEM107, Kamin & Reddy

  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. SEM107, Kamin & Reddy

  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 SEM107, Kamin & Reddy

  4. 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 SEM107, Kamin & Reddy

  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) SEM107, Kamin & Reddy

  6. 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) {} } SEM107, Kamin & Reddy

  7. Classes and objects • Every class has objects, or instances, created using new. Think of the class as an object-producing machine. Button new TextField new SEM107, Kamin & Reddy

  8. Example: Point3D Three-dimensional points consist of three values, the x, y, and z coordinates. x z y Point3D new x z y SEM107, Kamin & Reddy

  9. Instance variables containing objects (cont.) Client: Box3D box1 = new Box3D(new Point3D()); box1 c1 c2 x 1 z 1 y 1 x 0 z 0 y 0 SEM107, Kamin & Reddy

  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); } } SEM107, Kamin & Reddy

  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”); SEM107, Kamin & Reddy

  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)); } SEM107, Kamin & Reddy

  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. SEM107, Kamin & Reddy

  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); } SEM107, Kamin & Reddy

  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! SEM107, Kamin & Reddy

  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; } . . . SEM107, Kamin & Reddy

  17. Iteration • Traditional method of repeating statements (not using recursion) loop control{ --------------- --------------- } repeat these statements zero or more times, controlled by loop control SEM107, Kamin & Reddy

  18. While loops contains variables that are changed in loop while (condition) statement repeat until condition becomes true Keep in mind that statement can be a compound statement. SEM107, Kamin & Reddy

  19. For loops init; while (cond) { S incr; }  for (init; cond; incr) S SEM107, Kamin & Reddy

  20. Do-while loops S; while (cond) S do S while (cond)  SEM107, Kamin & Reddy

  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); } SEM107, Kamin & Reddy

  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(); } } SEM107, Kamin & Reddy

  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 the middle of a computation, here adding a 10 millisecond delay to each iteration of the loop. SEM107, Kamin & Reddy

More Related