12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.

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.
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.
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
TCU CoSc Programming with Java Handling Events.
© The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces.
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.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
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.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
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.
Welcome to CIS 083 ! Events CIS 068.
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,
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.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
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.
– 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.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Layout Managers Arranges and lays out the GUI components on a container.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
For (int i = 1; i
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
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.
Object Oriented Programming.  Interface  Event Handling.
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”
17/3/00SEM107 © Kamin & Reddy Class 13 - Animation 1 Class 13 - Animation r Summary of loops m while-do m for m do-while r while loop examples r for loop.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
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,
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.
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.
Event Handling CS 21a: Introduction to Computing I First Semester,
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
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.
Chapter 22 Selection and Iteration if ( Boolean Expression ) true statement ; else false statement ; OperatorMeaning ! Logical not < Less than
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.
Advanced User Interfaces
Lecture 09 Applets.
Interfaces.
Ellen Walker Hiram College
CS 106A, Lecture 14 Events and Instance Variables
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing between events of the same type

12/5/00SEM107, Kamin & ReddyReview - 35 The Java event model An event is any occurrence an applets might want to respond to, e.g. user clicks mouse on button, user moves mouse, user enters text into text field. Java event model is a method of allowing applets to respond to events. The Java event model is based on classifying events into different types.

12/5/00SEM107, Kamin & ReddyReview - 36 Event types 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. 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 even, e.g. mouse moves in applet

12/5/00SEM107, Kamin & ReddyReview - 37 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

12/5/00SEM107, Kamin & ReddyReview - 38 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)

12/5/00SEM107, Kamin & ReddyReview - 39 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) {} }

12/5/00SEM107, Kamin & ReddyReview - 40 Classes and objects Every class has objects, or instances, created using new. Think of the class as an object- producing machine. new Button TextField

12/5/00SEM107, Kamin & ReddyReview - 41 Example: Point3D Three-dimensional points consist of three values, the x, y, and z coordinates. new Point3D x y z x y z

12/5/00SEM107, Kamin & ReddyReview - 42 Instance variables containing objects (cont.) Client: Box3D box1 = new Box3D(new Point3D()); x y z box x y z c1c2

12/5/00SEM107, Kamin & ReddyReview - 43 Instance methods Methods defined without static keyword are instance methods. Unlike class methods, instance methods can refer to instance variables. class Point3D { double x, y, z; Point3D () { x=0; y=0; z=0; } void print () { System.out.print(x+”,”+y+”,”+z); }

12/5/00SEM107, Kamin & ReddyReview - 44 Example: Appointment class (cont.) class Appointment { int time; int duration; String description; Appointment (int t, int d, String s) { time = t; duration = d; description = s; } Client: dr_appt = new Appointment(9*60+30, 60, “Dr. No”);

12/5/00SEM107, Kamin & ReddyReview - 45 Instance methods calling instance methods When an instance method im1 calls another instance method im2, it does not have to name a receiver. By default, the receiver of im2 will be the receiver of im1. boolean overlaps (Appointment appt) { return ((time <= appt.time && endingTime() > appt.time) || (appt.time <= time && appt.endingTime() > time)); }

12/5/00SEM107, Kamin & ReddyReview - 46 “ this ” Methods can refer to the receiver as a whole - instead of just the receiver’s instance variables - by using variable this. In every instance method in a class C, “ this ” is implicitly declared to be a variable of type C.

12/5/00SEM107, Kamin & ReddyReview - 47 “ this ” (cont.) More important uses are when instance method needs to pass entire object to another method. E.g. rewrite overlaps : In applets, “register” component by: b.addActionListener(this); boolean overlaps (Appointment appt) { if (time <= appt.time) return endingTime() > appt.time; else return appt.overlaps(this); }

12/5/00SEM107, Kamin & ReddyReview - 48 Private instance variables Often, you don’t want clients of a class to be able to access its instance variables. To prevent them from doing so, declare the variables private. class Appointment { private int time; private int duration; private String description;... } Client: Appointment dr = new Appointment(...);... dr.time... // compile error!

12/5/00SEM107, Kamin & ReddyReview - 49 Why private instance variables? An Appointment object represents an appointment at a particular time. There are many ways to represent appointments. Here are two alternatives: class Appointment { private int time; private int endtime;... int endingTime () { return endtime; }... class Appointment { private int hour, minute; private int duration; String description; int endingTime () { return hour*60+minute+ duration; }...

12/5/00SEM107, Kamin & ReddyReview - 50 Iteration Traditional method of repeating statements (not using recursion) loop control { } repeat these statements zero or more times, controlled by loop control

12/5/00SEM107, Kamin & ReddyReview - 51 While loops while ( condition ) statement contains variables that are changed in loop repeat until condition becomes true Keep in mind that statement can be a compound statement.

12/5/00SEM107, Kamin & ReddyReview - 52 For loops for ( init ; cond ; incr ) S init ; while ( cond ) { S incr ; } 

12/5/00SEM107, Kamin & ReddyReview - 53 Do-while loops do S while ( cond ) S ; while ( cond ) S 

12/5/00SEM107, Kamin & ReddyReview - 54 Aside: Rolling forever Can have applet that simply rolls eyes forever. However, can’t do it like this: because it would tie up the browser. Instead, need to relinquish control to the browser occasionally. public void paint(Graphics g) { int i; for (i=0; i<360; i = (i+1)%360) pair1.draw(g, 2*Math.PI*i/360.0); }

12/5/00SEM107, Kamin & ReddyReview - 55 Aside: Rolling forever (cont.) The new version of RollingEyesApplet : public class RollingEyesApplet6 extends Applet { RollingEyes5 pair1; int lastangle = 0; public void init () { pair1 = new RollingEyes5(200,100); } public void paint(Graphics g) { pair1.draw(g, 2*Math.PI*lastangle/360.0); lastangle = (lastangle+1) % 360; repaint(); }

12/5/00SEM107, Kamin & ReddyReview - 56 Example: Slower rolling eyes (cont.) public void paint(Graphics g) { int i; for (i=0; i<360; i = i+1) { pair1.draw(g, 2*Math.PI*i/360.0); try { Thread.sleep(10); } catch (InterruptedException t) {} } The highlighted portion creates a pause in the middle of a computation, here adding a 10 millisecond delay to each iteration of the loop.