Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphics and Multimedia 2 Lecture 8. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing a General Path.

Similar presentations


Presentation on theme: "Graphics and Multimedia 2 Lecture 8. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing a General Path."— Presentation transcript:

1 Graphics and Multimedia 2 Lecture 8

2 OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing a General Path

3 Font Control Font s o After a Font is created, its properties cannot be modified o Programmers must create a new Font object to be different Font constructors o Must require a font name as an argument o They usually require the font size as an argument o And usually require the font style which is a member of the FontStyle enumeration: Bold, Italic, Regular, Strikeout, Underline. Graphics method DrawString sets the current drawing font—the font in which the text displays—to its Font argument.

4 Font Control DrawString constructors: o takes a String to display, o the display Font, o a Brush o and the x- and y-coordinates of the location for the String ’s first character.

5 Font Control GraphicsUnit argument—an enumeration that allows users to specify the unit of measurement employed to describe the font size. Members of the GraphicsUnit enumeration include Point (1/72 inch), Display (1/75 inch), Document (1/300 inch), Millimeter, Inch and Pixel.

6 1 ' Fig. 16.9: UsingFonts.vb 2 ' Demonstrating various font settings. 3 4 Public Class FrmFonts 5 Inherits System.Windows.Forms.Form 6 9 ' demonstrate various font and style settings 10 Protected Overrides Sub OnPaint( ByVal paintEvent As PaintEventArgs) 12 13 Dim graphicsObject As Graphics = paintEvent.Graphics 14 Dim brush As SolidBrush = New SolidBrush(Color.DarkBlue) 15 16 ' arial, 12 pt bold 17 Dim style As FontStyle = FontStyle.Bold 18 Dim arial As Font = New Font( New FontFamily("Arial"), 12, style) 20 21 ' times new roman, 12 pt regular 22 style = FontStyle.Regular 23 Dim timesNewRoman As Font = New Font( "Times New Roman", 12, style) 25 26 ' courier new, 16 pt bold and italic 27 style = FontStyle.Bold Or FontStyle.Italic 28 Dim courierNew As Font = New Font("Courier New", 16, style) 30 Creates a DarkBlue SolidBrush object ( brush ), causing all strings drawn with that brush to appear in DarkBlue

7 31 ' tahoma, 18 pt strikeout 32 style = FontStyle.Strikeout 33 Dim tahoma As Font = New Font("Tahoma", 18, style) 34 35 graphicsObject.DrawString(arial.Name & " 12 point bold.", arial, brush, 10, 10) 37 38 graphicsObject.DrawString(timesNewRoman.Name & _ 39 " 12 point plain.", timesNewRoman, brush, 10, 30) 40 41 graphicsObject.DrawString(courierNew.Name & _ 42 " 16 point bold and italic.", courierNew, brush, 10, 54 ) 43 44 graphicsObject.DrawString(tahoma.Name & _ 45 " 18 point strikeout.", tahoma, brush, 10, 75) 46 End Sub ' OnPaint 47 48 End Class ' FrmFonts

8 Drawing Lines, Rectangles and Ovals Drawing shape outlines o Versions that take a Pen and four Integer s are used Drawing solid shapes o Versions that take a Brush and four Integer s Integer arguments o First two represent the coordinates of the upper- left corner of the shape or its enclosing area o Last two indicate the shape’s width and height

9 Drawing Lines, Rectangles and Ovals

10 Fig. 16.15Ellipse bounded by a rectangle. height width (x, y)

11 1 ' Fig. 16.14: LinesRectanglesOvals.vb 2 ' Demonstrating lines, rectangles, and ovals. 3 4 Public Class FrmDrawing 5 Inherits System.Windows.Forms.Form 6 7 ' Visual Studio.NET generated code 8 9 ' display ovals lines, and rectangles 10 Protected Overrides Sub OnPaint( _ 11 ByVal paintEvent As PaintEventArgs) 12 13 ' get graphics object 14 Dim g As Graphics = paintEvent.Graphics 15 Dim brush As SolidBrush = New SolidBrush(Color.Blue) 16 Dim pen As Pen = New Pen(Color.AliceBlue) 17 18 ' create filled rectangle 19 g.FillRectangle(brush, 90, 30, 150, 90) 20 21 ' draw lines to connect rectangles 22 g.DrawLine(pen, 90, 30, 110, 40) 23 g.DrawLine(pen, 90, 120, 110, 130) 24 g.DrawLine(pen, 240, 30, 260, 40) 25 g.DrawLine(pen, 240, 120, 260, 130) 26 27 ' draw top rectangle 28 g.DrawRectangle(pen, 110, 40, 150, 90) 29 30 ' set brush to red 31 brush.Color = Color.Red 32 33 ' draw base Ellipse 34 g.FillEllipse(brush, 280, 75, 100, 50) 35

12 36 ' draw connecting lines 37 g.DrawLine(pen, 380, 55, 380, 100) 38 g.DrawLine(pen, 280, 55, 280, 100) 39 40 ' draw Ellipse outline 41 g.DrawEllipse(pen, 280, 30, 100, 50) 42 End Sub ' OnPaint 43 44 End Class ' FrmDrawing

13 Drawing a General Path General path is a shape constructed from straight lines and complex curves. TranslateTransform is a graphics method to indicate that the origin of an object should be translated to the coordinates (X,Y). RotateTransform is a graphics method to move an object to the next position with a particular rotation angle in degree. How to use… Dim graphicsObject As Graphics = e.Graphics graphicsObject.TranslateTransform(X,Y) graphicsObject.RotateTransform(angle)

14 DrawStars.vb 1 ' Fig. 16.22: DrawStars.vb 2 ' Using paths to draw stars on a form. 3 4 Imports System.Drawing.Drawing2D 5 6 Public Class FrmDrawStars 7 Inherits System.Windows.Forms.Form 8 9 ' Visual Studio.NET generated code 10 11 ' create path and draw stars along it 12 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 13 Dim graphicsObject As Graphics = e.Graphics 14 Dim i As Integer 15 Dim random As Random = New Random() 16 Dim brush As SolidBrush = _ 17 New SolidBrush(Color.DarkMagenta) 18 19 ' x and y points of path 20 Dim xPoints As Integer() = _ 21 {55, 67, 109, 73, 83, 55, 27, 37, 1, 43} 22 Dim yPoints As Integer() = _ 23 {0, 36, 36, 54, 96, 72, 96, 54, 36, 36} 24 25 ' create graphics path for star 26 Dim star As GraphicsPath = New GraphicsPath() 27 28 ' translate origin to (150, 150) 29 graphicsObject.TranslateTransform(150, 150) 30 31 ' create star from series of points 32 For i = 0 To 8 Step 2 33 star.AddLine(xPoints(i), yPoints(i), _ 34 xPoints(i + 1), yPoints(i + 1)) 35 Next Defines two Integer arrays, representing the x- and y- coordinates of the points in the star

15 36 37 ' close shape 38 star.CloseFigure() 39 40 ' rotate origin and draw stars in random colors 41 For i = 1 To 18 42 graphicsObject.RotateTransform(20) 43 44 brush.Color = Color.FromArgb(random.Next(200, 255), _ 45 random.Next(255), random.Next(255), random.Next(255)) 46 47 graphicsObject.FillPath(brush, star) 48 Next 49 50 End Sub ' OnPaint 51 52 End Class ' FrmDrawStars Uses GraphicsPath method CloseFigure to complete the shape Draws the star 18 times, rotating it around the origin Uses Graphics method RotateTransform to move to the next position on the form Graphics method FillPath draws a filled version of the star with the Brush created earlier


Download ppt "Graphics and Multimedia 2 Lecture 8. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing a General Path."

Similar presentations


Ads by Google