Download presentation
Presentation is loading. Please wait.
Published bySteven Joseph Modified over 8 years ago
1
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types r Getting information from components and events r Distinguishing between events of the same type r Reading: Horstmann, Chapter 10
2
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 2 Painting r A JPanel has a method: void paintComponent(Graphics g) using which one can define painting of arbitrary graphical shapes. r 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.
3
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 3 Color r Color is expressed in terms of RGB values: an integer between 0 and 255 for each of Red, Green and Blue pigments. r The higher the number, the higher the color (which we might think of as brighter/darker). r white – new Color(0, 0, 0) r black – new Color(255, 255, 255) r bright red – new Color(255, 0, 0) r g.setColor method sets the color for future painting operations.
4
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 4 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); }
5
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 5 Who calls paintComponent? r We never call it directly. r 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. r When a panel’s state changes and we want to update its display, we call the method void repaint() which will eventually call paintComponent.
6
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 6 The Java event model r An event is any occurrence an application might want to respond to, e.g. m user clicks the mouse on a button, m user moves the mouse, m user enters text into a 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.
7
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 7 Event types r 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. 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 a panel
8
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 8 Reading text from text field r 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): class MyPanel extends JPanel implements ActionListener { JTextField t; public MyPanel () {... t.addActionListener(this);... } public void actionPerformed (ActionEvent e) {... } declare panel listens to this type of event “register” w/ text field define required method
9
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 9 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 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. r The following slides show how to do this for each type of event.
10
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 10 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 applet respond to button click by drawing a rectangle in a darker gray
11
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 11 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() ; } }
12
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 12 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.
13
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 13 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)
14
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 14 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:
15
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 15 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) {}... }
16
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 16 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:
17
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 17 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.)
18
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 18 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) {} }
19
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 19 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. The following applet can either lighten or darken the rectangle:
20
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 20 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); }
21
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 21 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() ; }
22
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 22 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. The following applet responds to button click by darkening box, and to mouse click by lightening it:
23
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 23 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); }
24
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 24 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) {} }
25
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 25 Inner classes r Java supports inner classes, i.e., classes defined inside other classes. r 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(); } }.... }
26
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 26 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 ). r 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()); }
27
2004-01-30 MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 27 Why use inner classes? r We can handle each event separately in a self-contained method, e.g., m DarkenListener.actionPerformed m LightenListener.actionPerformed r Leads to better structure when a class has to implement listeners for a large number of events.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.