Visual Basic 2005
VII. GRAPHICS AND MULTIMEDIA

MELJUN CORTES
In this chapter you will learn:
•To understand graphics contexts and graphics objects.
•To manipulate colors and fonts.
•T...
Graphics and Multimedia

The

FCL supports graphics that enable
programmers to visually enhance their
Windows application...
Graphics and Multimedia

 Namespaces

System.Drawing and
System.Drawing.Drawing2D contain the most
commonly used GDI+ com...
Graphics and Multimedia

Class

Graphics contains methods used for
drawing Strings, lines, rectangles and
other shapes on...
Graphics and Multimedia

The

Color structure contains numerous
Shared properties that set the colors of
various graphica...
Graphics and Multimedia

To

begin drawing in Visual Basic, we first
must understand GDI+'s coordinate
system, a scheme f...
Graphics and Multimedia

A

graphics context represents a drawing
surface that enables drawing on the
screen
A Graphics ...
Graphics and Multimedia

 When

drawing on a Form, you can override
method OnPaint to retrieve a Graphics object
from arg...
Graphics and Multimedia

Variable

graphicsObject can now be
used to draw shapes and Strings on the
Form
Instead of over...
Graphics and Multimedia

Controls,

such as Labels and Buttons, do
not have their own graphics contexts, but
you can crea...
Graphics and Multimedia

Colors

can enhance a program's appearance
and help convey meaning
Structure Color defines meth...
Graphics and Multimedia

structure Shared constants and
their RGB values:

Color

Constants in
structure
Color

RGB Value...
Graphics and Multimedia

Color

structure members:

Structure Color
methods and
properties

Description

Common Methods
F...
Graphics and Multimedia

Manipulating

colors

Public Class FrmShowColors
Public Class FrmShowColors
' color for back rec...
Graphics and Multimedia

Manipulating

colors

' set brush color and display back rectangle
' set brush color and display...
Graphics and Multimedia

Manipulating

colors

‘ change Form's foreground color
‘ change Form's foreground color
Private ...
Graphics and Multimedia

Manipulating

colors

Color Control
GRAPHICS AND MULTIMEDIA

Visual Basic 2005

18
Graphics and Multimedia

The

properties of Font objects cannot be
modified
If you need a different Font, you must
creat...
Font class read-only properties

Font

Graphics and Multimedia

class read-only properties

Property

Description

Bold

...
Graphics and Multimedia

Fonts

and FontStyles

Public Class FrmUsingFonts
Public Class FrmUsingFonts
' demonstrate vario...
Graphics and Multimedia

Fonts

and FontStyles

' tahoma, 18 pt strikeout
' tahoma, 18 pt strikeout
style = FontStyle.Str...
Graphics and Multimedia

Fonts

and FontStyles

Font Control
GRAPHICS AND MULTIMEDIA

Visual Basic 2005

23
Graphics and Multimedia

You

can determine precise information
about a font's metrics (or properties),
such as height, d...
Graphics and Multimedia

methods that return fontmetric information

FontFamily
Method

Description

GetCellAscent

Retur...
Graphics and Multimedia

Each

of the drawing methods has several
overloaded versions
Methods that draw hollow shapes ty...
Graphics and Multimedia

 Draws

a line from (x1, y1) to (x2, y2). The Pen
determines the line's color, style and width:
...
Graphics and Multimedia

 Draws

a solid rectangle of the specified width and
height. The top-left corner of the rectangl...
Graphics and Multimedia

 Draws

a filled ellipse inside a bounding rectangle
of the specified width and height. The top-...
Graphics and Multimedia

Demonstration

of methods that draw
lines, rectangles and ellipses
Public Class FrmLinesRectangl...
Graphics and Multimedia

Demonstration

of methods that draw
lines, rectangles and ellipses
' set brush to
' set brush to...
Graphics and Multimedia

Demonstration

of methods that draw
lines, rectangles and ellipses

Drawing Lines, Rectangles an...
Graphics and Multimedia

Arcs

are portions of ellipses and are
measured in degrees, beginning at a
starting angle and co...
Graphics and Multimedia

Positive

and negative arc angles

Drawing Arcs
GRAPHICS AND MULTIMEDIA

Visual Basic 2005

34
Graphics and Multimedia

 Draws

an arc beginning from angle startAngle (in
degrees) and sweeping sweepAngle degrees. The...
Graphics and Multimedia

 Draws

a pie section of an ellipse beginning from
angle startAngle (in degrees) and sweeping
sw...
Graphics and Multimedia

 Functions

similarly to DrawPie, except draws a
solid arc (i.e., a sector). The Brush determine...
Graphics and Multimedia

Polygons

are multisided shapes
There are several Graphics methods used
to draw polygons
Method...
Graphics and Multimedia

The

FCL offers many other graphics
capabilities
The Brush hierarchy, for example, also
include...
Graphics and Multimedia

Shapes

drawn on a form

Advanced Graphics Capabilities
GRAPHICS AND MULTIMEDIA

Visual Basic 20...
Graphics and Multimedia

A

general path is a shape constructed
from straight lines and complex curves
An object of clas...
Graphics and Multimedia

Paths

used to draw stars on a form

Advanced Graphics Capabilities
GRAPHICS AND MULTIMEDIA

Vis...
Graphics and Multimedia

The

FCL offers many convenient ways to
include images and animations in
programs
Multimedia pr...
Graphics and Multimedia

Visual

Basic's multimedia capabilities
include graphics, images, animations and
video

Applicat...
Graphics and Multimedia

The

Windows Media Player control
enables an application to play video and
sound in many multime...
Graphics and Multimedia

Windows

Media Player demonstration

Public Class FrmMediaPlayer
Public Class FrmMediaPlayer
' o...
Graphics and Multimedia

Windows

Media Player demonstration

Windows Media
Player control

Video file loaded
into the Wi...
Graphics and Multimedia

Exercise
GRAPHICS AND MULTIMEDIA

Visual Basic 2005

48
End of Graphics and Multimedia
GRAPHICS AND MULTIMEDIA

Visual Basic 2005

49
Upcoming SlideShare
Loading in …5
×

MELJUN CORTES Visual Bsic 2005 Graphics and Multimedia

465 views

Published on

MELJUN CORTES Visual Bsic 2005 Graphics and Multimedia

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
465
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Visual Bsic 2005 Graphics and Multimedia

  1. 1. Visual Basic 2005 VII. GRAPHICS AND MULTIMEDIA MELJUN CORTES
  2. 2. In this chapter you will learn: •To understand graphics contexts and graphics objects. •To manipulate colors and fonts. •To understand and use GDI+ Graphics methods to draw lines, rectangles, Strings and images. •To use class Image to manipulate and display images. •To draw complex shapes from simple shapes with class GraphicsPath. •To use Windows Media Player to play audio or video in a Visual Basic application. •To use Microsoft Agent to add interactive animated characters to a Visual Basic application. Graphics and Multimedia Objectives GRAPHICS AND MULTIMEDIA Visual Basic 2005 2
  3. 3. Graphics and Multimedia The FCL supports graphics that enable programmers to visually enhance their Windows applications The many sophisticated drawing capabilities are part of namespace System.Drawing and the other namespaces that make up the .NET resource GDI+ GDI+ is an application programming interface (API) that provides classes for creating two-dimensional vector graphics, manipulating fonts and inserting images Overview GRAPHICS AND MULTIMEDIA Visual Basic 2005 3
  4. 4. Graphics and Multimedia  Namespaces System.Drawing and System.Drawing.Drawing2D contain the most commonly used GDI+ components System.Object System.Object Color Color System.MarshalByRefObject System.MarshalByRefObject Point Point Font Font Rectangle Rectangle FontFamily FontFamily Size Size Graphics Graphics Icon Icon Pen Pen HatchBrush HatchBrush Region Region LinearGradientBrush LinearGradientBrush SolidBrush SolidBrush PathGradientBrush PathGradientBrush Brush Brush SolidBrush SolidBrush TextureBrush TextureBrush Drawing Classes and the Coordinate System GRAPHICS AND MULTIMEDIA Visual Basic 2005 4
  5. 5. Graphics and Multimedia Class Graphics contains methods used for drawing Strings, lines, rectangles and other shapes on a Control The drawing methods of class Graphics usually require a Pen or Brush object to render a specified shape The Pen draws shape outlines; the Brush draws solid objects Drawing Classes and the Coordinate System GRAPHICS AND MULTIMEDIA Visual Basic 2005 5
  6. 6. Graphics and Multimedia The Color structure contains numerous Shared properties that set the colors of various graphical components, plus methods that allow users to create new colors Class Font contains properties that define unique fonts Class FontFamily contains methods for obtaining font information Drawing Classes and the Coordinate System GRAPHICS AND MULTIMEDIA Visual Basic 2005 6
  7. 7. Graphics and Multimedia To begin drawing in Visual Basic, we first must understand GDI+'s coordinate system, a scheme for identifying every point on the screen +x (0, 0) +y X Axis (x, y) Y Axis Drawing Classes and the Coordinate System GRAPHICS AND MULTIMEDIA Visual Basic 2005 7
  8. 8. Graphics and Multimedia A graphics context represents a drawing surface that enables drawing on the screen A Graphics object manages a graphics context by controlling how information is drawn Graphics objects contain methods for drawing, font manipulation, color manipulation and other graphics-related actions Graphics Context and Graphics Objects GRAPHICS AND MULTIMEDIA Visual Basic 2005 8
  9. 9. Graphics and Multimedia  When drawing on a Form, you can override method OnPaint to retrieve a Graphics object from argument PaintEventArgs or to create a new Graphics object associated with the appropriate surface  To override the inherited OnPaint method, use the following method header: Protected Overrides Sub OnPaint(PaintEventArgs e) Protected Overrides Sub OnPaint(PaintEventArgs e)  Next, extract the incoming Graphics object from argument PaintEventArg, as in: Dim graphicsObject As Graphics = e.Graphics Dim graphicsObject As Graphics = e.Graphics Graphics Context and Graphics Objects GRAPHICS AND MULTIMEDIA Visual Basic 2005 9
  10. 10. Graphics and Multimedia Variable graphicsObject can now be used to draw shapes and Strings on the Form Instead of overriding the OnPaint method, programmers can add an event handler for the Paint event Visual Studio .NET generates the Paint event handler in this form: Protected Sub MyEventHandler_Paint( _ Protected Sub MyEventHandler_Paint( _ ByVal sender As Object, ByVal e As PaintEventArgs) ByVal sender As Object, ByVal e As PaintEventArgs) Graphics Context and Graphics Objects GRAPHICS AND MULTIMEDIA Visual Basic 2005 10
  11. 11. Graphics and Multimedia Controls, such as Labels and Buttons, do not have their own graphics contexts, but you can create them To draw on a control, first create a graphics object by invoking the control's CreateGraphics method, as in: Dim graphicsObject As Graphics = controlName.CreateGraphics() Dim graphicsObject As Graphics = controlName.CreateGraphics() Graphics Context and Graphics Objects GRAPHICS AND MULTIMEDIA Visual Basic 2005 11
  12. 12. Graphics and Multimedia Colors can enhance a program's appearance and help convey meaning Structure Color defines methods and constants used to manipulate colors Every color can be created from a combination of alpha, red, green and blue components (called ARGB values) All four ARGB components are Bytes that represent integer values in the range 0 to 255 The alpha value determines the opacity of the color Color Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 12
  13. 13. Graphics and Multimedia structure Shared constants and their RGB values: Color Constants in structure Color RGB Value Constants in structure color RGB Value Orange 255, 200, 0 White 255, 255, 255 Pink 255, 175, 175 Gray 128, 128, 128 Cyan 0, 255, 255 DarkGray 64, 64, 64 Magenta 255, 0, 255 Red 255, 0, 0 Yellow 255, 255, 0 Green 0, 255, 0 Black 0, 0, 0 Blue 0, 0, 255 Color Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 13
  14. 14. Graphics and Multimedia Color structure members: Structure Color methods and properties Description Common Methods FromArgb A Shared method that creates a color based on red, green and blue values expressed as ints from 0 to 255. The overloaded version allows specification of alpha, red, green and blue values. FromName A Shared method that creates a color from a name, passed as a String. Common Properties A A byte between 0 and 255, representing the alpha component. R A byte between 0 and 255, representing the red component. G A byte between 0 and 255, representing the green component. B A byte between 0 and 255, representing the blue component. Color Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 14
  15. 15. Graphics and Multimedia Manipulating colors Public Class FrmShowColors Public Class FrmShowColors ' color for back rectangle ' color for back rectangle Private backgroundColor As Color = Color.Wheat Private backgroundColor As Color = Color.Wheat ' color for front rectangle ' color for front rectangle Private foregroundColor As Color = Color.FromArgb(100, 0, 0, 255) Private foregroundColor As Color = Color.FromArgb(100, 0, 0, 255) ' override Form's OnPaint method ' override Form's OnPaint method Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim graphicsObject As Graphics = e.Graphics ' get graphics object Dim graphicsObject As Graphics = e.Graphics ' get graphics object ' create text brush ' create text brush Dim textBrush As New SolidBrush(Color.Black) Dim textBrush As New SolidBrush(Color.Black) ' create solid brush ' create solid brush Dim brush As New SolidBrush(Color.White) Dim brush As New SolidBrush(Color.White) ' draw white background ' draw white background graphicsObject.FillRectangle(brush, 4, 4, 270, 180) graphicsObject.FillRectangle(brush, 4, 4, 270, 180) ' display name of backColor ' display name of backColor graphicsObject.DrawString(BackColor.Name, Me.Font, textBrush, 40, 5) graphicsObject.DrawString(BackColor.Name, Me.Font, textBrush, 40, 5) .. .. .continued .continued Color Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 15
  16. 16. Graphics and Multimedia Manipulating colors ' set brush color and display back rectangle ' set brush color and display back rectangle brush.Color = backgroundColor brush.Color = backgroundColor graphicsObject.FillRectangle(brush, 45, 20, 150, 120) graphicsObject.FillRectangle(brush, 45, 20, 150, 120) ' display Argb values of front color ' display Argb values of front color graphicsObject.DrawString("Alpha: " & foregroundColor.A & _ graphicsObject.DrawString("Alpha: " & foregroundColor.A & _ " Red: " & foregroundColor.R & " Green: " & _ " Red: " & foregroundColor.R & " Green: " & _ foregroundColor.G & " Blue: " & foregroundColor.G & " Blue: " & foregroundColor.B, Me.Font, textBrush, 55, 165) foregroundColor.B, Me.Font, textBrush, 55, 165) ' set brush color and display front rectangle ' set brush color and display front rectangle brush.Color = foregroundColor brush.Color = foregroundColor graphicsObject.FillRectangle(brush, 65, 35, 170, 130) graphicsObject.FillRectangle(brush, 65, 35, 170, 130) End Sub ' OnPaint End Sub ' OnPaint ' change Form's background color ' change Form's background color Private Sub btnColorName_Click(ByVal sender As System.Object, _ Private Sub btnColorName_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ ByVal e As System.EventArgs) _ Handles btnColorName.Click Handles btnColorName.Click ' set backColor to color specified in text box ' set backColor to color specified in text box backgroundColor = Color.FromName(txtColorName.Text) backgroundColor = Color.FromName(txtColorName.Text) Invalidate() ' refresh Form Invalidate() ' refresh Form End Sub ' btnColorName_Click End Sub ' btnColorName_Click .. .. .continued .continued Color Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 16
  17. 17. Graphics and Multimedia Manipulating colors ‘ change Form's foreground color ‘ change Form's foreground color Private Sub btnColorValue_Click(ByVal sender As System.Object, _ Private Sub btnColorValue_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ ByVal e As System.EventArgs) _ Handles btnColorValue.Click Handles btnColorValue.Click ' obtain new front color from text boxes ' obtain new front color from text boxes foregroundColor = Color.FromArgb(Convert.ToInt32(txtAlpha.Text), _ foregroundColor = Color.FromArgb(Convert.ToInt32(txtAlpha.Text), _ Convert.ToInt32(txtRed.Text), _ Convert.ToInt32(txtRed.Text), _ Convert.ToInt32(txtGreen.Text), _ Convert.ToInt32(txtGreen.Text), _ Convert.ToInt32(txtBlue.Text)) Convert.ToInt32(txtBlue.Text)) Invalidate() ' refresh Form Invalidate() ' refresh Form End Sub ' btnColorValue_Click End Sub ' btnColorValue_Click End Class End Class Color Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 17
  18. 18. Graphics and Multimedia Manipulating colors Color Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 18
  19. 19. Graphics and Multimedia The properties of Font objects cannot be modified If you need a different Font, you must create a new Font object There are many overloaded versions of the Font constructor for initializing Font objects Font Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 19
  20. 20. Font class read-only properties Font Graphics and Multimedia class read-only properties Property Description Bold Returns true if the font is bold. FontFamily Returns the font's FontFamily a grouping structure to organize fonts and define their similar properties. Height Returns the height of the font. Italic Returns true if the font is italic. Name Returns the font's name as a String. Size Returns a float value indicating the current font size measured in design units (design units are any specified unit of measurement for the font). SizeInPoints Returns a float value indicating the current font size measured in points. Strikeout Returns True if the font is in strikeout format. Underline Returns true if the font is underlined. Font Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 20
  21. 21. Graphics and Multimedia Fonts and FontStyles Public Class FrmUsingFonts Public Class FrmUsingFonts ' demonstrate various font and style settings ' demonstrate various font and style settings Protected Overrides Sub OnPaint(ByVal paintEvent As PaintEventArgs) Protected Overrides Sub OnPaint(ByVal paintEvent As PaintEventArgs) Dim graphicsObject As Graphics = paintEvent.Graphics Dim graphicsObject As Graphics = paintEvent.Graphics Dim brush As New SolidBrush(Color.DarkBlue) Dim brush As New SolidBrush(Color.DarkBlue) ' arial, 12 pt bold ' arial, 12 pt bold Dim style As FontStyle = FontStyle.Bold Dim style As FontStyle = FontStyle.Bold Dim arial As New Font("Arial", 12, style) Dim arial As New Font("Arial", 12, style) ' times new roman, 12 pt regular ' times new roman, 12 pt regular style = FontStyle.Regular style = FontStyle.Regular Dim timesNewRoman As New Font("Times New Roman", 12, style) Dim timesNewRoman As New Font("Times New Roman", 12, style) ' courier new, 16 pt bold and italic ' courier new, 16 pt bold and italic style = FontStyle.Bold Or FontStyle.Italic style = FontStyle.Bold Or FontStyle.Italic Dim courierNew As New Font("Courier New", 16, style) Dim courierNew As New Font("Courier New", 16, style) .. .. .continued .continued Font Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 21
  22. 22. Graphics and Multimedia Fonts and FontStyles ' tahoma, 18 pt strikeout ' tahoma, 18 pt strikeout style = FontStyle.Strikeout style = FontStyle.Strikeout Dim tahoma As New Font("Tahoma", 18, style) Dim tahoma As New Font("Tahoma", 18, style) graphicsObject.DrawString(arial.Name & _ graphicsObject.DrawString(arial.Name & _ " 12 point bold.", arial, brush, 10, 10) " 12 point bold.", arial, brush, 10, 10) graphicsObject.DrawString(timesNewRoman.Name & _ graphicsObject.DrawString(timesNewRoman.Name & _ " 12 point plain.", timesNewRoman, brush, 10, 30) " 12 point plain.", timesNewRoman, brush, 10, 30) graphicsObject.DrawString(courierNew.Name & _ graphicsObject.DrawString(courierNew.Name & _ " 16 point bold and italic.", courierNew, brush, 10, 50) " 16 point bold and italic.", courierNew, brush, 10, 50) graphicsObject.DrawString(tahoma.Name & _ graphicsObject.DrawString(tahoma.Name & _ " 18 point strikeout.", tahoma, brush, 10, 70) " 18 point strikeout.", tahoma, brush, 10, 70) End Sub ' OnPaint End Sub ' OnPaint End Class End Class Font Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 22
  23. 23. Graphics and Multimedia Fonts and FontStyles Font Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 23
  24. 24. Graphics and Multimedia You can determine precise information about a font's metrics (or properties), such as height, descent, ascent and leading Font Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 24
  25. 25. Graphics and Multimedia methods that return fontmetric information FontFamily Method Description GetCellAscent Returns an Integer representing the ascent of a font as measured in design units. GetCellDescent Returns an Integer representing the descent of a font as measured in design units. GetEmHeight Returns an Integer representing the height of a font as measured in design units. GetLineSpacing Returns an Integer representing the distance between two consecutive lines of text as measured in design units. Font Control GRAPHICS AND MULTIMEDIA Visual Basic 2005 25
  26. 26. Graphics and Multimedia Each of the drawing methods has several overloaded versions Methods that draw hollow shapes typically require as arguments a Pen and four Integers Methods that draw solid shapes typically require as arguments a Brush and four Integers Drawing Lines, Rectangles and Ovals GRAPHICS AND MULTIMEDIA Visual Basic 2005 26
  27. 27. Graphics and Multimedia  Draws a line from (x1, y1) to (x2, y2). The Pen determines the line's color, style and width: DrawLine(ByVal p As Pen, ByVal x1 As Integer, ByVal y1 As Integer, _ DrawLine(ByVal p As Pen, ByVal x1 As Integer, ByVal y1 As Integer, _ ByVal x2 As Integer, ByVal y2 As Integer) ByVal x2 As Integer, ByVal y2 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 rectangle's color, style and border width DrawRectangle(ByVal DrawRectangle(ByVal ByVal ByVal ByVal ByVal p As Pen, ByVal x As Integer, _ p As Pen, ByVal x As Integer, _ y As Integer, ByVal width As Integer, _ y As Integer, ByVal width As Integer, _ height As Integer) height As Integer) Drawing Lines, Rectangles and Ovals GRAPHICS AND MULTIMEDIA Visual Basic 2005 27
  28. 28. Graphics and Multimedia  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. FillRectangle(ByVal FillRectangle(ByVal ByVal ByVal ByVal ByVal b As Brush, ByVal x As Integer, _ b As Brush, ByVal x As Integer, _ y As Integer, ByVal width As Integer, _ y As Integer, ByVal width As Integer, _ height As Integer) height As Integer)  Draws an ellipse inside a bounding rectangle of the specified width and height. The top-left corner of the bounding rectangle is located at (x, y). The Pen determines the color, style and border width of the ellipse. DrawEllipse(ByVal p As Pen, ByVal x As Integer, ByVal y As Integer, _ DrawEllipse(ByVal p As Pen, ByVal x As Integer, ByVal y As Integer, _ ByVal width As Integer, ByVal height As Integer) ByVal width As Integer, ByVal height As Integer) Drawing Lines, Rectangles and Ovals GRAPHICS AND MULTIMEDIA Visual Basic 2005 28
  29. 29. Graphics and Multimedia  Draws a filled ellipse inside a bounding rectangle of the specified width and height. The top-left corner of the bounding rectangle is located at (x, y). The Brush determines the pattern inside the ellipse. FillEllipse(ByVal FillEllipse(ByVal ByVal ByVal ByVal ByVal b As Brush, ByVal x As Integer, _ b As Brush, ByVal x As Integer, _ y As Integer, ByVal width As Integer, _ y As Integer, ByVal width As Integer, _ height As Integer) height As Integer) Drawing Lines, Rectangles and Ovals GRAPHICS AND MULTIMEDIA Visual Basic 2005 29
  30. 30. Graphics and Multimedia Demonstration of methods that draw lines, rectangles and ellipses Public Class FrmLinesRectanglesOvals Public Class FrmLinesRectanglesOvals ' override Form OnPaint method ' override Form OnPaint method Protected Overrides Sub OnPaint(ByVal paintEvent As PaintEventArgs) Protected Overrides Sub OnPaint(ByVal paintEvent As PaintEventArgs) ' get graphics object ' get graphics object Dim g As Graphics = paintEvent.Graphics Dim g As Graphics = paintEvent.Graphics Dim brush As New SolidBrush(Color.Blue) Dim brush As New SolidBrush(Color.Blue) Dim pen As New Pen(Color.Black) Dim pen As New Pen(Color.Black) ' create filled rectangle ' create filled rectangle g.FillRectangle(brush, 90, 30, 150, 90) g.FillRectangle(brush, 90, 30, 150, 90) ' draw lines to ' draw lines to g.DrawLine(pen, g.DrawLine(pen, g.DrawLine(pen, g.DrawLine(pen, g.DrawLine(pen, g.DrawLine(pen, g.DrawLine(pen, g.DrawLine(pen, connect rectangles connect rectangles 90, 30, 110, 40) 90, 30, 110, 40) 90, 120, 110, 130) 90, 120, 110, 130) 240, 30, 260, 40) 240, 30, 260, 40) 240, 120, 260, 130) 240, 120, 260, 130) ' draw top rectangle ' draw top rectangle g.DrawRectangle(pen, 110, 40, 150, 90) g.DrawRectangle(pen, 110, 40, 150, 90) .. .. .continued .continued Drawing Lines, Rectangles and Ovals GRAPHICS AND MULTIMEDIA Visual Basic 2005 30
  31. 31. Graphics and Multimedia Demonstration of methods that draw lines, rectangles and ellipses ' set brush to ' set brush to brush.Color = brush.Color = red red Color.Red Color.Red ' draw base Ellipse ' draw base Ellipse g.FillEllipse(brush, 280, 75, 100, 50) g.FillEllipse(brush, 280, 75, 100, 50) ' draw connecting lines ' draw connecting lines g.DrawLine(pen, 380, 55, 380, 100) g.DrawLine(pen, 380, 55, 380, 100) g.DrawLine(pen, 280, 55, 280, 100) g.DrawLine(pen, 280, 55, 280, 100) ' draw Ellipse outline ' draw Ellipse outline g.DrawEllipse(pen, 280, 30, 100, 50) g.DrawEllipse(pen, 280, 30, 100, 50) End Sub ' OnPaint End Sub ' OnPaint End Class End Class Drawing Lines, Rectangles and Ovals GRAPHICS AND MULTIMEDIA Visual Basic 2005 31
  32. 32. Graphics and Multimedia Demonstration of methods that draw lines, rectangles and ellipses Drawing Lines, Rectangles and Ovals GRAPHICS AND MULTIMEDIA Visual Basic 2005 32
  33. 33. Graphics and Multimedia Arcs are portions of ellipses and are measured in degrees, beginning at a starting angle and continuing for a specified number of degrees called the arc angle An arc is said to sweep (traverse) its arc angle, beginning from its starting angle Arcs that sweep in a clockwise direction are measured in positive degrees Arcs that sweep in a counter-clockwise direction are measured in negative degrees Drawing Arcs GRAPHICS AND MULTIMEDIA Visual Basic 2005 33
  34. 34. Graphics and Multimedia Positive and negative arc angles Drawing Arcs GRAPHICS AND MULTIMEDIA Visual Basic 2005 34
  35. 35. Graphics and Multimedia  Draws an arc beginning from angle startAngle (in degrees) and sweeping sweepAngle degrees. The ellipse is defined by a bounding rectangle of width, height and upper-left corner (x,y). The Pen determines the color, border width and style of the arc DrawArc(ByVal DrawArc(ByVal ByVal ByVal ByVal ByVal p As Pen, ByVal x As Integer, ByVal y As Integer, _ p As Pen, ByVal x As Integer, ByVal y As Integer, _ width As Integer, ByVal height As Integer, _ width As Integer, ByVal height As Integer, _ startAngle As Integer, ByVal sweepAngle As Integer) startAngle As Integer, ByVal sweepAngle As Integer) Drawing Arcs GRAPHICS AND MULTIMEDIA Visual Basic 2005 35
  36. 36. Graphics and Multimedia  Draws a pie section of an ellipse beginning from angle startAngle (in degrees) and sweeping sweepAngle degrees. The ellipse is defined by a bounding rectangle of width, height and upperleft corner (x,y). The Pen determines the color, border width and style of the arc DrawPie(ByVal DrawPie(ByVal ByVal ByVal ByVal ByVal p As Pen, ByVal x As Integer, ByVal y As Integer, _ p As Pen, ByVal x As Integer, ByVal y As Integer, _ width As Integer, ByVal height As Integer, _ width As Integer, ByVal height As Integer, _ startAngle As Integer, ByVal sweepAngle As Integer) startAngle As Integer, ByVal sweepAngle As Integer) Drawing Arcs GRAPHICS AND MULTIMEDIA Visual Basic 2005 36
  37. 37. Graphics and Multimedia  Functions similarly to DrawPie, except draws a solid arc (i.e., a sector). The Brush determines the fill pattern for the solid arc FillPie(ByVal FillPie(ByVal ByVal ByVal ByVal ByVal b As Brush, ByVal x As Integer, ByVal y As b As Brush, ByVal x As Integer, ByVal y As width As Integer, ByVal height As Integer, width As Integer, ByVal height As Integer, startAngle As Integer, ByVal sweepAngle As startAngle As Integer, ByVal sweepAngle As Integer, _ Integer, _ _ _ Integer) Integer) Drawing Arcs GRAPHICS AND MULTIMEDIA Visual Basic 2005 37
  38. 38. Graphics and Multimedia Polygons are multisided shapes There are several Graphics methods used to draw polygons Method Description DrawLines Draws a series of connected lines. The coordinates of each point are specified in an array of Point objects. 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. If the last point is different from the first point, those two points are connected to close the polygon. FillPolygon Draws a solid polygon. The coordinates of each point are specified in an array of Point objects. If the last point is different from the first point, those two points are connected to close the polygon. Drawing Polygons and Polylines GRAPHICS AND MULTIMEDIA Visual Basic 2005 38
  39. 39. Graphics and Multimedia The FCL offers many other graphics capabilities The Brush hierarchy, for example, also includes HatchBrush, LinearGradientBrush, PathGradientBrush and TextureBrush Advanced Graphics Capabilities GRAPHICS AND MULTIMEDIA Visual Basic 2005 39
  40. 40. Graphics and Multimedia Shapes drawn on a form Advanced Graphics Capabilities GRAPHICS AND MULTIMEDIA Visual Basic 2005 40
  41. 41. Graphics and Multimedia A general path is a shape constructed from straight lines and complex curves An object of class GraphicsPath (namespace System.Drawing.Drawing2D) represents a general path The GraphicsPath class provides functionality that enables the creation of complex shapes from vector-based primitive graphics objects Advanced Graphics Capabilities GRAPHICS AND MULTIMEDIA Visual Basic 2005 41
  42. 42. Graphics and Multimedia Paths used to draw stars on a form Advanced Graphics Capabilities GRAPHICS AND MULTIMEDIA Visual Basic 2005 42
  43. 43. Graphics and Multimedia The FCL offers many convenient ways to include images and animations in programs Multimedia programming is an entertaining and innovative field, but one that presents many challenges Multimedia applications demand extraordinary computing power Introduction to Multimedia GRAPHICS AND MULTIMEDIA Visual Basic 2005 43
  44. 44. Graphics and Multimedia Visual Basic's multimedia capabilities include graphics, images, animations and video Application that loads and displays an image based on the width and height set by the user Loading, Displaying and Scaling Images GRAPHICS AND MULTIMEDIA Visual Basic 2005 44
  45. 45. Graphics and Multimedia The Windows Media Player control enables an application to play video and sound in many multimedia formats These include: ◦ MPEG (Motion Pictures Experts Group) audio and video ◦ AVI (audio-video interleave) video ◦ WAV (Windows wave-file format) audio ◦ MIDI (Musical Instrument Digital Interface) audio Windows Media Player GRAPHICS AND MULTIMEDIA Visual Basic 2005 45
  46. 46. Graphics and Multimedia Windows Media Player demonstration Public Class FrmMediaPlayer Public Class FrmMediaPlayer ' open new media file in Windows Media Player ' open new media file in Windows Media Player Private Sub openItem_Click(ByVal sender As System.Object, _ Private Sub openItem_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles openItem.Click ByVal e As System.EventArgs) Handles openItem.Click openMediaFileDialog.ShowDialog() openMediaFileDialog.ShowDialog() ' load and play the media clip ' load and play the media clip player.URL = openMediaFileDialog.FileName player.URL = openMediaFileDialog.FileName End Sub ' openItem_Click End Sub ' openItem_Click ' exit program when exit menu item is clicked ' exit program when exit menu item is clicked Private Sub exitItem_Click(ByVal sender As System.Object, _ Private Sub exitItem_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles exitItem.Click ByVal e As System.EventArgs) Handles exitItem.Click Application.Exit() Application.Exit() End Sub ' exitItem_Click End Sub ' exitItem_Click End Class End Class Windows Media Player GRAPHICS AND MULTIMEDIA Visual Basic 2005 46
  47. 47. Graphics and Multimedia Windows Media Player demonstration Windows Media Player control Video file loaded into the Windows Media Player Windows Media Player GRAPHICS AND MULTIMEDIA Visual Basic 2005 47
  48. 48. Graphics and Multimedia Exercise GRAPHICS AND MULTIMEDIA Visual Basic 2005 48
  49. 49. End of Graphics and Multimedia GRAPHICS AND MULTIMEDIA Visual Basic 2005 49

×