CS12420 - Lecture 04 Mice Lynda Thomas

Slides:



Advertisements
Similar presentations
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.
Advertisements

Event Handling.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Inner Classes in Java CMSC 432 Shon Vick. 2 Conceptual Structure package p 1 class A 1 class A 2... class A n... package n class A 1 class A 2... class.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Event Handling Events and Listeners Timers and Animation.
For IST410 Students only Events-1 Event Handling.
Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)
CSSE221: Software Dev. Honors Day 9 Announcements Announcements HW3 passed back, follow link from HW3 to its solution. HW3 passed back, follow link from.
Intermediate Java1 An example that uses inner classes Week Four Continued.
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.
PROGRAMMING GRAPHICAL USER INTERFACES Lecture 21 JAVA GUI BASICS Basic Java GUI Components –java.awt.Component, java.awt.Container, javax.swing.JComponent,
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.
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Inheritance & Interfaces. The Plan ● Motivate Inheritance ● Motivate Interfaces ● Examples ● Continue Practice on Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 CSE 331 More Events (Mouse, Keyboard, Window, Focus, Change, Document...) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
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.
CS Lecture 01 Frames and Components and events Lynda Thomas
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CS Lecture 00 Swing overview and introduction Lynda Thomas
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
Java's Graphical User Interface Toolkit
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.
 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.
Layout Managers Arranges and lays out the GUI components on a container.
For (int i = 1; i
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.
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
© Marty Hall, Larry Brown Web core programming 1 Handling Mouse and Keyboard Events.
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
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.
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.
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.
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.
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.
Events and Event Handling
Handling User Events with Swing
INFSY 547: WEB-Based Technologies
Computer Science 209 Graphics and GUIs.
GUI III IS
Programming in Java Event Handling
Quick intro to Event Driven Programming
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
ITEC324 Principle of CS III
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Programming Graphical User Interface (GUI)
ITEC324 Principle of CS III
Chapter 6 Inheritance.
Presentation transcript:

CS Lecture 04 Mice Lynda Thomas

In This Lecture Mouse interaction and its Listeners Mouse use and motion AWT :MouseListener comp.addMouseListener(MouseListener ml); AWT :MouseMotionListener comp.addMouseMotionListener(MouseMotionListener mml);

MouseListener void mouseClicked(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e) Called by runtime system

MouseMotionListener void mouseMoved(MouseEvent e) void mouseDragged(MouseEvent e) Called by runtime system

MouseEvent int getX() int getY() int getClickCount() boolean SwingUtilities.isLeftMouseButton(MouseEvent e) boolean SwingUtilities.isMiddleMouseButton(MouseEvent e) boolean SwingUtilities.isRightMouseButton(MouseEvent e) Also MouseEvent.getButton()

Mouse Project Specification Title position: x = 137 y = 53 number of clicks: 27 mouse is in component: yes Panel 300x300 Mouse pointer

So first the easy stuff The driver: Mouse-example/MouseEventDriver.java Brings up the Frame: Mouse-example/MouseEventFrame.java This contains two panels – the event panel and the status panel See handout 7

public class MouseEventFrame extends SimpleFrame { private MouseEventPanel mePanel; private StatusPanel stPanel; public MouseEventFrame() { this.setTitle("Mouse Click App"); mePanel = new MouseEventPanel(); stPanel = new StatusPanel(); add(mePanel,BorderLayout.CENTER); add(stPanel,BorderLayout.SOUTH); pack(); } private MyMousePositionListener mPL; private MyMouseListener mL; mPL = new MyMousePositionListener(stPanel); mePanel.addMouseMotionListener(mPL); mL = new MyMouseListener(stPanel); mePanel.addMouseListener(mL); Notice the pattern of create, add, make something listen just like with buttons

StatusPanel and MouseEventPanel are the two panels that are in the Frame

MyMouseListener and MyMousePositionListener are the two listeners (note you could have one listener that implemented both interfaces)

import java.awt.event.*; public class MyMousePositionListener implements MouseMotionListener { private StatusPanel statusPane;//link to status panel public MyMousePositionListener(StatusPanel sp) { statusPane = sp;//set up the link } public void mouseMoved(MouseEvent e) {//update the status panel statusPane.setCoordinates(e.getX(),e.getY()); } …. Notice how the Listener needs a link back to the status panel so it can update that panel – don’t be afraid to do that. For instance, suppose you wanted to draw on the event panel what would you do???

General Info on Listeners addXXXXListener(XXXXListener listener) Implementing listeners – several ways As a class on their own As internal classes As part of the application class As anonymous listeners

Own Class See for instance CounterListener.java in Lecture 01 (handout 3)

Internal Class The CounterPanel class could have an internal Listener class: public class CounterPanel extends JPanel { //... Including the call to new InternalCounterListener private class InternalCounterListener implements ActionListener { public void actionPerformed(ActionEvent e) { //code for this method taken from CounterListener.java }

Or Listening could be something another Application Class does public class CounterPanel extends JPanel implements ActionListener { //no need to have an instance of a Listener as the class is a Listener public CounterPanel(){ // code here... upButton.addActionListener(this); downButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { //code for this method taken from CounterListener }

Anonymous (on-the-fly) Listeners comp.addXXXXListener( //whole def of XXXXListener here ); upButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { increment(); } ); //similar code for downButton

Comparing Implementations As part of the application view class Easy : less interface experience required Confusing, less structured, limits listener’s range As internal classes When manipulating objects from one view class As a class on their own Elegant and efficient (But visibility of things to act on..?) When manipulating objects from many view classes As anonymous listeners No need to check which component triggered listener Can be difficult to understand

In This Lecture Mouse interaction Listeners

PS. What would it take to turn this into a drawing program JUST DO IT – SEE NEXT TOPIC See the play-drawing folder This represents a naïve view, but notice problems: –If I call super.paintComponent() then the screen gets cleaned –If I don’t: ‘artifacts’ Note what happens when you cover the window and uncover it Next we will solve these problems

In The Next Lecture We solve the problems in play-drawing and produce some Interactive graphics