14
Graphics and Multimedia

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

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

Graphics and Multimedia

Page 2: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

Outline

• Drawing Polygons and Polylines• Advanced Graphics Capabilities

Page 3: 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

Page 4: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

Drawing Polygons and Polylines

Method Description DrawLines Draws a series of connected lines. The coordinates of each point are

specified in an array of Points. If the last point is different from the first point, the figure is not closed.

DrawPolygon Draws a polygon. The coordinates of each point are specified in an array of Point objects. This method draws a closed polygon, even if the last point is different from the first point.

FillPolygon Draws a solid polygon. The coordinates of each point are specified in an array of Points. This method draws a closed polygon, even if the last point is different from the first point.

Fig. 16.19 Graphics methods for drawing polygons.

Page 5: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

1 ' Fig. 16.20: DrawPolygons.vb2 ' Demonstrating polygons.3 4 Public Class FrmPolygon5 Inherits System.Windows.Forms.Form6 21 ' contains list of polygon points 22 Private mPoints As ArrayList = New ArrayList()23 24 ' initialize default pen and brush25 Dim mPen As Pen = New Pen(Color.DarkBlue)26 Dim mBrush As SolidBrush = New SolidBrush(Color.DarkBlue)27 28 ' draw panel mouse down event handler29 Private Sub drawWindow_MouseDown(ByVal sender _30 As Object, ByVal e As _31 System.Windows.Forms.MouseEventArgs) _32 Handles drawWindow.MouseDown33 34 ' Add mouse position to vertex list35 mPoints.Add(New Point(e.X, e.Y))36 drawWindow.Invalidate() ' refresh panel37 End Sub ' drawWindow_MouseDown38 39 ' draw panel paint event handler40 Private Sub drawWindow_Paint(ByVal sender As Object, _41 ByVal e As System.Windows.Forms.PaintEventArgs) _42 Handles drawWindow.Paint43 44 ' get graphics object for panel45 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 storesmouse-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, displaysthe polygon using the method that the user selected via the GUI radio buttons

Page 6: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

46 47 ' if arraylist has 2 or more points, display shape48 If mPoints.Count > 1 Then49 50 ' get array for use in drawing functions51 Dim pointArray() As Point = _52 mPoints.ToArray(mPoints(0).GetType())53 54 If polygonRadio.Checked Then ' draw polygon55 graphicsObject.DrawPolygon(mPen, pointArray)56 57 ElseIf lineRadio.Checked Then ' draw lines58 graphicsObject.DrawLines(mPen, pointArray)59 60 ElseIf filledPolygonRadio.Checked Then ' draw filled61 graphicsObject.FillPolygon(mBrush, pointArray)62 End If63 64 End If65 66 End Sub ' drawWindow_Paint67 68 ' handle cmdClear click event69 Private Sub cmdClear_Click(ByVal sender As System.Object, _70 ByVal e As System.EventArgs) Handles cmdClear.Click71 72 mPoints = New ArrayList() ' remove points73 74 drawWindow.Invalidate() ' refresh panel75 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.

Page 7: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

76 77 ' handle polygon radio button CheckedChange event78 Private Sub polygonRadio_CheckedChanged(ByVal sender As _79 System.Object, ByVal e As System.EventArgs) _80 Handles polygonRadio.CheckedChanged81 82 drawWindow.Invalidate() ' refresh panel83 End Sub ' polygonRadio_CheckedChanged84 85 ' handle line radio button CheckChanged event86 Private Sub lineRadio_CheckedChanged(ByVal sender As _87 System.Object, ByVal e As System.EventArgs) _88 Handles lineRadio.CheckedChanged89 90 drawWindow.Invalidate() ' refresh panel91 End Sub ' lineRadio_CheckedChanged92 93 ' handle filled polygon radio button CheckChanged event94 Private Sub filledPolygonRadio_CheckedChanged(ByVal sender _95 As System.Object, ByVal e As System.EventArgs) _96 Handles filledPolygonRadio.CheckedChanged97 98 drawWindow.Invalidate() ' refresh panel99 End Sub ' filledPolygonRadio_CheckedChanged100

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.

Page 8: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

101 ' handle cmdNewColor click event102 Private Sub cmdNewColor_Click(ByVal sender As _103 System.Object, ByVal e As System.EventArgs) _104 Handles cmdNewColor.Click105 106 ' create new color dialog107 Dim colorBox As ColorDialog = New ColorDialog()108 109 ' show dialog and obtain result110 Dim result As DialogResult = colorBox.ShowDialog()111 112 ' return if user cancels113 If result = DialogResult.Cancel Then114 Return115 End If116 117 mPen.Color = colorBox.Color ' set pen to new color118 mBrush.Color = colorBox.Color ' set brush119 drawWindow.Invalidate() ' refresh panel120 End Sub ' cmdNewColor_Click121 122 End Class ' FrmPolygon

Event method cmlNewColor_Click allows the user to select a new drawing color with a ColorDialog

Page 9: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities
Page 10: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

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

Page 11: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

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.GraphicsDim BitmapVar As Bitmap = New Bitmap(width, height)

.

.

.Dim BrushVar As TextureBrush = New TextureBrush(BitmapVar)graphicsObject.FillRectangle(BrushVar, X, Y, width, height)

Advanced Graphics Capabilities

Page 12: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

DrawShapes.vb

1 ' Fig. 16.21: DrawShapes.vb2 ' Drawing various shapes on a form.3 4 Imports System.Drawing.Drawing2D5 6 Public Class FrmDrawShapes7 Inherits System.Windows.Forms.Form8 9 ' Visual Studio .NET generated code10 11 ' draw various shapes on form12 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)13 14 ' references to object we will use15 Dim graphicsObject As Graphics = e.Graphics16 17 ' ellipse rectangle and gradient brush18 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 rectangle24 Dim thickRedPen As Pen = New Pen(Color.Red, 10)25 Dim drawArea2 As Rectangle = New Rectangle(80, 30, 65, 100)26 27 ' bitmap texture28 Dim textureBitmap As Bitmap = New Bitmap(10, 10)29 Dim graphicsObject2 As Graphics = _30 Graphics.FromImage(textureBitmap) ' get bitmap graphics31 32 ' brush and pen used throughout program33 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.

Page 13: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

DrawShapes.vb

36 37 ' draw ellipse filled with a blue-yellow gradient38 graphicsObject.FillEllipse(linearBrush, 5, 30, 65, 100)39 40 ' draw thick rectangle outline in red41 graphicsObject.DrawRectangle(thickRedPen, drawArea2)42 43 ' fill textureBitmap with yellow44 solidColorBrush.Color = Color.Yellow45 graphicsObject2.FillRectangle(solidColorBrush, 0, 0, 10, 10)46 47 ' draw small black rectangle in textureBitmap48 coloredPen.Color = Color.Black49 graphicsObject2.DrawRectangle(coloredPen, 1, 1, 6, 6)50 51 ' draw small blue rectangle in textureBitmap52 solidColorBrush.Color = Color.Blue53 graphicsObject2.FillRectangle(solidColorBrush, 1, 1, 3, 3)54 55 ' draw small red square in textureBitmap56 solidColorBrush.Color = Color.Red57 graphicsObject2.FillRectangle(solidColorBrush, 4, 4, 3, 3)58 59 ' create textured brush and display textured rectangle60 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 white67 coloredPen.Color = Color.White68 coloredPen.Width = 669 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.

Page 14: Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities

71 72 ' draw lines in green and yellow73 coloredPen.Color = Color.Green74 coloredPen.Width = 575 graphicsObject.DrawLine(coloredPen, 395, 30, 320, 150)76 77 ' draw a rounded, dashed yellow line78 coloredPen.Color = Color.Yellow79 coloredPen.DashCap = LineCap.Round80 coloredPen.DashStyle = DashStyle.Dash81 graphicsObject.DrawLine(coloredPen, 320, 30, 395, 150)82 End Sub ' OnPaint83 84 End Class ' FrmDrawShapes

The DashCap enumeration specifies the styles for the start andend of a dashed line.