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.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Made with love, by Zachary Langley Applets The Graphics Presentation.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Graphical User Interfaces, 2D Graphics & Game Programming.
TCU CoSc Programming with Java Handling Events.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
Layout Managers Arranges and lays out the GUI components on a container.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
UID – Event Handling and Listeners Boriana Koleva
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
(c) by Elizabeth Sugar Boese.1 Chapter 6 Events - Lecture Slides.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Event Handling H_Func(Event) { } Event Receiver Object Source Object Registration.
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Computer Science 209 Graphics and GUIs.
Event-driven programming for GUI
Chapter 12 Event Handling
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Presentation transcript:

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

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.

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.

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); }

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.

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.

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

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

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.

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

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() ; } }

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.

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)

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:

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) {}... }

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:

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.)

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) {} }

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:

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); }

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() ; }

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:

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); }

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) {} }

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(); } }.... }

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()); }

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.