Lecture 11 Event Handling

Lecture 11 – Event Handling  Painting  Event types  Catching different event types  Getting information from components and events  Distinguishi...
Author: Mavis Bryant
2 downloads 1 Views 51KB Size
Lecture 11 – Event Handling  Painting  Event types  Catching different event types  Getting information from components and

events  Distinguishing between events of the same type  Reading: Big Java (3), Chapter 9 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 1

Painting  A JPanel has a method: void paintComponent(Graphics g)

using which one can define painting of arbitrary graphical shapes.  Library operations from Graphics class: g.drawLine(x1, y1, x2, y2) g.drawRect(x, y, xSize, ySize) g.fillRect(x, y, xSize, ySize) g.drawOval(x, y, xSize, ySize)

etc. 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 2

Color  Color is expressed in terms of RGB values: an

integer between 0 and 255 for each of Red, Green and Blue pigments.  The higher the number, the higher the color (which we might think of as brighter/lighter).  black – new Color(0, 0, 0)  white – new Color(255, 255, 255)  bright red – new Color(255, 0, 0)  g.setColor method sets the color for future painting operations. 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 3

Example – A red square public class PaintPanel extends JPanel { public PaintPanel() { setPreferredSize( new Dimension(100, 100)); } public void paintComponent(Graphics g) { super.paintComponent(g); // mandatory g.setColor(new Color(255, 0, 0)); g.fillRect(10, 10, 80, 80); } } 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 4

Who calls paintComponent?  We never call it directly.  The Java run-time system calls it whenever

it needs to paint the panel, e.g., when the user restores a window or moves it.  When a panel’s state changes and we want to update its display, we call the method void repaint()

which will eventually call paintComponent.

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 5

The Java event model  An event is any occurrence an application

might want to respond to, e.g.  user

clicks the mouse on a button,  user moves the mouse,  user enters text into a text field.  Java event model is a method of allowing

objects to respond to events.  The Java event model is based on classifying events into different types. 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 6

Event types  We will consider four types of events, and

explain how to write a panel to respond to each kind. A panel 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 event, e.g. mouse moves in a panel 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 7

Reading text from text field  Recall how a panel was written to respond to the

event of a user entering text in a text field (and then hitting the Enter key): declare panel listens to this type of event

class MyPanel extends JPanel implements ActionListener { JTextField t; “register” w/ text field public MyPanel () { ... t.addActionListener(this); ... } public void actionPerformed (ActionEvent e) { ... define required method } 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 8

Listening to events  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 with 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. 

 The following slides show how to do this for each

type of event.

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 9

Action events  Declare

panel: implements ActionListener  Register component: component.addActionListener(this);  Required methods: public void actionPerformed (ActionEvent e)

Action events are: user clicks on button; user hits the ENTER key in text field. E.g. the following panel respond to button click by drawing a rectangle in a darker gray 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 10

Action event example public class ActionPanel extends JPanel implements ActionListener { Button darken = new Button(“Darken”); int red = 255, green = 255, blue = 255; public ActionPanel () { add(darken); darken.addActionListener(this); } public void paintComponent (Graphics g) { super.paintComponent(g); g.setColor(new Color(red,green,blue)); g.fillRect(10,10,80,80); } public void actionPerformed (ActionEvent e) { red = red-10; green = green-10; blue = blue-10; repaint() ; } }

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 11

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.

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 12

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)

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 13

Mouse events  Declare

applet: implements MouseListener  Register component: no component to register; just write: addMouseListener(this);  Required methods: public public public public public

void void void void void

mousePressed (MouseEvent e) mouseReleased (MouseEvent e) mouseEntered (MouseEvent e) mouseExited (MouseEvent e) mouseClicked (MouseEvent e)

Example: Darken box when mouse is clicked: 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 14

Mouse event example public class MousePanel extends JPanel implements MouseListener { int red = 255, green = 255, blue = 255; public MousePanel () { addMouseListener(this); } public void paintComponent (Graphics g) { super.paintComponent(g); g.setColor(new Color(red,green,blue)); g.fillRect(10,10,80,80); } public void mouseClicked (MouseEvent e) { red = red-10; green = green-10; blue = blue-10; repaint(); } public void mousePressed (MouseEvent e) {} ... }

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 15

Getting information about an event  You can always get information about a

component by using instance methods from the components class, e.g. in JTextField: 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: 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 16

Getting information about an event  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.  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.) 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 17

Another mouse event example Place a small circle wherever mouse is clicked. public class MousePanel2 extends JPanel implements MouseListener { int x = 50, y = 50; public MousePanel2 () { 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) {} } 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 18

Which component caused the event?  There can be multiple event-producing

components.  Use getSource to figure out which one caused the event  E.g. The following applet can either lighten or darken the rectangle:

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 19

Action event example public class ActionPanel2 extends JPanel implements ActionListener { Button darken = new Button(“Darken”), lighten = new Button(“Lighten”); int red = 255, green = 255, blue = 255; public ActionPanel2 () { add(darken); darken.addActionListener(this); add(lighten); lighten.addActionListener(this); }

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 20

Action event example (cont.) public void paintComponent (Graphics g) { super.paintComponent(g); g.setColor(new Color(red,green,blue)); g.fillRect(10,40,100,50); } public void actionPerformed (ActionEvent e) { int delta; if (e.getSource == lighten) delta = 10; else if (e.getSource == darken) delta = -10; red = red+delta; green = green+delta; blue = blue+delta; repaint() ; } } 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 21

Catching different types of events  An applet/panel 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.  E.g. The following applet responds to button click by darkening box, and to mouse click by lightening it:

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 22

Action & mouse example public class MouseButtonPanel extends JPanel implements ActionListener, MouseListener { Button darken = new Button(“Darken”); int red = 255, green = 255, blue = 255; public MouseButtonPanel () { add(darken); darken.addActionListener(this); addMouseListener(this); } public void paintComponent (Graphics g) { super.paintComponent(g); g.setColor(new Color(red,green,blue)); g.fillRect(10,10,100,50); } 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 23

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) {} } 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 24

Inner classes  Java supports inner classes, i.e., classes defined

inside other classes.  Inner classes are often used to define listeners for events. public class ActionPanel3 extends JPanel { int darkness = 128; private class DarkenListener implements ActionListener { public void actionPerformed(ActionEvent e) { darkness = darkness-10; repaint(); } } .... } 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 25

Inner classes (cont.)  The inner class can access the instance variables of the outer class (e.g., darkness) as well as the methods (e.g., repaint).  An instance of the inner class can be supplied as

the listener for events: public class ActionPanel3 extends JPanel { .... JButton darken = new JButton(“Darken”); public ActionPanel3() { add(darken); darken.addActionListener( new DarkenListener()); } } 2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 26

Why use inner classes?  We can handle each event separately in a

self-contained method, e.g.,  DarkenListener.actionPerformed  LightenListener.actionPerformed

 Leads to better structure when a class has to

implement listeners for a large number of events.

2013-14, Sem2

MSc Workshop - © S. Kamin, U.Reddy

Lect 11 - Events - 27