© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.

Slides:



Advertisements
Similar presentations
Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
Advertisements

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.
Web Design & Development Lecture 19. Java Graphics 2.
 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.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Painter Application Introducing Graphics with Windows Presentation Foundation.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Event Handling Events and Listeners Timers and Animation.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
GUI Programming in Java
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
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 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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
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.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– 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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
(c) by Elizabeth Sugar Boese.1 Chapter 6 Events - Lecture Slides.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
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.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Event Handling H_Func(Event) { } Event Receiver Object Source Object Registration.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Computer Science 209 Graphics and GUIs.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Presentation transcript:

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application 21.2 Constructing the Painter Application 21.3 Interfaces 21.4 The mousePressed Event Handler 21.5 The mouseReleased Event Handler 21.6The mouseDragged Event Handler 21.7 Wrap-Up Tutorial 21 – “Cat and Mouse” Painter Application Introducing Interfaces, Mouse Input; the Event- Handling Mechanism

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objective In this tutorial, you will learn to: –Use mouse events to allow user interaction with an application. –Use the mousePressed, mouseReleased and mouseDragged event handlers. –Use the Graphics object to draw circles on a JPanel. –Determine which mouse button was pressed.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application (Cont.) Figure 21.1 Painter application before drawing.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application (Cont.) Figure 21.2 Drawing on the Painter application’s DrawJPanel. Drawing lines composed of small, colored circles To draw on the JDrawPanel, click and hold the left mouse button and drag the mouse

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application (Cont.) Figure 21.3 Drawing a cat and a computer mouse on the DrawJPanel. Be creative – draw a cat and a computer mouse on the JDrawPanel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application (Cont.) Figure 21.4 Erasing part of the drawing. Erasing by drawing circles that are the same color as the DrawJPanel ’s background To erase, click and hold the right mouse button and drag it over part of your drawing

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Painter Application When the mouse is pressed Store the mouse’s location If the left mouse button is pressed Set the color to blue Set the diameter for drawing Else Set the color to the DrawJPanel’s background color Set the diameter for erasing Repaint the DrawJPanel When the mouse is dragged Store the mouse’s location Repaint the DrawJPanel When the paintComponent method is called Set the drawing color Draw a circle with the appropriate diameter at the mouse’s location

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Painter Application (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces Event source –The GUI component with which the user interacts Event listener –The object that is notified by the event source when an event occurs Interface –Describes what a class does –But not how it is done Implementing the interface –Declare all the methods in the interface

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces (Cont.) Figure 21.6 Calling the addMouseListener method. Adding a MouseListener object The MouseListener interface declares event handler headers for mouse events.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces (Cont.) Creating an anonymous inner class Figure 21.7 Creating an instance of the MouseListener interface.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces (Cont.) Inner class –Declared inside another class –Anonymous inner class Inner class with no name MouseEvent –Generated when the mouse is used to interact with an application

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces (Cont.) Figure 21.8 Declaring event handlers for the MouseListener interface. Empty event handlers in the anonymous inner class

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler Figure 21.9 Coding the mousePressed event handler. Adding code to the mousePressed event handler mousePressed event handler is called when the mouse button is pressed on a component

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Storing the position of the mouse cursor. Calling method getPoint to store the mouse’s position The getPoint method returns a Point object

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Setting the color and diameter of the circle.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Setting the color using method setColor.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Drawing the circle using method fillOval. Calling method fillOval The fillOval method of class Graphics draws a filled oval.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure General oval. Bounding box A bounding box specifies an oval’s upper-left x - and y - coordinates, width and height

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Running the application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler Figure Declaring constants for the released circle’s color and size.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler (Cont.) Figure Coding the mouseReleased event handler. Adding code to the mouseReleased event handler

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler (Cont.) Figure Storing the location of the mouse’s cursor. Coding the drawJPanelMouseReleased method

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler (Cont.) Figure Setting the color and size of the circle to be drawn.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler (Cont.) Figure Running the application. Drawing a flower using only mouseReleased and mousePressed event handlers

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler Figure Adding constants for the erasing circle. The getBackground method returns a Color object representing the color of the DrawJPanel ’s background

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Removing the method call to drawJPanelMouseReleased.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Using the isMetaDown method to determine which mouse button is pressed. Determining which mouse button is pressed The isMetaDown method returns true when the user presses the right mouse button on a mouse with two or three buttons

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) MouseMotionListener interface –Declares event handlers for mouse events –mouseDragged event handler Called when a mouse button is pressed and the mouse is dragged. –mouseMoved event handler Called when the mouse is moved without any buttons pressed.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Calling method addMouseMotionListener. Adding a MouseMotionListener to the DrawJPanel The addMouseMotionListener method of the DrawJPanel class registers an event listener with an event source

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Creating an anonymous inner class. Creating an anonymous inner class that implements the MouseMotionListener interface

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Coding the mouseDragged event handler.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Storing the location of the mouse’s cursor.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Running the completed Painter application.

 2004 Prentice Hall, Inc. All rights reserved. Outline 36 DrawJPanel.java (1 of 6) 1 // Tutorial 21: DrawJPanel.java 2 // This class allows the user to draw and erase on the application. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 public class DrawJPanel extends JPanel 8 { 9 // Point to hold the mouse cursor's location 10 private Point currentPoint; // constants for the drawn circle 13 private final Color DRAW_COLOR = Color.BLUE; 14 private final int DRAW_DIAMETER = 8; // constants for the erase circle 17 private final Color ERASE_COLOR = this.getBackground(); 18 private final int ERASE_DIAMETER = 8; // instance variables for the circle 21 private Color drawColor; 22 private int drawDiameter; 23

 2004 Prentice Hall, Inc. All rights reserved. Outline 37 DrawJPanel.java (2 of 6) 24 // constructor 25 public DrawJPanel() 26 { 27 addMouseListener( new MouseListener() // anonymous inner class 30 { 31 // event handler called when a mouse button is clicked 32 public void mouseClicked( MouseEvent event ) 33 { 34 } // event handler called when mouse enters this DrawJPanel 37 public void mouseEntered( MouseEvent event ) 38 { 39 } // event handler called when mouse exits this DrawJPanel 42 public void mouseExited( MouseEvent event ) 43 { 44 } 45 Adding a MouseListener Creating an anonymous inner class Empty event handlers in the anonymous inner class. To implement the MouseListener, these methods must be implemented

 2004 Prentice Hall, Inc. All rights reserved. Outline // event handler called when a mouse button is pressed 47 public void mousePressed( MouseEvent event ) 48 { 49 drawJPanelMousePressed( event ); 50 } // event handler called when a mouse button is released 53 public void mouseReleased( MouseEvent event ) 54 { 55 } } // end anonymous inner class ); // end call to addMouseListener addMouseMotionListener( new MouseMotionListener() // anonymous inner class 64 { 65 // event handler called when the mouse is dragged 66 public void mouseDragged( MouseEvent event ) 67 { 68 drawJPanelMouseDragged( event ); 69 } 70 DrawJPanel.java (3 of 6) Calling the drawJPanelMousePressed method when the mouse is pressed Calling the drawJPanelMouseDragged method when the mouse is dragged Empty event handlers in the anonymous inner class Adding a MouseMotionListener to the DrawJPanel Creating an anonymous inner class that implements the MouseMotionListener interface

 2004 Prentice Hall, Inc. All rights reserved. Outline 39 DrawJPanel.java (4 of 6) 71 // event handler called when the mouse is moved 72 public void mouseMoved( MouseEvent event ) 73 { 74 } } // end anonymous inner class ); // end call to addMouseMotionListener } // end constructor // draw a circle on this DrawJPanel 83 private void drawJPanelMousePressed( MouseEvent event ) 84 { 85 // store the location of the mouse 86 currentPoint = event.getPoint(); if ( event.isMetaDown() ) // right mouse button is pressed 89 { 90 drawColor = ERASE_COLOR; 91 drawDiameter = ERASE_DIAMETER; 92 } Determining which mouse button was pressed Calling method getPoint to store the mouse’s position

 2004 Prentice Hall, Inc. All rights reserved. Outline 40 DrawJPanel.java (5 of 6) 93 else // left mouse button is pressed 94 { 95 drawColor = DRAW_COLOR; 96 drawDiameter = DRAW_DIAMETER; 97 } repaint(); // repaint this DrawJPanel } // end method drawJPanelMousePressed // draw a small circle at the mouse's location 104 public void paintComponent( Graphics g ) 105 { 106 g.setColor( drawColor ); // set the color if ( currentPoint != null ) 109 { 110 // draw a filled circle at the mouse's location 111 g.fillOval( currentPoint.x, currentPoint.y, 112 drawDiameter, drawDiameter ); 113 } } // end method paintComponent 116 Using the Graphics object to draw a circle

 2004 Prentice Hall, Inc. All rights reserved. Outline 41 DrawJPanel.java (6 of 6) 117 // draw a circle on this DrawJPanel 118 private void drawJPanelMouseDragged( MouseEvent event ) 119 { 120 // store the location of the mouse in currentPoint 121 currentPoint = event.getPoint(); repaint(); // repaint this DrawJPanel } // end method drawJPanelMouseDragged } // end class DrawJPanel

 2004 Prentice Hall, Inc. All rights reserved. Outline 42 Painter.java (1 of 2) 1 // Tutorial 21: Painter.java 2 // Application enables user to draw on a subclass of JPanel. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import java.util.*; 7 8 public class Painter extends JFrame 9 { 10 // DrawJPanel for circles drawn by user 11 private DrawJPanel myDrawJPanel; // no-argument constructor 14 public Painter() 15 { 16 createUserInterface(); 17 } // set up the GUI components 20 public void createUserInterface() 21 { 22 // get content pane for attaching GUI components 23 Container contentPane = getContentPane(); 24

 2004 Prentice Hall, Inc. All rights reserved. Outline 43 Painter.java (2 of 2) 25 // enable explicit positioning of GUI components 26 contentPane.setLayout( null ); // set up myDrawJPanel 29 myDrawJPanel = new DrawJPanel(); 30 myDrawJPanel.setBounds( 0, 0, 300, 300 ); 31 contentPane.add( myDrawJPanel ); // set properties of application's window 34 setTitle( "Painter" ); // set title bar text 35 setSize( 300, 300 ); // set window size 36 setVisible( true ); // display window } // end method createUserInterface // main method 41 public static void main( String[] args ) 42 { 43 Painter application = new Painter(); 44 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class Painter