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.

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

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
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.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
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.
TCU CoSc Programming with Java Handling Events.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS221 © 2007 Ray S. Babcock Menus, Toolbars, and a Mouse Appendix C.6-C.7.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Mouse Events and Keyboard Events
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.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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,
Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision.
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.
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.
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.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
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.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
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.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
2/5/00SEM107 © Kamin & Reddy Review -1 Class 19 - Review r This lecture contains a selection of slides from previous lectures, giving the “high points”
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.
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.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Event Handling CS 21a: Introduction to Computing I First Semester,
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
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.
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.
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
Chapter 12 Event-Driven Programming
Advanced User Interfaces
Lecture 09 Applets.
Computer Science 209 Graphics and GUIs.
Interfaces.
Event-driven programming for GUI
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Making Java GUIs Functional
Presentation transcript:

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

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

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

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:

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

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/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 ;

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.

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

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

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.

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

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

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.

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)

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:

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

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:

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

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

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

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:

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

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

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:

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

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