Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.

Slides:



Advertisements
Similar presentations
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
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.
Mouse Events and Keyboard Events
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
For IST410 Students only Events-1 Event Handling.
Event-Driven Programming
Applet Class Hierarchy. Applet Methods called by the system public void init() When the applet is first loaded. Do initialization here. public void start()
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications Event.
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.
1 GUI 5Lecture 7 GUI - V Overview  Using No Layout Manager  Message Dialog Boxes  Running a Program as an Applet and an Application  More on Handling.
Intermediate Java1 An example that uses inner classes Week Four Continued.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
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.
1 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
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.
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.
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.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
OOP (Java): GUI II/ OOP Objectives – –describe some more GUI features: JPanel, and mouse listeners/adapters Semester 2,
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
 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.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
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.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
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,
COSC 4126 User Interaction User Interaction capturing and responding to input events.
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.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS Lecture 04 Mice Lynda Thomas
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
2/17/2016 DEPT OF CSE 1 EVENT HANDLING. 2/17/2016 DEPT OF CSE 2 Event handling is at the core of successful applet programming There are several types.
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.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
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.
CSI 3125, Preliminaries, page 1 Adapter Class. CSI 3125, Preliminaries, page 2 Adapter Class Java provides a special feature, called an adapter class,
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
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.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
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.
Programming in Java Event Handling
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
GUI Event Handling Nithya Raman.
CSE Software Engineering Fall 1999 Updated by J. Brown
ITEC324 Principle of CS III
Chapter 16 Event-Driven Programming
ITEC324 Principle of CS III
Presentation transcript:

Mouse Events

Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener  MouseMotionListener. The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked. The MouseMotionListener listens for actions such as dragging or moving the mouse.

MouseListener Methods F mouseEntered(MouseEvent e) F mouseExited(MouseEvent e) F mousePressed(MouseEvent e) F mouseReleased(MouseEvent e) F mouseClicked(MouseEvent e)

MouseMotionListener Methods F mouseDragged(MouseEvent e) F mouseMoved(MouseEvent e)

MouseEvent Class Since the MouseEvent class inherits InputEvent, you can use the methods defined in the InputEvent class: F public boolean ifAltDown() F public boolean ifControlDown() F public boolean ifShiftDown() F public boolean ifMetaDown() (the right mouse button is pressed)

Methods of MouseEvent Class F public int getClickCount() F public int getX() F public int getY() F public Point getPoint()

MouseMotionListener Methods F mouseDragged(MouseEvent e) F mouseMoved(MouseEvent e) Methods F public int getX() F public int getY() F public Point getPoint()

Example 1: mouse is clicked import javax.*; import java.awt.*; public class TestMouse { public TestMouse() { setTitle(“Test Mouse”); getContentPane().add(new MousePanel()); } public static void main(Strings[] args) { TestMouse frame = new TestMouse(); frame.setSize(300,300); frame.setVisible(true); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }

Example 1: mouse is clicked class MousePanel extends JPanel implements MouseListener { int mx, my; public MousePanel() { addMouseListener(this) } all // Implement all methods declared in MouseListener // interface! public void mousePressed(MouseEvent e) { } public void mouseReleassed(MouseEvent e) { } public void mouseExited (MouseEvent e) { } public void mouseFocus(MouseEvent e) { } public void mouseClicked(MouseEvent e) { mx = getX(); my = getY(); // System.out.println(“Clicked at (“+mx +“,”+my + ”)”); repaint(); }...

Example 1: Mouse is clicked … public void paintComponent(Graphics g) super.paintComponent(g); g.setColor(Color.red); g.drawRect(mx-20, my-20, 40, 40); } } // Inner Class MousePanel }// Outer Class TestMouse

Example 2: Mouse is dragged class MousePanel2 extends JPanel implements MouseMotionListener { int mx, my; // Register the Listener public MousePanel2() { addMouseMotionListener(this) } all // Implement all methods declared in MouseMotionListener // interface public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { mx = getX(); my = getY(); //System.out.println(“Mouse at (“ +mx + “,”+my+ ”)”); repaint(); }...

Example 2: Mouse is dragged … public void paintComponent(Graphics g) super.paintComponent(g); g.setColor(Color.red); g.fillRect(mx-2, my-2, 4, 4); } } // Inner Class MousePanel2 }// Outer Class TestMouse

Adapter Class vs. Listener Interface F Java provides a special feature, called an adapter class, that can simplify the creation of event handlers in certain situations. F An adapter class provides an empty implementation of all methods in an event listener interface. F Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. F You can define a new class to act as an event listener by extending one of the adapter classes and implementing only those events in which you are interested.

Example 3 with Adapter class MousePanel3 extends JPanel { // Register the event handler int mx, my; public MousePanel3() { addMouseListener(new MyMouseAdapter()); } // Inner class extends Adapter class class MyMouseAdapter extends MouseAdapter { // Implement methods you really need public void mouseClicked(MouseEvent e) { mx = getX(); my = getX(); //System.out.println(“Clicked at (“+mx+“,”+my+”)”); repaint(); }...

Example 3: Mouse is clicked … public void paintComponent(Graphics g) super.paintComponent(g); g.setColor(Color.red); g.fillRect(mx-20, my-20, 40, 40); } } // Inner Class MousePanel3 }// Outer Class TestMouse

Example 4: Draw shapes  Suppose, the method draw(Graphics g,...) is implemented for the classes Circle and Rectangle (sub-classes of the abstract class Shape ). We can draw array of Shapes. … public void paintComponent(Graphics g) super.paintComponent(g); for(int k = 0; k < shapes.length; k++) { shapes[k].draw(g, mx, my); } repaint(); } } // Inner Class MousePanel4 }// Outer Class TestMouse

Handling Complex Mouse Events F Objective: Create a program for drawing using a mouse. Draw by dragging with the left mouse button pressed; erase by dragging with the right button pressed. F See code and applet on web:  See more examples of applets on web:

Handling Keyboard Events  void keyPressed(KeyEvent e) Called when a key is pressed.  void keyReleased(KeyEvent e) Called when a key is released.  void keyTyped(KeyEvent e) Called when a key is pressed and then released. To process a keyboard event, use the following handlers in the KeyListener interface:

Handling Keyboard Events char keyChar; 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.getChar(); } repaint(); } VK_HOME, VK_PGUP, VK_PGDN, VK_ESCAPE, VKF1, …, VK12; VK_0, …, VK_9; VK_A, … VK_Z;...