Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Managing Graphics and

Creating Animation
CHAPTER 8
Creating the Graphics Object

A graphics object is created using a CreateGraphics() method. You can create a graphics object that draws to
the form itself or a control.

Dim myGraphics As Graphics = me.CreateGraphics

Dim myGraphics As Graphics = PictureBox1.CreateGraphics

Dim myGraphics As Graphics = TextBox1.CreateGraphics


Creating a Pen

myPen = New Pen(Brushes.DarkMagenta, 10)

Dim myPen As Pen


myPen = New Pen(Drawing.Color.Blue, 5)
Example
Draw a line using graphics my pen and my graphic
function. Use the my pen size (10, 10, 100, 10) to
draw a line with color.
Creating a Rectangle with DrawRectangle
Method

myGraphics.DrawRectangle(myPen, X, Y, width, height)

The code

Dim myPen As Pen


myPen = New Pen(Drawing.Color.Blue, 5)
Dim myGraphics As Graphics = Me.CreateGraphics
myGraphics.DrawRectangle(myPen, 0, 0, 100, 50)
Customizing Line Style of the Pen Object

myPen.DashStyle=Drawing.Drawing2D.DashStyle.Dot

Dash Style Value Line Style


Dot Line consists of dots
Dash Line consists of dashes
DashDot Line consists of alternating dashes and dots
DashDotDot Line consists of alternating dashes and double dots
Solid Solid line
Custom Custom line style
Example
Draw a reactange shape using customizing line
style of the pen object “DashStyle.Dot” and
size of the rectangle ( 10, 10, 100, 50).
Drawing Ellipse and Circle
myGraphics.DrawEllipse(myPen, myRectangle)
Example
Draw an ellipse and a circle using the format
myGraphics.DrawEllipse(myPen, myRectangle)
Drawing Polygon and Pie
Dim A1 As New Point(X1,Y1)
Dim A2 As New Point(X2,Y2)
Dim A3 As New Point(X3,Y3)
.
.
Dim An as New Point(Xn,Yn)

Dim myPoints As Point() = {A1, A2, A3,....,An}


Dim myGraphics As Graphics = Me.CreateGraphics
myGraphics.DrawPolygon(myPen, myPoints)

myPen = New Pen(Drawing.Color.Blue, 5)


Drawing Pie

myGraphics.DrawPie(myPen, X, Y, width,height, StartAngle, SweepAngle)


Filling Shapes with Color

myBrush = New SolidBrush(Color.myColor)


Drawing and Filling a Rectangle

myGraphics.FillRectangle (myBrush, 0, 0, 150, 150)


Creating Animation
End..
Project:
1. Create a Stopwatch
2. Create a Digital Dice
3. Create animation using Timer

You might also like