 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.

Slides:



Advertisements
Similar presentations
Event Handling.
Advertisements

Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Mouse Events and Keyboard Events
Event Handling Events and Listeners Timers and Animation.
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
Applet Class Hierarchy. Applet Methods called by the system public void init() When the applet is first loaded. Do initialization here. public void start()
Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Concurrent Programming and Threads Threads Blocking a User Interface.
For (int i = 1; i
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
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.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
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.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
CS Lecture 04 Mice Lynda Thomas
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה. בשבוע שעבר  paint  Graphics  repaint.
Nested Classes and Event Handling Chapter 10. Overview We explain how to write Java code that responds to events. The Timer class can be used to respond.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
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 Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
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.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Event Handling and Listeners in SWING The practice of event handling.
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.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Events and Event Handling
Chapter 14 Event-Driven Programming
Lecture 8 Object Oriented Programming Using Java
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Computer Science 209 Graphics and GUIs.
GUI III IS
Programming in Java Event Handling
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
GUI Event Handling Nithya Raman.
GUI Programming III: Events
Event-driven programming for GUI
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in response to an events  Definition: An adapter is a class that has default listener methods  Implementation:  Implement the listener interface or extend an adapter class  Create listener methods appropriately  Register the listener to a component or a listener manager  Note: An alternate implementation is to create an anonymous listener object passed as an argument when registering.

 Listen for button clicks public class MyPanel extends JPanel implements ActionListener { Jbutton b; public MyPanel() { JButton b = new JButton(“hello”); add(b); b.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { JButton x = (JButton)(e.getSource()); System.out.println (“it’s a ” + x.getText() + “ button”); } } } Note: A single listener can repond to events from multiple objects

 Purpose  Listen for user keyboard entries  Limitation: the component must have focus  Implementation  Create keyPressed, keyTyped, and keyReleased methods if using the implements KeyListener interface  Create only the methods needed if using a KeyAdapter  Register the listener with the component

class KeyPanel extends JPanel { private int x=100, y=100; String keyChar = ‘x'; public KeyPanel() { setFocusable(true); // Doesn’t work for JApplets addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_DOWN: y+=10; break; case KeyEvent.VK_UP: y-=10; break; case KeyEvent.VK_LEFT: x-=10; break; case KeyEvent.VK_RIGHT: x+=10; break; default: keyChar = “” + e.getKeyChar(); } repaint(); }}); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString(keyChar,x,y);}}

 Differences from KeyListener objects  Don’t need to worry about whether a component has focus  Monitors keyboard events before they are sent to components with focus  The listener method is dispatchKeyEvent, not keyPressed  Register the Listener with the system KeyboardFocusManager  Useful for applets, because applet JPanels won’t get focus  Implementation KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); manager.addKeyEventDispatcher(new KeyEventDispatcher() { public boolean dispatchKeyEvent(KeyEvent event) { /* code goes here; return true; } });

 Mouse Listener Events: Mouse operations  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) {}  Mouse Motion Listener: Movements of the mouse  public void mouseDragged(MouseEvent e) {}  public void mouseMoved(MouseEvent e) {}  Item Listener: State of an object changed  Public void itemStateChanged(ItemEvent e) {}  Even more: FocusListener, WindowListener, etc.

 Definition: A path of execution through a process  Any Class can be a thread if it:  implements runnable  provides a run method (public void run())  A thread starts by calling start(), which in turn calls run()  A thread ends when the run method exits  Be careful  Threads all run concurrently; order of execution is not known  One thread can step on variables set in another thread

public class MovingPanel extends JPanel implements ActionListener { private String message = “Welcome to Java” private int x=0, y = 20; public MovingPanel() {Timer timer=new Timer(1000,this); timer.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString(message, x, y); } public void actionPerformed(ActionEvent e) { if (x>getWidth() { x = -20; } x+=5; if (y>=getHeight() – 20) y = 20; y+=5; repaint(); }

int data[][] = new int[4][4], count = 1; for (int row=0; row<data.length; row++) for (int col=0; col<data[row].length; col++) data[row][col] = count++; OR int[][] data = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} }; for (int row=0; row<data.length; row++) { for (int col=0; col<data[row].length; col++) System.out.printf("%4d", data[row][col]); System.out.println(); } [[0][1][2][3] [0]1234 [1]5678 [2] [3]

count=1; int values[][] = new int[4][], cols[] = {2, 4, 1,3}, count = 1; for (int row=0; row<values.length; row++) { values[row] = new int[cols[row]]; for (int col=0; col<values[row].length; col++) values[row][col] = count++; } for (int row=0; row<values.length; row++) { for (int col=0; col<values[row].length; col++) System.out.printf("%4d", values[row][col]); System.out.println(); } Definition: Different rows have different numbers of columnt

public double distance(Point first, Point second) { double delta1 = Math.pow(first.x – second.x, 2); double delta2 = Math.pow(first.y – second.y), 2; return Math.sqrt(delta1 + delta2); }  Consider a two dimensional array, data  Problem: Find the pair of points closest together using the above method.

 Moving sprites in four dimensions  int[][] directions = { {0,1}, {1,0}, {0,-1}, {-1,0}};  Moving sprites in eight directions  int[][] directions = { {0,2}, {1,1}, {2,0}, {1,-1}, {0,-2}, {-1, -1}, {0,-2}, { -1,1} };  Moving sprites in sixteen directions  int[][] directions = { { 4, 0}, { 3, 1}, { 2, 2}, { 1, 3}, { 0, 4}, {-1, 3}, {-2, 2}, {-3, 1}, {-4, 0}, {-3,-1}, {-2,-2}, {-1,-3}, { 0,-4}, { 1,-3}, { 2,-2}, { 3,-1} };  Questions:  What is the relation between the directions and the one that is half away in the array?  How would we extend this to twice the size?

 Divide the panel into row and column cells.  Define rules for what happens if predators or avatars intersect the various cells  Examples of possible game rules  Move them automatically to another place  Have the game shift to another room  Give the players points or create additional avatars  Kill the avatar that hits that particular cells  The rules for avatars may vary from that of predators

 First dimension: month  Second dimension: day  Third dimension: hour  Fourth dimension : Type of measurement [0] = temperature, [1] = humidity  Symbolic constants: public final static int T=0, H=1;  Questions:  To what does the following refer? int x=3, y=4, z=5, k=32, m=50; data[x][y][z][T] = 32; data[x][y][z][H] = 50;  How do we instantiate such an array?