Graphics and Multimedia
Outline Drawing Polygons and Polylines Advanced Graphics Capabilities
Drawing Polygons and Polylines Polygons – Multisided shapes – Graphics methods used to draw polygons DrawLines, DrawPolygon, and FillPolygon
Drawing Polygons and Polylines
1 ' Fig : DrawPolygons.vb 2 ' Demonstrating polygons. 3 4 Public Class FrmPolygon 5 Inherits System.Windows.Forms.Form 6 21 ' contains list of polygon points 22 Private mPoints As ArrayList = New ArrayList() ' initialize default pen and brush 25 Dim mPen As Pen = New Pen(Color.DarkBlue) 26 Dim mBrush As SolidBrush = New SolidBrush(Color.DarkBlue) ' draw panel mouse down event handler 29 Private Sub drawWindow_MouseDown(ByVal sender _ 30 As Object, ByVal e As _ 31 System.Windows.Forms.MouseEventArgs) _ 32 Handles drawWindow.MouseDown ' Add mouse position to vertex list 35 mPoints.Add(New Point(e.X, e.Y)) 36 drawWindow.Invalidate() ' refresh panel 37 End Sub ' drawWindow_MouseDown ' draw panel paint event handler 40 Private Sub drawWindow_Paint(ByVal sender As Object, _ 41 ByVal e As System.Windows.Forms.PaintEventArgs) _ 42 Handles drawWindow.Paint ' get graphics object for panel 45 Dim graphicsObject As Graphics = e.Graphics Declaring ArrayList mPoints as a container for our Point objects allows the user to specify a variable number of points The MouseDown event handler for Panel drawWindow stores mouse-click locations in the mPoints ArrayList. Calls method Invalidate of drawWindow to ensure that the panel refreshes to accommodate the new point. if the ArrayList mPoints contains two or more Points, displays the polygon using the method that the user selected via the GUI radio buttons
46 47 ' if arraylist has 2 or more points, display shape 48 If mPoints.Count > 1 Then ' get array for use in drawing functions 51 Dim pointArray() As Point = _ 52 mPoints.ToArray(mPoints(0).GetType()) If polygonRadio.Checked Then ' draw polygon 55 graphicsObject.DrawPolygon(mPen, pointArray) ElseIf lineRadio.Checked Then ' draw lines 58 graphicsObject.DrawLines(mPen, pointArray) ElseIf filledPolygonRadio.Checked Then ' draw filled 61 graphicsObject.FillPolygon(mBrush, pointArray) 62 End If End If End Sub ' drawWindow_Paint ' handle cmdClear click event 69 Private Sub cmdClear_Click(ByVal sender As System.Object, _ 70 ByVal e As System.EventArgs) Handles cmdClear.Click mPoints = New ArrayList() ' remove points drawWindow.Invalidate() ' refresh panel 75 End Sub ' cmdClear_Click Extracts an Array from the ArrayList via method ToArray Method ToArray can take a single argument to determine the type of the returned array; we obtain the type from the first element in the ArrayList. Creates an empty ArrayList (causing the old list to be erased) and refreshes the display.
76 77 ' handle polygon radio button CheckedChange event 78 Private Sub polygonRadio_CheckedChanged(ByVal sender As _ 79 System.Object, ByVal e As System.EventArgs) _ 80 Handles polygonRadio.CheckedChanged drawWindow.Invalidate() ' refresh panel 83 End Sub ' polygonRadio_CheckedChanged ' handle line radio button CheckChanged event 86 Private Sub lineRadio_CheckedChanged(ByVal sender As _ 87 System.Object, ByVal e As System.EventArgs) _ 88 Handles lineRadio.CheckedChanged drawWindow.Invalidate() ' refresh panel 91 End Sub ' lineRadio_CheckedChanged ' handle filled polygon radio button CheckChanged event 94 Private Sub filledPolygonRadio_CheckedChanged(ByVal sender _ 95 As System.Object, ByVal e As System.EventArgs) _ 96 Handles filledPolygonRadio.CheckedChanged drawWindow.Invalidate() ' refresh panel 99 End Sub ' filledPolygonRadio_CheckedChanged 100 Lines 78–++ define the event handlers for the radio buttons’ CheckedChanged event. Each method refreshes Panel drawWindow to ensure that the panel display reflects the selected drawing type.
101 ' handle cmdNewColor click event 102 Private Sub cmdNewColor_Click(ByVal sender As _ 103 System.Object, ByVal e As System.EventArgs) _ 104 Handles cmdNewColor.Click ' create new color dialog 107 Dim colorBox As ColorDialog = New ColorDialog() ' show dialog and obtain result 110 Dim result As DialogResult = colorBox.ShowDialog() ' return if user cancels 113 If result = DialogResult.Cancel Then 114 Return 115 End If mPen.Color = colorBox.Color ' set pen to new color 118 mBrush.Color = colorBox.Color ' set brush 119 drawWindow.Invalidate() ' refresh panel 120 End Sub ' cmdNewColor_Click End Class ' FrmPolygon Event method cmlNewColor_Click allows the user to select a new drawing color with a ColorDialog
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
Bitmap Class Produce images in color and gray scale with a particular width and height. Used to work with images defined by pixel data. How to use… Dim graphicsObject As Graphics = e.Graphics Dim BitmapVar As Bitmap = New Bitmap(width, height). Dim BrushVar As TextureBrush = New TextureBrush(BitmapVar) graphicsObject.FillRectangle(BrushVar, X, Y, width, height) Advanced Graphics Capabilities
DrawShapes.vb 1 ' Fig : DrawShapes.vb 2 ' Drawing various shapes on a form. 3 4 Imports System.Drawing.Drawing2D 5 6 Public Class FrmDrawShapes 7 Inherits System.Windows.Forms.Form 8 9 ' Visual Studio.NET generated code ' draw various shapes on form 12 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) ' references to object we will use 15 Dim graphicsObject As Graphics = e.Graphics ' ellipse rectangle and gradient brush 18 Dim drawArea1 As Rectangle = New Rectangle(5, 35, 30, 100) 19 Dim linearBrush As LinearGradientBrush = _ 20 New LinearGradientBrush(drawArea1, Color.Blue, _ 21 Color.Yellow, LinearGradientMode.ForwardDiagonal) ' pen and location for red outline rectangle 24 Dim thickRedPen As Pen = New Pen(Color.Red, 10) 25 Dim drawArea2 As Rectangle = New Rectangle(80, 30, 65, 100) ' bitmap texture 28 Dim textureBitmap As Bitmap = New Bitmap(10, 10) 29 Dim graphicsObject2 As Graphics = _ 30 Graphics.FromImage(textureBitmap) ' get bitmap graphics ' brush and pen used throughout program 33 Dim solidColorBrush As SolidBrush = _ 34 New SolidBrush(Color.Red) 35 Dim coloredPen As Pen = New Pen(solidColorBrush) Creates a Pen object pen, and passes arguments Color.Red and Integer 10, indicating that we want pen to draw red lines that are 10 pixels wide Creates a new Bitmap image, which is initially empty retrieves the Graphics object associated with an Image, which may be used to draw on an image.
DrawShapes.vb ' draw ellipse filled with a blue-yellow gradient 38 graphicsObject.FillEllipse(linearBrush, 5, 30, 65, 100) ' draw thick rectangle outline in red 41 graphicsObject.DrawRectangle(thickRedPen, drawArea2) ' fill textureBitmap with yellow 44 solidColorBrush.Color = Color.Yellow 45 graphicsObject2.FillRectangle(solidColorBrush, 0, 0, 10, 10) ' draw small black rectangle in textureBitmap 48 coloredPen.Color = Color.Black 49 graphicsObject2.DrawRectangle(coloredPen, 1, 1, 6, 6) ' draw small blue rectangle in textureBitmap 52 solidColorBrush.Color = Color.Blue 53 graphicsObject2.FillRectangle(solidColorBrush, 1, 1, 3, 3) ' draw small red square in textureBitmap 56 solidColorBrush.Color = Color.Red 57 graphicsObject2.FillRectangle(solidColorBrush, 4, 4, 3, 3) ' create textured brush and display textured rectangle 60 Dim texturedBrush As TextureBrush = _ 61 New TextureBrush(textureBitmap) graphicsObject.FillRectangle( _ 64 texturedBrush, 155, 30, 75, 100) ' 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) A TextureBrush is a brush that fills the interior of a shape with an image, rather than a solid color. draw on the Bitmap a pattern consisting of black, blue, red and yellow rectangles and lines.
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) ' 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 End Sub ' OnPaint End Class ' FrmDrawShapes The DashCap enumeration specifies the styles for the start and end of a dashed line.