Chapter 16 Drawing in a Window

Slides:



Advertisements
Similar presentations
 putpixel function plots a pixel at location (x, y) of specified color.  Declaration :- void putpixel(int x, int y, int color);
Advertisements

Week 8 - Monday.  What did we talk about last time?  StdAudio.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Chapter 2: Graphics Programming
Processing Lecture. 1 What is processing?
Chapter 9 Color, Sound and Graphics
Region of Interest Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
C++ Graphics Primitives April 15th. void clearscreen(int c) –clear the screen to background color c –If c = 1 screen black.
Graphics Images – PictureBox control Drawing graphics - Graphics object Multimedia controls PictureBox control Image property – select image Choose how.
COMPUTER GRAPHICS Prepared by S.MAHALAKSHMI Asst. Prof(Sr) / SCSE VIT University.
1 Chapter 15 Drawing in a Window. 2 The Window Client Area  A coordinate system that is local to the window.  It always uses the upper-left corner of.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
Graphics and Multimedia Part #2
Visual C++ Lecture 11 Friday, 29 Aug Windows Graphic User Interface l Event driven programming environment l Windows graphic libraries (X11 on Unix,
Graphics A graphics program allows you to combine pictures and text in many different ways. Features – General Level Draw graphics Enter Text Common Tools.
© 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.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
Chapter2: Drawing a Window. Drawing with the GDI.
GDI+ 1. Objectives 2 GDI+ class  Create and render Graphic  Display information on the computer screen, printer 3.
1 Windows GDI Programming CIS 487/587 Bruce R. Maxim UM-Dearborn.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
Tkinter Canvas.
Presentation Outline Introduction Painting and Repainting GDI.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Bitmap (Chapter 15).
Attributes of drawing elements
Chapter2: Drawing a Window. Review: Introducing MFC.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Chapter2: Drawing a Window
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
GDI +. Graphics class's methods System.Drawing Graphics Objects.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Chapter2: Drawing a Window. Drawing with the GDI.
Drawing in Windows. To help with drawing on the Windows operating system, Microsoft created the Graphical Device Interface, abbreviated as GDI. – It is.
Introduction To GDI GDI Definition : It is a interface present in windows which provide function and related structures that an application can use to.
Windows Programming Lecture 14. WM_PAINT message Whenever an application receives the WM_PAINT message, whether the entire window needs repainting? WM_PAINT.
12 Graphics and Java 2D™.
Chapter 16 (cont.) Drawing in a Window Using the Mouse
Programming windows with MFC Chapter 2 - Drawing in a Window
Presentation Outline Introduction Painting and Repainting GDI.
Chapter 17 Creating the Document and Improving the View
Inserting and Working with Images
Building Java Programs
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Building Java Programs
Building Java Programs
Computer Programming I
Week 8 - Monday CS 121.
Graphics and Multimedia
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
Chapter 5 Working with Images
Basic Graphics Drawing Shapes 1.
The Canvas.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
The Microsoft Office suite provides themes to ensure consistent formatting both within and across documents and thus helps you create documents with a.
Graphics -- Introduction
Chapter Lessons Use the Macromedia Flash drawing tools
Graphic Device Interface
Windows Programming Lecture 13
Chapter 2 Graphics Programming with C++ and the Dark GDK Library
Working with Symbols and Interactivity
Drawing Graphics in JavaScript
Chapter 14 Drawing in a Window
Chapter 17 Creating the Document and Improving the View
Presentation transcript:

Chapter 16 Drawing in a Window

The Window Client Area A coordinate system that is local to the window. It always uses the upper-left corner of the client area as its reference point. Figure 16-1

Graphical Device Interface (GDI) You don’t draw pictures directly to the screen. You must define the graphical output (lines, circles, text) using the Graphical Device Interface. The GDI enables you to program graphical output independently of the hardware Such as the display screen, printers, plotters

What Is a Device Context? You must use a device context to draw anything on a graphical output device. In a word, a device context is a data structure defined by Windows. A device context contains attributes such as Drawing color Background color Line thickness Font Mapping mode Your output requests are specified by device-independent GDI function calls. A device context contains information that allows Windows to translate those requests into actions on the particular physical output device.

Mapping Modes (1) MM_TEXT A logical unit is one device pixel with positive x from left to right, and positive y from top to bottom of the window client area. (0,0) Figure 16-1

Mapping Modes (2) MM_LOENGLISH (P.947) A logical unit is 0.01 inches with positive x from left to right, and positive y from the top of the client area upwards. Consistent with what we learned in high school. By default, the point at the upper-left corner has the coordinates (0,0) in every mapping mode. Coordinate are always 32-bit signed integers. Figure 16-2

The View Class in Your Application In the class CSketcherView, the function OnDraw() is called when a WM_PAINT message is received in your program. Windows sends this message to your program whenever it requires the client area to be redrawn. The user resizes the window Part of your window was previously “covered” by another window

The OnDraw() Member Function void CSketcherView::OnDraw(CDC* pDC) { CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: add draw code for native data here } Returns the address of the document object related to the current view (P.878) Make sure the pointer pDoc contains a valid address. (P.768) Make sure the pointer pDoc is not null.

Assertion Failed

The CDC Class You should do all the drawing in your program using members of the CDC class. C – Class DC – Device Context There are over a hundred member functions of this class. Sometimes you use objects of CClientDC It is derived from CDC, and thus contains all the members we will discuss. Its advantage is that CClientDC always contains a device context that represents only the client area of a window.

Current Position In a device context, you draw entities such as lines, and text relative to a current position. You may set the current position by calling the MoveTo() function.

MoveTo() The CDC class overloads the MoveTo() function in two versions to provide flexibility. CPoint MoveTo(int x, int y); CPoint MoveTo(POINT aPoint); POINT is a structure defined as: typedef struct tagPOINT { LONG x; LONG y; } POINT; CPoint is a class with data members x and y of type LONG. The return value from the MoveTo() function is a CPoint object that specifies the position before the move. This allows you to move back easily.

Drawing Lines Figure 16-3

LineTo() The CDC class also defines two versions of the LineTo() function BOOL LineTo(int x, int y); BOOL LineTo(POINT aPoint); You may use either a POINT struct or a CPoint object as the argument.

Ex16_1 (P.952) When the LineTo() function is executed, the current position is changed to the point specifying the end of the line. void CSketcherView::OnDraw(CDC* pDC) { CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; pDC->MoveTo(50,50); pDC->LineTo(50,200); pDC->LineTo(150,200); pDC->LineTo(150,50); pDC->LineTo(50,50); }

Figure 16-4 (P.952) (50,50) (150,50) (50,200) (150,200)

Drawing Rectangles & Circles void CSketcherView::OnDraw(CDC* pDC) { CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; pDC->Rectangle(50,50, 150, 150); pDC->Ellipse(50,50, 150,150); pDC->Ellipse(200,50, 400,150); }

A circle is a special ellipse (50,50) (200,50) (400,150) (150,150)

Exercise: Lines and Rectangles Create an MFC application. Modify the OnDraw() member function of your View class, to draw a figure like this. The coordinates are for your reference. You don’t need to show them. (150,150) (200,200)

Exercise: Circles Use a for-loop in OnDraw() to draw a figure like this. Note that a rectangle or an ellipse has a solid background color (default to be white). Therefore, if you plot the smaller circles first, they will be covered by larger ones. (200,200)

Exercise: Square Wave Write a program to draw the square wave below. Observe the pattern. You can see it is a repetition of 8 periods, so you can use a for-loop to easy repeat the same pattern.

Exercise: Sine Wave Write a program to draw the sine wave from 0 degree to 720 degree. Recall that you learned in Calculus class that, you can approximate a smooth curve by a series of line segments. x=90o x=0o x=360o x=270o

Arc Another way to draw circles is to use the Arc() function. BOOL Arc(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); (x1, y1) and (x2, y2) define the upper-left and lower-right corners of a rectangle enclosing the circle (ellipse). The points (x3, y3) and (x4, y4) define the start and end points of the arc, which is drawn counterclockwise. If (x4, y4) is identical to (x3, y3), you get a circle. BOOL Arc(LPCRECT lpRect, POINT Startpt, POINT Endpt); lpRect points to an object of the class CRect, which has four public data members: left , top, right, bottom.

Drawing with the Arc() Function void CSketcherView::OnDraw(CDC* pDC) { CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; pDC->Arc(50,50,150,150,100,75,150,100); CRect* pRect = new CRect(250,50,300,100); CPoint Start(275,100); CPoint End(250,75); pDC->Arc(pRect, Start, End); delete pRect; } (100,75) (50,50) (150,100) (150,150) (250,75) (275,100)

Figure 16-5 (P.954)

Drawing in Color

Using a Pen Declare a pen object and initialize it as a red solid pen drawing a line 2 pixels wide (P.955) CPen aPen; aPen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); CPen* pOldPen = pDC->SelectObject(&aPen); pDC->Arc(50,50,150,150,100,75,150,100); pDC->SelectObject(pOldPen); CRect* pRect = new CRect(250,50,300,100); CPoint Start(275,100); CPoint End(250,75); pDC->Arc(pRect, Start, End); delete pRect;

Pen Style BOOL CreatePen(int aPenStyle, int aWidth, COLORREF aColor); PS_SOLID – solid line PS_DASH – dashed line PS_DOT – dotted line PS_DASHDOT – alternating dashes and dots PS_DASHDOTDOT – alternating dashes and double dots. PS_NULL – draw nothing

Creating a Brush A brush is actually an 8x8 block of patterns that’s repeated over the region to be filled. All closed shapes in CDC will be filled with a brush (and a color). Select the brush into the device context by calling the SelectObject() member (similar to selecting a pen). CBrush aBrush(RGB(0,255,255)); CBrush* pOldBrush = pDC->SelectObject(&aBrush); const int width = 50; const int height = 50; int i; for (i=0; i<6; i++) pDC->Rectangle(i*2*width, 50,i*2*width+50, 150); pDC->SelectObject(pOldBrush);

Solid Brush

DeleteObject() (P.956) CBrush aBrush; for (int i=0; i<25; i++) { aBrush.CreateSolidBrush(RGB(0,i*10,i*10)); CBrush* pOldBrush = pDC->SelectObject(&aBrush); pDC->Rectangle(i*20, 10, i*20+10, 100); aBrush.DeleteObject(); }

Hatching Style HS_HORIZONTAL HS_VERTICAL HS_FDIAGONAL HS_BDIAGONAL HS_CROSS HS_DIAGCROSS CBrush aBrush; aBrush.CreateHatchBrush(HS_DIAGCROSS, RGB(0,255,255)); CBrush* pOldBrush = static_cast<CBrush*> (pDC->SelectObject(&aBrush));

SketcherView.cpp void CSketcherView::OnDraw(CDC* pDC) { CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; CBrush aBrush(HS_DIAGCROSS, RGB(0,255,255)); CBrush* pOldBrush = pDC->SelectObject(&aBrush); const int width = 50; const int height = 50; int i; for (i=0; i<6; i+=2) pDC->Rectangle(i*2*width, 50,i*2*width+50, 150); pDC->SelectObject(pOldBrush); for (i=1; i<6; i+=2) }

A Hatched Brush

The BrushHatch enumeration typedef enum { HS_HORIZONTAL = 0x00000000, HS_VERTICAL = 0x00000001, HS_FDIAGONAL = 0x00000002, HS_BDIAGONAL = 0x00000003, HS_CROSS = 0x00000004, HS_DIAGCROSS = 0x00000005 } BrushHatch; CBrush aBrush; for (int i=0; i<6; i++) { aBrush.CreateHatchBrush(i, RGB(0,0,0)); CBrush* pOldBrush = pDC->SelectObject(&aBrush); pDC->Rectangle(i*100+50, 50, i*100+100, 150); aBrush.DeleteObject(); }

Summary The client coordinate system Drawing in the client area Device contexts Mapping modes Drawing in a window Line, Rectangle, Ellipse, Arc Pen Brush

Homework: Pentagon Draw a pentagon like this. You may need to include <cmath> if you want to all the sin/cos functions. (cos2,sin2) (cos,sin) (cos4,sin4) (cos3,sin3)

Homework Use LineTo() and Ellipse() to draw the following figure. Hint: You may need to include <cmath> to utilize the sin() and cos() function.

Homework Use CreateSolidBrush() to write a program generating the output as shown in this figure. Demonstrate to TA before 17:00. void CSketcherView::OnDraw(CDC* pDC) { CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; CBrush aBrush; for (int i=0; i<16; i++) for (int j=0; j<16; j++) switch (pDoc->GetColor()) case RED: aBrush.CreateSolidBrush(RGB(255, i*16, j*16)); break; case GREEN: aBrush.CreateSolidBrush(RGB(i*16, 255, j*16)); case BLUE: aBrush.CreateSolidBrush(RGB(i*16, j*16, 255)); } CBrush* pOldBrush = pDC->SelectObject(&aBrush); pDC->Rectangle(i*16+2, j*16+2, (i+1)*16-2, (j+1)*16-2); aBrush.DeleteObject();

如何學好程式設計 整理筆記 學以致用