20
Graphics and Multimedia

Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

Embed Size (px)

DESCRIPTION

Font Control Font s – 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 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.

Citation preview

Page 1: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

Graphics and Multimedia

Page 2: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

OUTLINE

• Font Control• Drawing Lines, Rectangles and Ovals• Drawing Arcs• Drawing a General Path

Page 3: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

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 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.

Page 4: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

• DrawString constructors:– takes a String to display, – the display Font,– a Brush – and the x- and y-coordinates of the location for the String’s first

character.

Font Control

Page 5: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

Font ControlProperty Description Bold Tests a font for a bold font style. Returns True if the font is bold.

FontFamily Represents the FontFamily of the Font (a grouping structure to organize fonts and define their similar properties).

Height Represents the height of the font. Italic Tests a font for an italic font style. Returns True if the font is italic. Name Represents the font’s name as a String. Size Returns a Single value indicating the current font size measured in

design units (design units are any specified units of measurement for the font).

SizeInPoints Returns a Single value indicating the current font size measured in points.

Strikeout Tests a font for a strikeout font style. Returns True if the font is in strikeout format.

Underline Tests a font for a underline font style. Returns True if the font is underlined.

Fig. 16.8 Font class read-only properties.

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

Page 6: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

1 ' Fig. 16.9: UsingFonts.vb2 ' Demonstrating various font settings.3 4 Public Class FrmFonts5 Inherits System.Windows.Forms.Form6 9 ' demonstrate various font and style settings10 Protected Overrides Sub OnPaint( ByVal paintEvent As PaintEventArgs)12 13 Dim graphicsObject As Graphics = paintEvent.Graphics14 Dim brush As SolidBrush = New SolidBrush(Color.DarkBlue)15 16 ' arial, 12 pt bold17 Dim style As FontStyle = FontStyle.Bold18 Dim arial As Font = New Font( New FontFamily("Arial"), 12, style)20 21 ' times new roman, 12 pt regular22 style = FontStyle.Regular23 Dim timesNewRoman As Font = New Font( "Times New Roman", 12, style)25 26 ' courier new, 16 pt bold and italic27 style = FontStyle.Bold Or FontStyle.Italic28 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

Page 7: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

31 ' tahoma, 18 pt strikeout32 style = FontStyle.Strikeout33 Dim tahoma As Font = New Font("Tahoma", 18, style)3435 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 ' OnPaint47 48 End Class ' FrmFonts

Page 8: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

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

Page 9: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

Drawing Lines, Rectangles and Ovals

Graphics Drawing Methods and Descriptions Note: Many of these methods are overloaded—consult the documentation for a full listing.

DrawLine(ByVal p As Pen, ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer) Draws a line from (x1, y1) to (x2, y2). The Pen determines the color, style and width of the line. DrawRectangle(ByVal p As Pen, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) Draws a rectangle of the specified width and height. The top-left corner of the rectangle is at point (x, y). The Pen determines the color, style, and border width of the rectangle. FillRectangle(ByVal b As Brush, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) Draws a solid rectangle of the specified width and height. The top-left corner of the rectangle is at point (x, y). The Brush determines the fill pattern inside the rectangle. DrawEllipse(ByVal p As Pen, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) Draws an ellipse inside a rectangle. The width and height of the rectangle are as specified, and its top-left corner is at point (x, y). The Pen determines the color, style and border width of the ellipse. FillEllipse(ByVal b As Brush, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) Draws a filled ellipse inside a rectangle. The width and height of the rectangle are as specified, and its top-left corner is at point (x, y). The Brush determines the pattern inside the ellipse. Fig. 16.13 Graphics methods that draw lines, rectangles and ovals.

Page 10: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

Drawing Lines, Rectangles and Ovals

Fig. 16.15 Ellipse bounded by a rectangle.

height

width

(x, y)

Page 11: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

1 ' Fig. 16.14: LinesRectanglesOvals.vb2 ' Demonstrating lines, rectangles, and ovals.3 4 Public Class FrmDrawing5 Inherits System.Windows.Forms.Form6 7 ' Visual Studio .NET generated code8 9 ' display ovals lines, and rectangles10 Protected Overrides Sub OnPaint( _11 ByVal paintEvent As PaintEventArgs)12 13 ' get graphics object14 Dim g As Graphics = paintEvent.Graphics15 Dim brush As SolidBrush = New SolidBrush(Color.Blue)16 Dim pen As Pen = New Pen(Color.AliceBlue)17 18 ' create filled rectangle19 g.FillRectangle(brush, 90, 30, 150, 90)20 21 ' draw lines to connect rectangles22 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 rectangle28 g.DrawRectangle(pen, 110, 40, 150, 90)29 30 ' set brush to red31 brush.Color = Color.Red32 33 ' draw base Ellipse34 g.FillEllipse(brush, 280, 75, 100, 50)35

Page 12: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

36 ' draw connecting lines37 g.DrawLine(pen, 380, 55, 380, 100)38 g.DrawLine(pen, 280, 55, 280, 100)39 40 ' draw Ellipse outline41 g.DrawEllipse(pen, 280, 30, 100, 50)42 End Sub ' OnPaint43 44 End Class ' FrmDrawing

Page 13: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

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

Page 14: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

Drawing Arcs

Fig. 16.16 Positive and negative arc angles.

Page 15: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

Drawing Arcs

Page 16: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

1 ' Fig. 16.18: DrawArcs.vb2 ' Drawing various arcs on a form.3 4 Public Class FrmArcTest5 Inherits System.Windows.Forms.Form8 9 Protected Overrides Sub OnPaint( _10 ByVal paintEvent As PaintEventArgs)11 12 ' get graphics object13 Dim graphicsObject As Graphics = paintEvent.Graphics14 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 degrees21 graphicsObject.DrawRectangle(pen1, rectangle1)22 graphicsObject.DrawArc(pen2, rectangle1, 0, 360)23 24 ' start at 0 and sweep 110 degrees25 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 degrees30 rectangle1.Location = New Point(185, 35)31 graphicsObject.DrawRectangle(pen1, rectangle1)32 graphicsObject.DrawArc(pen2, rectangle1, 0, -270)33

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

Page 17: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

34 ' start at 0 and sweep 360 degrees35 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 degrees41 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 degrees46 rectangle1.Location = New Point(185, 120)47 graphicsObject.DrawRectangle(pen1, rectangle1)48 graphicsObject.FillPie(brush2, rectangle1, 0, -270)49 End Sub ' OnPaint50 51 End Class ' FrmArcTest

Sets the Size property to a new Size object

Page 18: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs 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.GraphicsgraphicsObject.TranslateTransform(X,Y)graphicsObject.RotateTransform(angle)

Drawing a General Path

Page 19: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

DrawStars.vb

1 ' Fig. 16.22: DrawStars.vb2 ' Using paths to draw stars on a form.3 4 Imports System.Drawing.Drawing2D5 6 Public Class FrmDrawStars7 Inherits System.Windows.Forms.Form8 9 ' Visual Studio .NET generated code10 11 ' create path and draw stars along it12 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)13 Dim graphicsObject As Graphics = e.Graphics14 Dim i As Integer15 Dim random As Random = New Random()16 Dim brush As SolidBrush = _17 New SolidBrush(Color.DarkMagenta)18 19 ' x and y points of path20 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 star26 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 points32 For i = 0 To 8 Step 233 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

Page 20: Graphics and Multimedia. OUTLINE Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing a General Path

36 37 ' close shape38 star.CloseFigure()39 40 ' rotate origin and draw stars in random colors41 For i = 1 To 1842 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 Next49 50 End Sub ' OnPaint51 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