1 / 97

Chapter 16 – Graphics and Multimedia

Chapter 16 – Graphics and Multimedia. Outline 只教 16.3

kata
Download Presentation

Chapter 16 – Graphics and Multimedia

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 16 – Graphics and Multimedia Outline 只教 16.3 16.1 Introduction16.2   Graphics Contexts and Graphics Objects16.3   Color Control16.4   Font Control16.5   Drawing Lines, Rectangles and Ovals16.6   Drawing Arcs16.7   Drawing Polygons and Polylines16.8   Advanced Graphics Capabilities16.9   Introduction to Multimedia16.10   Loading, Displaying and Scaling Images16.11   Animating a Series of Images16.12   Windows Media Player16.13   Microsoft Agent

  2. 16.1 Introduction • GDI+ • extension of the Graphical Device Interface • System.Drawing and System.Drawing.Drawing2D • Class Graphics • Various methods for drawing on a Control • Requires a Pen (shape outlines) or Brush (solid objects) • Structure Color • Font and FontFamily classes

  3. System.Drawing System.Drawing Key Key class Class Bitmap Bitmap Structure Structure Font Font FontFamily FontFamily Graphics Graphics Color Color Icon Icon Point Point Pen Pen Rectangle Rectangle Region Region Size Size SolidBrush SolidBrush TextureBrush TextureBrush Image Image SolidBrush SolidBrush HatchBrush HatchBrush LinearGradient LinearGradient PathGradient PathGradient SolidBrush SolidBrush TextureBrush TextureBrush 16.1 Introduction Fig. 16.1 System.Drawing namespace’s Classes and Structures.

  4. 16.1 Introduction • GDI+’s coordinate system • Upper-left corner of component has coordinates (0, 0) • Coordinate pairs • Allow positioning of text and shapes • Horizontal coordinate (x-coordinate) • Distance to the right from upper-left corner • Vertical coordinate (y-coordinate) • Distance down from upper-left corner • Coordinate units measured in pixels • Used with structures Rectangle and Point

  5. (0, 0) +x x-axis +y (x, y) y-axis 16.1 Introduction Fig. 16.2 GDI+ coordinate system. Units are measured in pixels.

  6. 16.2   Graphics Contexts and Graphics Objects • Graphics context • Represents drawing surface • Managed using a Graphics object • OverridableOnPaint method • Inherited by every Form • Performs most graphics operations • Takes PaintEventArgs object as argument • Graphics property contains Graphics object • Triggers Control’s Paint event

  7. 16.2   Graphics Contexts and Graphics Objects • Paint event handler versus overriding OnPaint • Painting as an event-driven process • Calling method Invalidate instead of OnPaint • Passing Rectangle argument to Invalidate • Graphics contexts for Controls • Each Control has its own graphics context • Method CreateGraphics

  8. 16.3   Color Control • Structure Color • ARGB values • Alpha, red, green and blue values, respectively • Each value represented as a Byte • A, R, G, B properties • Alpha value determines intensity • 0 = transparent, 255 = opaque color • Alpha blending of RGB value and background color • FromArgb method • SharedColor constants • Color names representing RGB values (A = 255) • FromName method

  9. 16.3   Color Control

  10. 16.3   Color Control

  11. 16.3   Color Control • Pen objects • Used to draw lines • Constructors allow specification of color and drawing width • Pens collection (System.Drawing) contains predefined Pens • Brush objects • Used to color interiors of shapes • Abstract class • Various derived classes for fill styles • Upcoming example: Color value and alpha demonstration

  12. 16.3   Color Control

  13. 1 ' Fig. 16.6: ShowColors.vb 2 ' Using different colors in Visual Basic. 3 4 PublicClass FrmColorForm 5 Inherits System.Windows.Forms.Form 6 7 ' input text boxes 8 FriendWithEvents txtColorName As TextBox 9 FriendWithEvents txtGreenBox As TextBox 10 FriendWithEvents txtRedBox As TextBox 11 FriendWithEvents txtAlphaBox As TextBox 12 FriendWithEvents txtBlueBox As TextBox 13 14 ' set color command buttons 15 FriendWithEvents cmdColorName As Button 16 FriendWithEvents cmdColorValue As Button 17 18 ' color labels 19 FriendWithEvents lblBlue As Label 20 FriendWithEvents lblGreen As Label 21 FriendWithEvents lblRed As Label 22 FriendWithEvents lblAlpha As Label 23 24 ' group boxes 25 FriendWithEvents nameBox As GroupBox 26 FriendWithEvents colorValueGroup As GroupBox 27 28 ' Visual Studio .NET generated code 29 30 ' color for back rectangle 31 Private mBehindColor As Color = Color.Wheat 32 33 ' color for front rectangle 34 Private mFrontColor As Color = Color.FromArgb(100, 0, 0, 255) 35 ShowColors.vb

  14. Creates a black and white SolidBrush for drawing on the form Gets a reference to PaintEventArgse’s Graphics object and assigns it to Graphics object graphicsObject Graphics method FillRectangle draws a solid white rectangle with the Brush supplied as a parameter Assigns the ColormBehindColor value to the Brush’s Color property and displays a rectangle 36 ' overrides Form OnPaint method 37 ProtectedOverridesSub OnPaint(ByVal e As PaintEventArgs) 38 Dim graphicsObject As Graphics = e.Graphics ' get graphics 39 40Dim textBrush As SolidBrush = _ 41 New SolidBrush(Color.Black) ' create text brush 42 43 Dim brush As SolidBrush = _ 44 New SolidBrush(Color.White) ' create solid brush 45 46 ' draw white background 47 graphicsObject.FillRectangle(brush, 4, 4, 275, 180) 48 49 ' display name of behindColor 50 graphicsObject.DrawString(mBehindColor.Name, Me.Font, _ 51 textBrush, 40, 5) 52 53 ' set brush color and display back rectangle 54 brush.Color = mBehindColor 55 56 graphicsObject.FillRectangle(brush, 45, 20, 150, 120) 57 58 ' display Argb values of front color 59 graphicsObject.DrawString("Alpha: " & mFrontColor.A & _ 60 " Red: " & mFrontColor.R & " Green: " & mFrontColor.G _ 61 & " Blue: " & mFrontColor.B, Me.Font, textBrush, _ 62 55, 165) 63 64 ' set brush color and display front rectangle 65 brush.Color = mFrontColor 66 67 graphicsObject.FillRectangle(brush, 65, 35, 170, 130) 68 EndSub' OnPaint 69 ShowColors.vb

  15. 70 ' handle cmdColorValue click event 71 PrivateSub cmdColorValue_Click(ByVal sender As _ 72 System.Object, ByVal e As System.EventArgs) _ 73 Handles cmdColorValue.Click 74 75 ' obtain new front color from text boxes 76 mFrontColor = Color.FromArgb(txtAlphaBox.Text, _ 77 txtRedBox.Text, txtGreenBox.Text, txtBlueBox.Text) 78 79 Invalidate() ' refresh Form 80 EndSub' cmdColorValue_Click 81 82 PrivateSub cmdColorName_Click(ByVal sender As _ 83 System.Object, ByVal e As System.EventArgs) _ 84 Handles cmdColorName.Click 85 86 ' set behindColor to color specified in text box 87 mBehindColor = Color.FromName(txtColorName.Text) 88 89 Invalidate() ' refresh Form 90 EndSub' cmdColorName_Click 91 92 EndClass ' FrmColorForm ShowColors.vb

  16. ShowColors.vb

  17. Define Button cmdTextButton’s event handler Creates new ColorDialog named colorBox and invokes ShowDialog method to display the window Sets the text color of both buttons to the selected color 1 ' Fig. 16.7: ShowColorsComplex.vb 2 ' Change the background and text colors of a form. 3 4 Imports System.Windows.Forms 5 6 PublicClass FrmColorDialogTest 7 Inherits System.Windows.Forms.Form 8 9 FriendWithEvents cmdBackgroundButton As Button 10 FriendWithEvents cmdTextButton As Button 11 12 ' Visual Studio .NET generated code 13 14 ' change text color 15 PrivateSub cmdTextButton_Click (ByVal sender As System.Object, _ 16 ByVal e As System.EventArgs) Handles cmdTextButton.Click 17 18 ' create ColorDialog object 19 Dim colorBox As ColorDialog = New ColorDialog() 20 Dim result As DialogResult 21 22 ' get chosen color 23 result = colorBox.ShowDialog() 24 25 If result = DialogResult.CancelThen 26 Return 27 End If 28 29 ' assign forecolor to result of dialog 30 cmdBackgroundButton.ForeColor = colorBox.Color 31 cmdTextButton.ForeColor = colorBox.Color 32 End Sub ' cmdTextButton_Click 33 ShowColorsComplex.vb

  18. Defines the event handler for button cmdBackgroundButton Sets the dialog’s FullOpen property to True 34 ' change background color 35 PrivateSub cmdBackgroundButton_Click( _ 36 ByVal sender As System.Object, _ 37 ByVal e As System.EventArgs) _ 38 Handles cmdBackgroundButton.Click 39 40 ' create ColorDialog object 41 Dim colorBox As ColorDialog = New ColorDialog() 42 Dim result As DialogResult 43 44 ' show ColorDialog and get result 45 colorBox.FullOpen = True 46 result = colorBox.ShowDialog() 47 48 If result = DialogResult.CancelThen 49 Return 50 End If 51 52 ' set background color 53 Me.BackColor = colorBox.Color 54 End Sub' cmdBackgroundButton_Click 55 56 End Class ' FrmColorDialogTest ShowColorsComplex.vb

  19. ShowColorsComplex.vb

  20. 16.4 Font Control • Fonts • After a Font is created, its properties cannot be modified • Programmers must create a new Font object to be different • Font constructors • Must require a font name as an argument • They usually require the font size as an argument • And usually require the font style • Font metrics • Height • Descent (the amount that characters dip below the baseline) • Ascent (the amount that characters rise above the baseline) • Leading (difference between ascent of one line and descent of the previous line)

  21. 16.4 Font Control

  22. Creates a DarkBlueSolidBrush object (brush), causing all strings drawn with that brush to appear in DarkBlue Font constructor initializes Font objects 1 ' Fig. 16.9: UsingFonts.vb 2 ' Demonstrating various font settings. 3 4 Public Class FrmFonts 5 Inherits System.Windows.Forms.Form 6 7 ' Visual Studio .NET generated code 8 9 ' demonstrate various font and style settings 10 Protected Overrides Sub OnPaint( _ 11 ByVal paintEvent As PaintEventArgs) 12 13 Dim graphicsObject As Graphics = paintEvent.Graphics 14Dim brush As SolidBrush = New SolidBrush(Color.DarkBlue) 15 16 ' arial, 12 pt bold 17Dim style As FontStyle = FontStyle.Bold 18 Dim arial As Font = New Font( _ 19 New FontFamily("Arial"), 12, style) 20 21 ' times new roman, 12 pt regular 22 style = FontStyle.Regular 23 Dim timesNewRoman As Font = New Font( _ 24 "Times New Roman", 12, style) 25 26 ' courier new, 16 pt bold and italic 27 style = FontStyle.BoldOr FontStyle.Italic 28 Dim courierNew As Font = New Font("Courier New", _ 29 16, style) 30 31 ' tahoma, 18 pt strikeout 32 style = FontStyle.Strikeout 33 Dim tahoma As Font = New Font("Tahoma", 18, style) 34 UsingFonts.vb

  23. 35 graphicsObject.DrawString(arial.Name & " 12 point bold.", _ 36 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 EndSub' OnPaint 47 48 End Class ' FrmFonts UsingFonts.vb

  24. 16.4 Font Control Fig. 16.10 An illustration of font metrics.

  25. 16.4 Font Control

  26. Creates Font arial and sets it to 12 point Arial font Uses class Font property FontFamily to obtain objects arial’s FontFamily object 1 ' Fig. 16.12: UsingFontMetrics.vb 2 ' Displaying font metric information. 3 4 Imports System 5 Imports System.Drawing 6 Imports System.Drawing.Text 7 8 PublicClass FrmFontMetrics 9 Inherits System.Windows.Forms.Form 10 11 ' Visual Studio .NET generated code 12 13 Protected Overrides Sub OnPaint( _ 14 ByVal paintEvent As PaintEventArgs) 15 16 Dim graphicsObject As Graphics = paintEvent.Graphics 17 Dim brush As SolidBrush = New SolidBrush(Color.Red) 18 Dim pen As Pen = New Pen(brush, Convert.ToSingle(2.5)) 19 20 ' Arial font metrics 21 Dim arial As Font = New Font("Arial", 12) 22 Dim family As FontFamily = arial.FontFamily 23 Dim sanSerif As Font = New Font("Microsoft Sans Serif", _ 24 14, FontStyle.Italic) 25 26 pen.Color = brush.Color 27 brush.Color = Color.DarkBlue 28 29 ' display Arial font metrics 30 graphicsObject.DrawString("Current Font: " & arial.ToString, _ 31 arial, brush, 10, 10) 32 33 graphicsObject.DrawString("Ascent: " & _ 34 family.GetCellAscent(FontStyle.Regular), arial, brush, _ 35 10, 30) 36 UsingFontMetrics.vb

  27. Uses methods of class FontFamily to return integers specifying the ascent, descent, height and leading of the font 37 graphicsObject.DrawString("Descent: " & _ 38 family.GetCellDescent(FontStyle.Regular), arial, brush, _ 39 10, 50) 40 41 graphicsObject.DrawString("Height: " & _ 42 family.GetEmHeight(FontStyle.Regular), _ 43 arial, brush, 10, 70) 44 45 graphicsObject.DrawString("Leading: " & _ 46 family.GetLineSpacing(FontStyle.Regular), arial, brush, _ 47 10, 90) 48 49 ' display Sans Serif font metrics 50 family = sanSerif.FontFamily 51 52 graphicsObject.DrawString("Current Font: " & _ 53 sanSerif.ToString(), sanSerif, brush, 10, 130) 54 55 graphicsObject.DrawString("Ascent: " & _ 56 family.GetCellAscent(FontStyle.Italic), _ 57 sanSerif, brush, 10, 150) 58 59 graphicsObject.DrawString("Descent: " & _ 60 family.GetCellDescent(FontStyle.Italic), sanSerif, _ 61 brush, 10, 170) 62 63 graphicsObject.DrawString("Height: " & family.GetEmHeight _ 64 (FontStyle.Italic), sanSerif, brush, 10, 190) 65 66 graphicsObject.DrawString("Leading: " & _ 67 family.GetLineSpacing(FontStyle.Italic), sanSerif, _ 68 brush, 10, 210) 69 End Sub ' OnPaint 70 71 End Class ' FrmFontMetrics UsingFontMetrics.vb

  28. UsingFontMetrics.vb

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

  30. 16.5 Drawing Lines, Rectangles and Ovals

  31. Methods DrawRectangle and FillRectangle draw rectangles on the screen Method DrawLine takes a Pen and two pairs of Integers, specifying the start and endpoint of the line 1 ' Fig. 16.14: LinesRectanglesOvals.vb 2 ' Demonstrating lines, rectangles, and ovals. 3 4 PublicClass FrmDrawing 5 Inherits System.Windows.Forms.Form 6 7 ' Visual Studio .NET generated code 8 9 ' display ovals lines, and rectangles 10 ProtectedOverridesSub 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 LinesRectanglesOvals.vb

  32. 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 EndSub' OnPaint 43 44 EndClass ' FrmDrawing LinesRectanglesOvals.vb

  33. (x, y) height width 16.5 Drawing Lines, Rectangles and Ovals Fig. 16.15 Ellipse bounded by a rectangle.

  34. 16.6 Drawing Arcs • Arcs • Arcs are portions of ellipses • Measured in degrees, beginning at the starting angle and continuing for a specified number of degrees, the arc angle • An arc is said to sweep (traverse) its arc angle, beginning from its starting angle • Measuring • Sweep in a clockwise direction, measured in positive degrees • Sweep in a counterclockwise direction, measured in negative degrees • Graphics methods used to draw arcs • DrawArc, DrawPie, and FillPie

  35. 16.6 Drawing Arcs Fig. 16.16 Positive and negative arc angles.

  36. 16.6 Drawing Arcs

  37. Creates the objects needed to draw various arcs Draws a rectangle and then an arc inside the rectangle Changes the location of the Rectangle by setting its Location property to a new Point 1 ' Fig. 16.18: DrawArcs.vb 2 ' Drawing various arcs on a form. 3 4 PublicClass FrmArcTest 5 Inherits System.Windows.Forms.Form 6 7 ' Visual Studio .NET generated code 8 9 Protected Overrides Sub OnPaint( _ 10 ByVal paintEvent As PaintEventArgs) 11 12 ' get graphics object 13Dim graphicsObject As Graphics = paintEvent.Graphics 14 Dim rectangle1 As Rectangle = New Rectangle(15, 35, 80, 80) 15 Dim brush1 As SolidBrush = New SolidBrush(Color.FireBrick) 16 Dim pen1 As Pen = New Pen(brush1, 1) 17 Dim brush2 As SolidBrush = New SolidBrush(Color.DarkBlue) 18 Dim pen2 As Pen = New Pen(brush2, 1) 19 20 ' start at 0 and sweep 360 degrees 21 graphicsObject.DrawRectangle(pen1, rectangle1) 22 graphicsObject.DrawArc(pen2, rectangle1, 0, 360) 23 24 ' start at 0 and sweep 110 degrees 25 rectangle1.Location = New Point(100, 35) 26 graphicsObject.DrawRectangle(pen1, rectangle1) 27 graphicsObject.DrawArc(pen2, rectangle1, 0, 110) 28 29 ' start at 0 and sweep -270 degrees 30 rectangle1.Location = New Point(185, 35) 31 graphicsObject.DrawRectangle(pen1, rectangle1) 32 graphicsObject.DrawArc(pen2, rectangle1, 0, -270) 33 DrawArcs.vb

  38. Sets the Size property to a new Size object 34 ' start at 0 and sweep 360 degrees 35 rectangle1.Location = New Point(15, 120) 36 rectangle1.Size = New Size(80, 40) 37 graphicsObject.DrawRectangle(pen1, rectangle1) 38 graphicsObject.FillPie(brush2, rectangle1, 0, 360) 39 40 ' start at 270 and sweep -90 degrees 41 rectangle1.Location = New Point(100, 120) 42 graphicsObject.DrawRectangle(pen1, rectangle1) 43 graphicsObject.FillPie(brush2, rectangle1, 270, -90) 44 45 ' start at 0 and sweep -270 degrees 46 rectangle1.Location = New Point(185, 120) 47 graphicsObject.DrawRectangle(pen1, rectangle1) 48 graphicsObject.FillPie(brush2, rectangle1, 0, -270) 49 EndSub' OnPaint 50 51 End Class ' FrmArcTest DrawArcs.vb

  39. 16.7 Drawing Polygons and Polylines • Polygons • Multisided shapes • Graphics methods used to draw polygons • DrawLines, DrawPolygon, and FillPolygon

  40. 16.7 Drawing Polygons and Polylines

  41. Declaring ArrayListmPoints as a container for our Point objects allows the user to specify a variable number of points Declares the Pen and Brush used to color our shapes 1 ' Fig. 16.20: DrawPolygons.vb 2 ' Demonstrating polygons. 3 4 PublicClass FrmPolygon 5 Inherits System.Windows.Forms.Form 6 7 ' polygon type options 8 FriendWithEvents filledPolygonRadio As RadioButton 9 FriendWithEvents lineRadio As RadioButton 10 FriendWithEvents polygonRadio As RadioButton 11 12 ' command buttons 13 FriendWithEvents cmdClear As Button 14 FriendWithEvents cmdNewColor As Button 15 16 FriendWithEvents drawWindow As Panel 17 FriendWithEvents typeGroup As GroupBox 18 19 ' Visual Studio .NET generated code 20 21 ' contains list of polygon points 22Private mPoints As ArrayList = New ArrayList() 23 24 ' initialize default pen and brush 25Dim mPen As Pen = New Pen(Color.DarkBlue) 26 Dim mBrush As SolidBrush = New SolidBrush(Color.DarkBlue) 27 28 ' draw panel mouse down event handler 29 PrivateSub drawWindow_MouseDown(ByVal sender _ 30 AsObject, ByVal e As _ 31 System.Windows.Forms.MouseEventArgs) _ 32 Handles drawWindow.MouseDown 33 DrawPolygons.vb

  42. Extracts an Array from the ArrayList via method ToArray 34 ' Add mouse position to vertex list 35 mPoints.Add(New Point(e.X, e.Y)) 36 drawWindow.Invalidate() ' refresh panel 37 EndSub' drawWindow_MouseDown 38 39 ' draw panel paint event handler 40 PrivateSub drawWindow_Paint(ByVal sender AsObject, _ 41 ByVal e As System.Windows.Forms.PaintEventArgs) _ 42 Handles drawWindow.Paint 43 44 ' get graphics object for panel 45 Dim graphicsObject As Graphics = e.Graphics 46 47 ' if arraylist has 2 or more points, display shape 48 If mPoints.Count > 1Then 49 50 ' get array for use in drawing functions 51Dim pointArray() As Point = _ 52 mPoints.ToArray(mPoints(0).GetType()) 53 54 If polygonRadio.Checked Then' draw polygon 55 graphicsObject.DrawPolygon(mPen, pointArray) 56 57 ElseIf lineRadio.Checked Then' draw lines 58 graphicsObject.DrawLines(mPen, pointArray) 59 60 ElseIf filledPolygonRadio.Checked Then' draw filled 61 graphicsObject.FillPolygon(mBrush, pointArray) 62 EndIf 63 64 EndIf 65 66 EndSub' drawWindow_Paint 67 DrawPolygons.vb

  43. 68 ' handle cmdClear click event 69 PrivateSub cmdClear_Click(ByVal sender As System.Object, _ 70 ByVal e As System.EventArgs) Handles cmdClear.Click 71 72 mPoints = New ArrayList() ' remove points 73 74 drawWindow.Invalidate() ' refresh panel 75 EndSub' cmdClear_Click 76 77 ' handle polygon radio button CheckedChange event 78 PrivateSub polygonRadio_CheckedChanged(ByVal sender As _ 79 System.Object, ByVal e As System.EventArgs) _ 80 Handles polygonRadio.CheckedChanged 81 82 drawWindow.Invalidate() ' refresh panel 83 EndSub' polygonRadio_CheckedChanged 84 85 ' handle line radio button CheckChanged event 86 PrivateSub lineRadio_CheckedChanged(ByVal sender As _ 87 System.Object, ByVal e As System.EventArgs) _ 88 Handles lineRadio.CheckedChanged 89 90 drawWindow.Invalidate() ' refresh panel 91 EndSub' lineRadio_CheckedChanged 92 93 ' handle filled polygon radio button CheckChanged event 94 PrivateSub filledPolygonRadio_CheckedChanged(ByVal sender _ 95 As System.Object, ByVal e As System.EventArgs) _ 96 Handles filledPolygonRadio.CheckedChanged 97 98 drawWindow.Invalidate() ' refresh panel 99 EndSub' filledPolygonRadio_CheckedChanged 100 DrawPolygons.vb

  44. 101 ' handle cmdNewColor click event 102 PrivateSub cmdNewColor_Click(ByVal sender As _ 103 System.Object, ByVal e As System.EventArgs) _ 104 Handles cmdNewColor.Click 105 106 ' create new color dialog 107 Dim colorBox As ColorDialog = New ColorDialog() 108 109 ' show dialog and obtain result 110 Dim result As DialogResult = colorBox.ShowDialog() 111 112 ' return if user cancels 113 If result = DialogResult.CancelThen 114 Return 115 EndIf 116 117 mPen.Color = colorBox.Color ' set pen to new color 118 mBrush.Color = colorBox.Color ' set brush 119 drawWindow.Invalidate() ' refresh panel 120 EndSub' cmdNewColor_Click 121 122 EndClass' FrmPolygon DrawPolygons.vb

  45. DrawPolygons.vb

  46. 16.8 Advanced Graphics Capabilities • Visual Basic offers many additional graphics capabilities • Examples • Brush hierarchy also includes: • HatchBrush, LinearGradientBrush, PathGradientBrush and TextureBrush • Additional graphics features • Dashed lines, thick lines, filling shapes with patterns, etc • General path • A shape constructed from straight lines and complex curves

  47. Creates LinearGradientBrush object Brush Creates a Pen object pen, and passes arguments Color.Red and Integer10, indicating that we want pen to draw red lines that are 10 pixels wide Creates a new Bitmap image, which is initially empty 1 ' Fig. 16.21: DrawShapes.vb 2 ' Drawing various shapes on a form. 3 4 Imports System.Drawing.Drawing2D 5 6 PublicClass FrmDrawShapes 7 Inherits System.Windows.Forms.Form 8 9 ' Visual Studio .NET generated code 10 11 ' draw various shapes on form 12 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 13 14 ' references to object we will use 15 Dim graphicsObject As Graphics = e.Graphics 16 17 ' ellipse rectangle and gradient brush 18 Dim drawArea1 As Rectangle = New Rectangle(5, 35, 30, 100) 19Dim linearBrush As LinearGradientBrush = _ 20 New LinearGradientBrush(drawArea1, Color.Blue, _ 21 Color.Yellow, LinearGradientMode.ForwardDiagonal) 22 23 ' pen and location for red outline rectangle 24Dim thickRedPen As Pen = New Pen(Color.Red, 10) 25 Dim drawArea2 As Rectangle = New Rectangle(80, 30, 65, 100) 26 27 ' bitmap texture 28Dim textureBitmap As Bitmap = New Bitmap(10, 10) 29 Dim graphicsObject2 As Graphics = _ 30 Graphics.FromImage(textureBitmap) ' get bitmap graphics 31 32 ' brush and pen used throughout program 33 Dim solidColorBrush As SolidBrush = _ 34 New SolidBrush(Color.Red) 35 Dim coloredPen As Pen = New Pen(solidColorBrush) DrawShapes.vb

  48. Uses Graphics method FillEllipse to draw an ellipse with brush, the color gradually changes from blue to yellow 36 37 ' draw ellipse filled with a blue-yellow gradient 38 graphicsObject.FillEllipse(linearBrush, 5, 30, 65, 100) 39 40 ' draw thick rectangle outline in red 41 graphicsObject.DrawRectangle(thickRedPen, drawArea2) 42 43 ' fill textureBitmap with yellow 44 solidColorBrush.Color = Color.Yellow 45 graphicsObject2.FillRectangle(solidColorBrush, 0, 0, 10, 10) 46 47 ' draw small black rectangle in textureBitmap 48 coloredPen.Color = Color.Black 49 graphicsObject2.DrawRectangle(coloredPen, 1, 1, 6, 6) 50 51 ' draw small blue rectangle in textureBitmap 52 solidColorBrush.Color = Color.Blue 53 graphicsObject2.FillRectangle(solidColorBrush, 1, 1, 3, 3) 54 55 ' draw small red square in textureBitmap 56 solidColorBrush.Color = Color.Red 57 graphicsObject2.FillRectangle(solidColorBrush, 4, 4, 3, 3) 58 59 ' create textured brush and display textured rectangle 60 Dim texturedBrush As TextureBrush = _ 61 New TextureBrush(textureBitmap) 62 63 graphicsObject.FillRectangle( _ 64 texturedBrush, 155, 30, 75, 100) 65 66 ' draw pie-shaped arc in white 67 coloredPen.Color = Color.White 68 coloredPen.Width = 6 69 graphicsObject.DrawPie( _ 70 coloredPen, 240, 30, 75, 100, 0, 270) DrawShapes.vb

  49. 71 72 ' draw lines in green and yellow 73 coloredPen.Color = Color.Green 74 coloredPen.Width = 5 75 graphicsObject.DrawLine(coloredPen, 395, 30, 320, 150) 76 77 ' draw a rounded, dashed yellow line 78 coloredPen.Color = Color.Yellow 79 coloredPen.DashCap = LineCap.Round 80 coloredPen.DashStyle = DashStyle.Dash 81 graphicsObject.DrawLine(coloredPen, 320, 30, 395, 150) 82 EndSub' OnPaint 83 84 EndClass ' FrmDrawShapes DrawShapes.vb

  50. Defines two Integer arrays, representing the x- and y-coordinates of the points in the star Defines GraphicsPath object star Sets the origin of the Graphics object 1 ' Fig. 16.22: DrawStars.vb 2 ' Using paths to draw stars on a form. 3 4 Imports System.Drawing.Drawing2D 5 6 PublicClass 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 AsInteger 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 20Dim xPoints AsInteger() = _ 21 {55, 67, 109, 73, 83, 55, 27, 37, 1, 43} 22 Dim yPoints AsInteger() = _ 23 {0, 36, 36, 54, 96, 72, 96, 54, 36, 36} 24 25 ' create graphics path for star 26Dim 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 = 0To8 Step 2 33 star.AddLine(xPoints(i), yPoints(i), _ 34 xPoints(i + 1), yPoints(i + 1)) 35 Next DrawStars.vb

More Related