Presentation is loading. Please wait.

Presentation is loading. Please wait.

7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.

Similar presentations


Presentation on theme: "7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching."— Presentation transcript:

1 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching different event types r Getting information from components and events r Distinguishing between events of the same type

2 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 2 Component sizes Each component type has an instance method getSize() that returns its dimensions, as an object of type Dimension, e.g. r Can also get the dimensions of the entire applet: // If b1 is a Button: Dimension b1size = b1.getSize(); Dimension appsize = getSize();

3 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 3 Component sizes (cont.)  From a Dimension object like b1size, you can find the width and height of the component by These are both int values.  For example, to print the height of the applet: System.out.println(getSize().height); b1size.height // height of the button b1size.width // width of the button

4 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 4 The switch statement  The switch statement is another form of conditional statement. It executes one among a set of statements, depending upon the value of an int -valued expression. r Syntax:

5 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 5 switch statement syntax switch ( expression ) { [case-clause]* [default-clause] } switch-statement case integer-constant : [statement]* break; case-clause Default-clause default: statement

6 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 6 switch statement syntax (cont.) r In other words, a switch statement looks like r This behaves like: switch ( expr ) { case i1 : statement1 ; break; case i2 : statement2 ; break;... case in : statementn ; break; default: statement ; }

7 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 7 switch statement syntax (cont.) r If the default part is omitted, then if the expression does not match any of the constants, nothing happens. int i = expr ; if (i== i1 ) statement1 ; else if (i== i2 ) statement2 ;... else if (i== in ) statementn ; else statement ;

8 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 8 The Java event model r An event is any occurrence an applet might want to respond to, e.g. user clicks mouse on button, user moves mouse, user enters text into text field. r Java event model is a method of allowing applets to respond to events. r The Java event model is based on classifying events into different types.

9 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 9 Event types r 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. r Major event types: m Action events, e.g. button click m Item event, e.g. click check box m Mouse event, e.g. mouse button click m Mouse motion even, e.g. mouse moves in applet

10 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 10 Reading text from text field r Recall how an applet was written to respond to the event of a user entering text in a text field (and then hitting the Enter key): class appletname extends Applet implements ActionListener { TextField t; public void init () {... t.addActionListener(this);... } public void actionPerformed (ActionEvent e) {... } declare applet listens to this type of event “register” text field define required method

11 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 11 Listening to events r For an applet to listen to any of the types of events, it must do these three things:  Declare itself to listen to that type of event  Register any components that can trigger the event. (If the event is a mouse event, it doesn’t need to be registered.)  Define method(s) required to listen to that type of event. r Following slides show how to do this for each type of event.

12 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 12 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

13 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 13 Action event example public class ActionApplet extends Applet implements ActionListener { Button darken = new Button(“Darken”); int red = 255, green = 255, blue = 255; public void init () { add(darken); darken.addActionListener(this); } public void paint (Graphics g) { g.setColor(new Color(red,green,blue)); g.fillRect(10,40,100,50); } public void actionPerformed (ActionEvent e) { red = red-10; green = green-10; blue = blue-10; repaint() ; }

14 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 14 Item events  Declare applet: implements ItemListener  Register component: component.addItemListener(this);  Required methods: public void itemStateChanged (ItemEvent e) Item events are: user clicks on checkbox.

15 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 15 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)

16 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 16 Mouse events  Declare applet: implements MouseListener  Register component: no component to register; just write: addMouseListener(this);  Required methods: public void mousePressed (MouseEvent e) public void mouseReleased (MouseEvent e) public void mouseEntered (MouseEvent e) public void mouseExited (MouseEvent e) public void mouseClicked (MouseEvent e) Example: Darken box when mouse is clicked:

17 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 17 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) {} }

18 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 18 Getting information about an event  You can always get information about a component by using instance methods from the components class, e.g. in TextField : String getText()  When an event occurs, you can get information from the event object that is passed as argument e to the event’s method. The type of information depends upon the type of event:

19 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 19 Getting information about an event (cont.) r Action events and item events: Use e.getSource() to find out which text field or button or check box was the source of the event. r Mouse events: Use e.getPoint() to find the location of the mouse. (This returns a Point object; use “.x ” and “.y ” to find x and y coordinates.)

20 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 20 Another mouse event example Place a small circle wherever mouse is clicked. public class MouseApplet2 extends Applet implements MouseListener { int x = 50, y = 50; public void init () { addMouseListener(this); } public void paint (Graphics g) { g.drawOval(x,y,20,20); } public void mouseClicked (MouseEvent e) { Point p = e.getPoint(); x = p.x; y = p.y; repaint(); } public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e) {} }

21 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 21 Aside: the update method r In the previous example, when the mouse button is clicked, the previous circle is erased before the new one is drawn. To prevent this, add this definition: Here’s why: repaint calls update, which normally erases the window and then calls paint. This changes the definition of update. public void update (Graphics g) { paint(g); }

22 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 22 Which component caused the event? r There can be multiple event-producing components.  Use getSource to figure out which one caused the event r E.g. following applet can either lighten or darken the rectangle:

23 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 23 Action event example public class ActionApplet2 extends Applet implements ActionListener { Button darken = new Button(“Darken”), lighten = new Button(“Lighten”); int red = 255, green = 255, blue = 255; public void init () { add(darken); darken.addActionListener(this); add(lighten); lighten.addActionListener(this); }

24 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 24 Action event example (cont.) public void paint (Graphics g) { g.setColor(new Color(red,green,blue)); g.fillRect(10,40,100,50); } public void actionPerformed (ActionEvent e) { int incr = (e.getSource() == lighten) ? 10 : -10; red = red+incr; green = green+incr; blue = blue+incr; repaint() ; }

25 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 25 Catching different types of events r An applet can catch events of any or all types. It just declares that it wants to catch events of those types, registers all components, and defines all required operations. r E.g. following applet responds to button click by darkening box, and to mouse click by lightening it:

26 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 26 Action & mouse example public class MouseButtonApplet extends Applet implements ActionListener, MouseListener { Button darken = new Button(“Darken”); int red = 255, green = 255, blue = 255; public void init () { add(darken); darken.addActionListener(this); addMouseListener(this); } public void paint (Graphics g) { g.setColor(new Color(red,green,blue)); g.fillRect(10,10,100,50); }

27 7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 27 Action & mouse example (cont.) public void actionPerformed (ActionEvent e) { red = red-10; green = green-10; blue = blue-10; repaint() ; } 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) {} }


Download ppt "7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching."

Similar presentations


Ads by Google