© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Advertisements

Chapter 9 Color, Sound and Graphics
COMPUTER PROGRAMMING I Objective 8.03 Apply Animation and Graphic Methods in a Windows Form (4%)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Processing Processing is a simple programming environment that was created to make it easier to develop visually oriented applications with an emphasis.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
© by Pearson Education, Inc. All Rights Reserved. continued …
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Painter Application Introducing Graphics with Windows Presentation Foundation.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Mouse event handling Mouse events( delegates EventHandler, event arguments EventArgs) MouseEnter: raised if the mouse cursor enters the area of the control.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Events. Events Single Event Handlers Click Event Mouse Events Key Board Events Create and handle controls in runtime Outline.
Mouse draw Using the mouse and a palette to make a picture.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Introducing Web Controls Outline 29.1 Analyzing the.
CSC 298 Windows Forms.
BIM211 – Visual Programming Objects, Collections, and Events 1.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Visual Basic .NET BASICS
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 27 - Phone Book Application Introducing Multimedia.
Lecture Set 13 Drawing Mouse and Keyboard Events Part B – Mouse and Keyboard Events.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 - Graphical User Interface Concepts: Part.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
Events with Data Arguments Data Values Travel with e.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
BIM211 – Visual Programming Interacting with Users Graphics 1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 3.1 Test-Driving the Welcome Application 3.2.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
CIS 3.5 Lecture 2.2 More programming with "Processing"
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Shipping Hub Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
Events with Data Arguments Data Values Travel with e.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
© 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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Paint Tutorial Created February 2006 Start Paint: Start>Programs>Accessories>Paint.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Computer Programming I
Visual Basic Properties, Methods and Events
Mouse Inputs in Processing
Extend Text Editor to Draw shapes
Presentation transcript:

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application Introducing the Graphics Object and Mouse Events Outline 21.1 Test-Driving the Painter Application 21.2 Constructing the Painter Application 21.3 Using a Graphics Object 21.4 Handling the MouseDown Event 21.5 Handling the MouseUp Event 21.6 Handling the MouseMove Event 21.7 Distinguishing Between Mouse Buttons 21.8 Wrap-Up

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Use mouse events to allow user interaction with an application. –Handle MouseDown, MouseUp, and MouseMove events. –Use the Graphics object to draw circles on the Form. –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 Opening the completed Painter application –Double click Painter.sln –Debug > Start

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application Figure 21.2 Drawing on the Painter application’s Form. Drawing lines composed of small, colored circles

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application Figure 21.3 Drawing a cat and a computer mouse on the Form.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application Figure 21.4 Erasing part of the drawing. Erasing by drawing circles that are the same color as the Form ’s background

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using a Graphics Object Creating a Graphics object –Use the Form ’s CreateGraphics method

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using a Graphics Object Declaring a Graphics object Figure 21.6 Declaring a Graphics object.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Figure 21.7 Creating a Graphics object.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Generating the MouseDown event handler –Events Button on Properties window –Double-click MouseDown property

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Figure 21.8 Creating a MouseDown event handler. MouseDown property

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Figure 21.9 MouseDown event handler generated for FrmPainter. MouseEventArgs argument

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Figure Adding code to the MouseDown event handler. Using the Graphics object to draw a colored circle

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Drawing on the Form whenever a mouse button is clicked –FillEllipse method Pass a Brush object, which is used to fill the shape with colors –Color of Brush is specified in call to constructor Pass x- and y- coordinates Pass height and width of ellipse

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Figure General ellipse. (x,y) WidthBounding rectangle Height

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Figure Selecting a color by using Intellisense. Type Color followed by a dot (. ) to access Intellisense

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Run the application –Debug > Start –Note that a blue-violet dot is drawn when the mouse button is pressed over the Form

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseDown Event Figure Running the application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseUp Event Adding a second diameter –Declare a second constant that stores the diameter of a smaller circle –A circle with this diameter will be drawn when the mouse button is released

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseUp Event Figure Declaring a constant for use in the MouseUp event handler. Using a constant to store a circle’s diameter

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseUp Event Generating the MouseUp event handler –Events Button on Properties window –Double-click MouseUp property

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseUp Event Figure MouseUp empty event handler. MouseUp event handler after commenting and formatting

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseUp Event Drawing a circle when the user releases a button –MouseUp circles have half the diameter of MouseDown circles

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseUp Event Figure MouseUp event handler code. Drawing a green circle with half the diameter of the blue-violet circle

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseUp Event Running the application – Debug > Start –BlueViolet circle is drawn when you press any mouse button and a Green circle is drawn when it is released

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseUp Event Figure Running the application. Pressing the mouse button and releasing it without moving the mouse pointer Pressing mouse button, then releasing after moving pointer Drawing a flower using only MouseUp and MouseDown event handlers

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Add a bool variable –Declare and initialize the bool variable m_blnShouldPaint –m_blnShouldPaint is true when any mouse button is held down

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Figure bool instance variable m_blnShouldPaint is declared and set to false. Declaring and setting an instance variable to control painting

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Altering the MouseDown event handler –Delete the code from the previous version of the application –Add line 100 of Fig to the MouseDown event handler to set m_blnShouldPaint to true when mouse button is pressed

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Figure Setting m_blnShouldPaint to true. Allow drawing when mouse button is pressed

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Altering the MouseUp event handler –Delete the code from the previous version of this application –Add line 108 of Fig to the MouseUp event handler to set m_blnShouldPaint to false

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Figure Setting m_blnShouldPaint to false. Disable drawing when mouse button is released

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Generating the MouseMove event handler –Events Button on Properties window –Double-click MouseUp property

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Figure MouseMove empty event handler. MouseMove event handler after commenting and formatting

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Adding code to the MouseMove handler –Add lines from Fig to the handler If m_blnShouldPaint is true, draw a BlueViolet circle on the Form If m_blnShouldPaint is false, nothing is drawn

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Handling the MouseMove Event Figure MouseMove event handler draws circles on the Form if a mouse button is held down. Drawing a circle when the mouse moves and a mouse button is pressed

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Distinguishing Between Mouse Buttons Add another bool variable –m_blnShouldErase determines whether the mouse should act like an eraser

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Distinguishing Between Mouse Buttons Figure bool instance variable m_blnShouldErase is declared and set to false. Declaring and setting an instance variable to control erasing

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Distinguishing Between Mouse Buttons Determining which mouse button was pressed –MouseButtons enumeration Defines constants for 3 mouse buttons: Right, Left, and M iddle –Change MouseDown event handler If left button is pressed m_blnShouldPaint becomes true If right button is pressed m_blnShouldErase becomes true

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Distinguishing Between Mouse Buttons Figure Determining which mouse button was pressed. Using the Button property to enable drawing Using the Button property to enable erasing

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Distinguishing Between Mouse Buttons Changing the MouseUp event handler –Both Boolean instance variable are set to false –No drawing or erasing allowed when mouse button is not pressed

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Distinguishing Between Mouse Buttons Changing the MouseMove event handler –m_blnShouldPaint is true Draw a BlueViolet circle –m_blnShouldErase is true Draw a circle with the same color as the background –Use the Form ’s BackColor property

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Distinguishing Between Mouse Buttons Figure Setting m_blnShouldErase to false when a mouse button is released. Disabling erasing when a mouse button is released

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Distinguishing Between Mouse Buttons Figure Changing the MouseMove event handler to allow erasing. Drawing circles if left mouse button is pressed while the mouse moves Erasing by drawing circles with the Form ’s background color

Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 48 Painter.cs (1 of 6)

Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 49 Painter.cs (2 of 6)

Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 50 Painter.cs (3 of 6)

Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 51 Painter.cs (4 of 6) The Button property specifies which button was pressed The MouseButtons enumeration specifies constants for the mouse button

Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 52 Painter.cs (5 of 6) Method FillEllipse used to draw a BlueViolet ellipse Create a Brush with the Form ’s background color

Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 53 Painter.cs (6 of 6)