Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities.

Similar presentations


Presentation on theme: "Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities."— Presentation transcript:

1 Graphics and Multimedia

2 Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

3 Drawing Polygons and Polylines Polygons – Multisided shapes – Graphics methods used to draw polygons DrawLines, DrawPolygon, and FillPolygon

4 Drawing Polygons and Polylines

5 1 ' Fig. 16.20: 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() 23 24 ' initialize default pen and brush 25 Dim 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 Private Sub drawWindow_MouseDown(ByVal sender _ 30 As Object, ByVal e As _ 31 System.Windows.Forms.MouseEventArgs) _ 32 Handles drawWindow.MouseDown 33 34 ' 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 38 39 ' 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 43 44 ' 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

6 46 47 ' if arraylist has 2 or more points, display shape 48 If mPoints.Count > 1 Then 49 50 ' get array for use in drawing functions 51 Dim 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 End If 63 64 End If 65 66 End Sub ' drawWindow_Paint 67 68 ' handle cmdClear click event 69 Private Sub 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 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.

7 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 81 82 drawWindow.Invalidate() ' refresh panel 83 End Sub ' polygonRadio_CheckedChanged 84 85 ' 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 89 90 drawWindow.Invalidate() ' refresh panel 91 End Sub ' lineRadio_CheckedChanged 92 93 ' 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 97 98 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.

8 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 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.Cancel Then 114 Return 115 End If 116 117 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 121 122 End Class ' FrmPolygon Event method cmlNewColor_Click allows the user to select a new drawing color with a ColorDialog

9

10 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

11 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

12 DrawShapes.vb 1 ' Fig. 16.21: 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 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) 19 Dim linearBrush As LinearGradientBrush = _ 20 New LinearGradientBrush(drawArea1, Color.Blue, _ 21 Color.Yellow, LinearGradientMode.ForwardDiagonal) 22 23 ' 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) 26 27 ' bitmap texture 28 Dim 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) 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.

13 DrawShapes.vb 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) 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.

14 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 End Sub ' OnPaint 83 84 End Class ' FrmDrawShapes The DashCap enumeration specifies the styles for the start and end of a dashed line.


Download ppt "Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities."

Similar presentations


Ads by Google