Lecture Set 13 Drawing Mouse and Keyboard Events Part A - Drawing.

Slides:



Advertisements
Similar presentations
Computer Graphics Prof. Muhammad Saeed Dept. of Computer Science & IT Federal Urdu University of Arts, Sciences and Technology.
Advertisements

Chapter 9 Color, Sound and Graphics
COMPUTER PROGRAMMING I Objective 8.03 Apply Animation and Graphic Methods in a Windows Form (4%)
Chapter 1: An Introduction to Visual Basic 2012
User Interface Programming in C#: Graphics
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.
Chapter 13 Graphics, Animation, Sound, and Drag-and-Drop Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Graphics and Multimedia. Outline Introduction Graphics Contexts and Graphics Objects Color Control.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Chapter 13: Advanced GUI and Graphics
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Graphics Images – PictureBox control Drawing graphics - Graphics object Multimedia controls PictureBox control Image property – select image Choose how.
1 Chapter 26 D&D – Graphics Outline 26.1 Introduction 26.3 Graphics Contexts and Graphics Objects 26.4 Color Control 26.5 Font Control 26.6 Drawing Lines,
Software Engineering1 The.NET Framework and the CLI 2D Graphics with GDI+ Visual Studio.NET 2008.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Lecture Set 3 Introduction to Visual Basic Concepts Part A – User Interfaces and Windows Forms – The Toolbox.
Chapter 3 Working with Symbols and Interactivity.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface A Brief Introduction to GDI+ S.R.G. Fraser, Pro Visual C++/CLI.
CST238 Week 5 Questions / Concerns? Announcements – HW#1 due (Project ideas) – Check-off Take Home lab#4 Recap New topics – Drawing Coming up: – GUI Bloopers.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
1 Windows Graphics. 2 Objectives You will be able to Use the Windows GDI+ to draw arbitrary figures and text on a Windows form. Add a handler for the.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition.
C# Programming Lecture 4 “GDI+” PGL01/CSP/2006.
Graphics and Multimedia Part #2
BIM211 – Visual Programming Objects, Collections, and Events 1.
© 2011 Delmar, Cengage Learning Chapter 3 Working with Symbols and Interactivity.
Working with Symbols and Interactivity
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
Tutorial 1 Introducing Adobe Flash CS3 Professional
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
GDI+ 1. Objectives 2 GDI+ class  Create and render Graphic  Display information on the computer screen, printer 3.
BIM211 – Visual Programming Interacting with Users Graphics 1.
Lecture 5 Graphics Erick Pranata. » Graphics Overview » About GDI+ » Getting Started.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Presentation Outline Introduction Painting and Repainting GDI.
Lecture 7: Intro to Computer Graphics. Remember…… DIGITAL - Digital means discrete. DIGITAL - Digital means discrete. Digital representation is comprised.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Ray Konopka Creating Custom Microsoft.NET Framework Controls in Delphi.
Ray Konopka Developing Custom.NET Components DevCon Course No: 3118.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path.
Images Part 11 dbg. 2 Images The form and several controls can display a number of different types of image files in the BackgroundImage Property. These.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
Windows Programming C# Software Development. Overview  DLLs / PInvoke  DirectX .NET Component Library  Custom Controls  Event Model (in C++)  GUI.
PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 1: An Introduction to Visual Basic 2015
IS 350 Course Introduction
Computer Programming I
Graphics and Multimedia
Drawing Mouse and Keyboard Events Part A - Drawing
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
6th Lecture – Rectangles and Regions, and intro to Bitmap Images
ThS. Nguyễn Hà Giang Khoa CNTT - Hutech
CASE Tools Graphical User Interface Programming Using C#
Working with Symbols and Interactivity
Repetition and Multiple Forms
Web Development Using ASP .NET
Chapter 12 Graphics in Windows and the Web
CPT 450 Computer Graphics 3rd Lecture.
Presentation transcript:

Lecture Set 13 Drawing Mouse and Keyboard Events Part A - Drawing

Slide 2 Objectives Describe the purpose of the graphics device interface (GDI) Describe how to draw When a paint even fires By creating the graphics event associated with a control Create custom colors and use the ColorDialog control Use named pens and brushes and create custom pens and brushes Create shapes and paint them on the screen

Slide 3 Introduction to Drawing Windows drawing services are collectively referred to as the graphics device interface (GDI) The GDI (now GDI+) has classes to define shapes (lines, rectangles, ellipses, arcs, pies) and to draw them You can also draw text and 2-D images Shapes are drawn to a graphics surface (screen or printer) Without the GDI, shapes would have to be drawn manually pixel-by-pixel

Slide 4 The GDI+ Makes its home in the System.Drawing namespace GDI+ is new with.NET GDI+ builds on old Windows drawing system GDI+ features include surfaces, drawing inks, drawing elements, & drawing transformations GDI+ generalizes bitmap and vector drawing on all surfaces – the screen (form and control surfaces), a printer, a JPEG file (to name a few) All such destinations are treated the same

Slide 5 More on the GDI+ GDI+ manages to generalize drawing surfaces through the System.Drawing.Graphics class An object of this class represents a drawing canvas with all the attributes you can think of Colors, brushes, pens Drawing elements such as rectangles, ellipses, lines, and other standard and custom shapes Fonts Transformations – rotate, resize, skew

Slide 6 Still More on the GDI+ Some controls take care of their own display features Others let you take over all or some of the drawing features GDI+ is huge and complex – we will see about 1% of its capabilities

Slide 7 Introduction to the Paint Event Shapes are rendered to a form during the Paint event Other controls, such as a PictureBox, support the Paint event too There is no design-time mechanism to draw shapes What does this mean? Shapes do not support events What does this mean? The end user has no interaction with a shape drawn on a graphics surface Shapes can only be drawn to a graphics surface such as a form or PictureBox or some other controls such as a ListBox which allow you some control over its appearance

Slide 8 Selecting a Canvas Most drawing in.NET is done within the context of a Graphics Object (on which you draw everything) There is always a surface behind the graphics object Any drawing to a graphics object is drawn to this surface as well The graphics object includes many methods that let you draw “things” on the graphics surface

Slide 9 Drawing to a Form This next example illustrates one way to draw on a form (using the paint event handler)  Remember that this handler fires whenever a form is drawn or resized The handler also fires when a minimized form is maximized or restored And, it fires when an obscured or partially obscured form in unobscured A paint event also fires when the Invalidate method of the underlying form or Picturebox control is called Some events, most notably the paint event for forms and controls provide direct access to a graphics object through its arguments.

Slide 10 Drawing to a Form (Example) The second argument to the paint handler has data type PaintEventArgs. It also has a property named Graphics. The graphics object referenced by the Graphics property provides the link to the GDI.

Slide 11 Drawing to a PictureBox All controls, such as Label s, Button s, Form s and Picturebox es have their own graphics contexts. To draw on a control, you first obtain its graphics object by invoking the control’s method CreateGraphics  Then you can use the methods in the graphics class to draw on that object In the example on the next page, we pass the Picturebox control to the subroutine Then we create the graphics object for the Picturebox surface and store the reference to this object in curgraph Now we are ready to “draw on” the graphics object This object provides the link to the GDI+

Slide 12 Drawing to a PictureBox (Example)

Slide 13 The Classes of the GDI+ The System.Drawing namespace contains classes and structures that define shapes The System.Drawing.Drawing2D namespace contains additional shapes not supported by older versions of Windows Windows 98 and Windows ME, for example The System.Drawing.Graphics class contains methods to draw graphical shapes This is where we will focus our energies

Slide 14 The Purpose of the System.Drawing Namespace It contains structures that define graphical shapes It contains other structures and classes that describe visual characteristics of graphical shapes ( System.Drawing.Color, System.Drawing.Pen ) It contains the Graphics class, which is used to draw shapes to a graphics surface So – we need this namespace to do our work Why? Well, let’s review for a moment a similar explanation related to files (but adapted to fit the need to do graphics)

Slide 15 An Underlying Framework Already visited this idea for files (Chapter 10 and databases (Chapter 11). Now we revisit it a third time for doing graphics Remember … In your programs, you manipulate objects using methods belonging to the class that defines the type of the object The class (an abstract data type) is an abstraction (model) of the object being manipulated

Slide 16 The Object Framework 1 Recall, too … In all these cases, we must … Design and implement a class to model the entity to be manipulated The class is said to define an “abstract data type” We can then create objects (instances of the class we just created) Next comes the tricky part … 

Slide 17 The Object Framework 2 We now have to connect that object to the actual entity to be drawn on For graphics the object is an instance of a class that provides an abstract view of a graphics entity such as a drawing. This view is modeled by the Graphics class So we program against elements of the graphics class (and some other related classes) without concern for the underlying structures For example, whether we are drawing on a splash screen, a form, a picbox, or some other tool makes no difference – we simply program against the graphics object we instantiate and let the underlying class to the work

Slide 18 The Object Framework 3 – The Connection The actual, physical surface to be drawn on (splash screen, form, picbox, etc) The graphics abstraction (methods and data stores) of a physical surface that you program against (pens, colors, rectangles, ellipses, lines, fonts, etc.) The connection – via creation of a graphics object Public Sub DrawPicChanges(ByVal p As PictureBox) Dim curRec As Rectangle Dim curCol As Color... 'A picture box is not a graphics object. Create graphics object. 'Then drawing can be done curGraph = p.CreateGraphics...

Slide 19 The Object Framework 4 – Why Bother Why do this – what is this all about? The abstraction enables us to manipulate graphics objects in a uniform, consistent way, regardless of the underlying structure of the file or the operating system that manages the file The underlying structure actually being drawn on is hidden under several layers of software

Slide 20 The Object Framework 5 – Why Bother All we need to do is connect the graphics object to the underlying surface being drawn on (a form, splash screen, picbox …) Example: From Hangman Dim curGraph As Graphics... Public Sub New(ByVal p As PictureBox)... ‘Connects a graphics object to a picbox curGraph = p.CreateGraphics Programmer layer – draw “on” a graphics object ( curgraph ) ↓ Logical layer – manages the drawing on the surface connected to the graphics object (the picture box) ↓ Physical layer – manages the actual (physical) drawing of pixels to a physical entity (the physical representation of the picture box)

Slide 21 The Object Framework 6 (another example) Similarly, for the Hangman splash screen Private Sub frmSplash_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles MyBase.Paint Dim fntSplash1 As New Font("Arial", 14, FontStyle.Bold) … Dim recCurrent As Rectangle Dim colCurrent As Color Dim penCurrent As Pen ‘Create graphics object connected to splash screen form Dim graCurrent As Graphics = e.Graphics ‘Draw to graphics object graCurrent and hence to screen graCurrent.DrawString("Welcome to Hangman Game", _ fntSplash1, Brushes.OrangeRed, 5, 5) …

Slide 22 The Object Framework 7 – Why Bother The central issue is this: Your programming task is made easier because … You program against a (generalized) abstraction of a real “thing” and not against the thing itself. You do not need to worry about the details of the underlying surface being drawn on but just the methods and properties provided to you by the abstraction (the given graphics class)

Slide 23 Drawing a First Shape Drawing operations should be performed in the Paint event The data type of the second argument is PaintEventArgs Remember: The Graphics property stores a reference to an instance of the Graphics class with which drawing is performed The DrawLine method is used to draw a line using a Pen

Slide 24 Drawing a First Shape (Example) Draw a red line using a Pen Private Sub frmMain_Paint( _ ByVal sender As Object, _ ByVal e As _ System.Windows.Forms.PaintEventArgs) _ Handles Me.Paint e.Graphics.DrawLine(Pens.Red, 10, _ 10, 150, 150) End Sub

Slide 25 Figure 12-1: Drawing a Line

Slide 26 Understanding the Paint Event Several actions cause the Paint event to fire The Paint event fires when the form is drawn The Paint event fires when a form is resized The Paint event fires when a minimized form is restored or maximized The Paint event fires when an obscured form is no longer obscured The Paint event fires when the Invalidate method is called

Slide 27 Understanding Color Computer monitors display colors by combining varying intensities of red, green, and blue The term RGB is short for red, green, blue RGB values range between 0 and indicates the color is off 255 indicates the color is applied at its full intensity

Slide 28 Table 12-1: Sample RGB Colors

Slide 29 Introduction to the c olor Structure Visual Studio supports several named colors using the Color structure Examples to set the background and foreground colors of a Label using named colors: lblDemo.BackColor = Color.Gray lblDemo.ForeColor = Color.DarkBlue

Slide 30 The Color Structure (Members) The R property gets the red component of a color The G property gets the green component of a color The B property gets the blue component of a color The FromArgb method creates a color from RGB values The ToArgb method returns a 32-bit Integer representing a color

Slide 31 The Color Structure (Example) Display RGB values for a named color Dim CurrentColor As Color = _ System.Drawing.Color.Azure Console.WriteLine(CurrentColor.R.ToString) Console.WriteLine(CurrentColor.G.ToString) Console.WriteLine(CurrentColor.B.ToString)

Slide 32 The FromArgb Method The method arguments contain RGB values between 0 and 255 The method returns a Color Example: Dim CustomColor As Color = _ System.Drawing.Color.FromArgb(18, 127, 222) lblDemo.BackColor = CustomColor

Slide 33 The ColorDialog Control The ColorDialog control works like other dialog controls The ShowDialog method displays the dialog box The selected color is stored in the Color property The control instance appears in the resizable tray below the Windows Forms Designer

Slide 34 Figure 12-2: Color Dialog Box

Slide 35 Introduction to Pens and Brushes Pens are used to draw lines and outline other shapes The Pens class contains named pens The Pen class is used to create custom pens Brushes are used to fill shapes The Brushes class contains named brushes The SolidBrush class is used to create custom brushes Call the Dispose method to explicitly release the resources held by custom pens and brushes

Slide 36 Using Pens to Outline Shapes A named pen can be used to draw a line as follows: e.Graphics.DrawLine( _ Pens.Red, 10, 10, 150, 150)

Slide 37 The Pen Constructor Use the Pen constructor to create a custom Pen The first argument contains a color The second argument contains the width of the Pen The width is measured in pixels Examples: Dim CurrentColor As Color = _ Color.FromArgb(122, 89, 94) Dim CurrentPen2Pixel As New _ Pen(CurrentColor, 2) e.Graphics.DrawLine( _ CurrentPen2Pixel, 10, 10, 150, 150)

Slide 38 Using Brushes to Fill Shapes A Brush is used to fill the region of a shape such as a rectangle Brushes inherit from the System.Drawing.Brush class Visual Studio supports different types of brushes A SolidBrush has a single color A HatchBrush has a background color and a pattern

Slide 39 Figure 12-4: Hierarchical Organization of the Brush Classes

Slide 40 The HatchBrush Constructor (Syntax) Public Sub New(hatchStyle As HatchStyle, foreColor As Color) Public Sub New(hatchStyle As HatchStyle, foreColor As Color, backColor As Color) The hatchStyle argument defines the pattern There are roughly 70 different hatch styles The foreColor argument contains the foreground color The backColor argument contains the background color

Slide 41 The HatchBrush Constructor (Example) Create a cross hatch pattern Dim CurrentBrush As New _ System.Drawing.Drawing2D.HatchBrush( _ System.Drawing.Drawing2D.HatchStyle.Cross, _ Color.Aqua, Color.DarkGreen)

Slide 42 Shapes Defined by the System.Drawing Namespace The Point structure defines the x and y coordinates of a point The Size structure defines the width and height of another graphical shape such as a rectangle The Rectangle structure defines the origin and size of a rectangle The Rectangle structure is also used as the basis for other shapes such as ellipses

Slide 43 Figure 12-5: Filling and Outlining a Rectangle

Slide 44 The Point Structure The Point constructor accepts x and y coordinates as its arguments The origin of the graphics surface is 0, 0 The unit of measure is pixels Example: Dim CurrentPoint As New Point(10, 10)

Slide 45 The Size Structure The Size structure accepts width and height values as arguments to the constructor Values are measured in pixels Example: Dim CurrentSize As New Size(150, 150)

Slide 46 The Client Area Graphical shapes are drawn to a form's client area A form's client area excludes the form's border and title bar The form's ClientSize property stores the size of the client area Example: btnFill.Size = Me.ClientSize btnFill.Height = Me.ClientSize.Height btnFill.Width = Me.ClientSize.Width

Slide 47 Creating a Rectangle A Point structure defines a rectangle's origin A Size structure defines a rectangle's size The overloaded Rectangle constructor accepts either Point and Size structures or coordinate values

Slide 48 Creating a Rectangle (Examples) Create rectangles using Point and Size structures or using coordinate values Dim CurrentPoint As New Point(10, 10) Dim CurrentSize As New Size(150, 150) Dim FirstRectangle As New _ Rectangle(CurrentPoint, CurrentSize) Dim SecondRectangle As New Rectangle( _ 10, 10, 150, 150)

Slide 49 Drawing Rectangles The DrawRectangle method draws a rectangle outline with a pen The FillRectangle method fills a rectangle with a brush Example to draw a rectangle outline: Dim CurrentRectangle As New Rectangle( _ 10, 10, 150, 150) e.Graphics.DrawRectangle(Pens.Black, _ CurrentRectangle) e.Graphics.DrawRectangle(Pens.Black, _ 10, 10, 150, 150)

Slide 50 Drawing Lines The DrawLine method draws a line using a pen Two points can be passed as arguments A pair of x and y coordinates can be passed as arguments Example: Dim StartPoint As New Point(10, 10) Dim EndPoint As New Point(150, 150) e.Graphics.DrawLine(Pens.Red, _ StartPoint, EndPoint) e.Graphics.DrawLine(Pens.Red, _ 10, 10, 150, 150)

Slide 51 Figure 12-6: Drawing a Line on a Form

Slide 52 Introduction to Elliptical Shapes The DrawEllipse method uses a bounding rectangle to draw an ellipse The FillEllipse method fills an ellipse The DrawArc method draws an arc There is no method to fill an arc The DrawPie and FillPie methods draw and fill a pie shape

Slide 53 Drawing and Filling an Ellipse (Example) Dim CurrentRectangle As New Rectangle( _ 10, 10, 150, 150) e.Graphics.DrawEllipse(Pens.Black, _ CurrentRectangle) e.Graphics.FillEllipse(Brushes.Red, _ CurrentRectangle)

Slide 54 Figure 12-8: Drawing and Filling an Ellipse

Slide 55 Drawing Arcs An arc is drawn along an ellipse One argument contains the starting angle Angles are measured in degrees A starting angle of 0 appears along the x axis Another argument contains the sweep angle Positive sweep angles are drawn in a clockwise direction Negative sweep angles are drawn in a counterclockwise direction

Slide 56 Drawing an Arc (Example) Draw a 90 degree arc Dim CurrentEllipse As New Rectangle( _ 10, 10, 150, 150) e.Graphics.DrawArc(Pens.Red, _ CurrentEllipse, 0, 90)

Slide 57 Figure 12-8: Drawing an Arc

Slide 58 Creating a Pie Shape The syntax is the same as drawing an arc The DrawPie method draws an outline of the pie and the FillPie method fills the pie Example: Dim CurrentCircle As New Rectangle( _ 10, 10, 150, 150) e.Graphics.FillPie(Brushes.Red, _ CurrentCircle, 0, 45) e.Graphics.DrawPie(Pens.Black, _ CurrentCircle, 0, 45)

Slide 59 Figure 12-10: Filling and Drawing a Pie

Slide 60 Drawing Images The FromFile method of the System.Drawing.Image class reads an image The DrawImage method draws an image An image can be drawn at a point The image is not resized An image can be drawn inside of a bounding rectangle The image is resized to fit in the rectangle

Slide 61 Drawing Images (Example) Dim CurrentImage As Image Dim PointOrigin As New Point(10, 10) Dim ImageRectangle As New Rectangle( _ 10, 10, 150, 150) CurrentImage = _ Image.FromFile("C:\Image.bmp") e.Graphics.DrawImage(CurrentImage, _ PointOrigin) e.Graphics.DrawImage(CurrentImage, _ ImageRectangle)

Slide 62 Figure 12-12: Rendering an Image at a Point

Slide 63 Figure 12-13: Rendering an Image in a Rectangle