Title: Visual Basic Graphics
1Visual Basic Graphics
Warning The way the following slides shows
Visual Basic graphics is somewhat different (and
much simpler!) to that of the book. Use these
slides as your main reference.
2We will change color to white
graphicsPicBox
picturebox
Note size 696 , 224
3Assume that all code you see today is written
here.
4When we run our program, we see this form, with a
blank picturebox control
224
696
Almost all computer graphic systems (including
VB) use an upside down Cartesian coordinate
system
5(0,0)
(696,224)
(102,112)
0
112
224
0
348
696
Any point in the Cartesian coordinate system is
specified by a pair of numbers X and Y.
6(0,0)
(696,224)
Let us draw the line above in VB
7There is a built-in function that will draw a
line
graphicsPicBox.CreateGraphics.DrawLine(Pens.Blue,
0, 0, 696, 224)
- The parameter list is
- The Pen object (more later)
- The first points X coordinate
- The first points Y coordinate
- The second points X coordinate
- The second points Y coordinate
Note You must click on the control to make the
line appear
8We can draw multiple lines
graphicsPicBox.CreateGraphics.DrawLine(Pens.Blue,
0, 0, 696, 224) graphicsPicBox.CreateGraphics.Dr
awLine(Pens.Red, 0, 224, 696, 0)
9There is a built-in function that will draw (axis
parallel) rectangles
graphicsPicBox.CreateGraphics.DrawRectangle(Pens.B
lack, 100, 100, 10, 90) graphicsPicBox.CreateGraph
ics.DrawRectangle(Pens.Red, 200, 100, 10,
80) graphicsPicBox.CreateGraphics.DrawRectangle(Pe
ns.Blue, 300, 100, 50, 50)
- The parameter list is
- The Pen
- The top left corner Xs coordinate
- The top left corner Ys coordinate
- The width of the rectangle
- The height of the rectangle
10It is legal (and sometimes useful) to draw
objects that do not fit on the screen (even if
this means giving a negative value for a
coordinate )
graphicsPicBox.CreateGraphics.DrawRectangle(Pens.
Black, -20, 10, 100, 90)
11There is a built-in function that will draw (axis
parallel) ellipses
graphicsPicBox.CreateGraphics.DrawEllipse(Pens.Bl
ack, 100, 100, 90, 60) graphicsPicBox.CreateGraph
ics.DrawEllipse(Pens.Red, 300, 100, 30, 80)
graphicsPicBox.CreateGraphics.DrawEllipse(Pens.Blu
e, 500, 100, 50, 50)
The parameter list is the same as for drawing
rectangles, except we imagine the smallest
rectangle that the ellipses we want could fit
into
12There is a built-in function that will draw arcs
graphicsPicBox.CreateGraphics.DrawArc(Pens.Blue,
100, 20, 350, 150, 0, 160)
The parameter list is the same as for drawing
ellipses, except we also specify the start and
sweep of the arc
13 graphicsPicBox.CreateGraphics.DrawArc(Pens.Blue,
100, 20, 350, 150, 0, 160)
start
sweep
14There is a built-in function that will draw pie
segments
graphicsPicBox.CreateGraphics.DrawPie(Pens.Blue,
100, 20, 350, 150, 0, 160)
The parameter list is the same as for drawing
arcs.
15The Pen revisited
A Pen is an object. We are not going to study
objects in VB too much. For now lets think of it
like this We know that a variable, like age has
exactly one property, its value, for example 24
or 58. We can think of an object, as having more
than one property. What properties do real pens
have? They have color, and width We can declare
and use a new pen like this.. Dim MyPen As New
Pen(Color.Purple, 5) graphicsPicBox.CreateGraphic
s.DrawPie(MyPen, 100, 20, 350, 150, 0, 160)
16Dim MyPen As New Pen(Color.Purple, 5)
graphicsPicBox.CreateGraphics.DrawPie(MyPen,
100, 20, 350, 150, 0, 160)
17Dim MyPen As New Pen(Color.Black, 3)
graphicsPicBox.CreateGraphics.DrawEllipse(MyPen,
100, 100, 90, 60) MyPen.Color Color.Red
MyPen.Width 15 graphicsPicBox.CreateGraphics.Dra
wEllipse(MyPen, 300, 100, 90, 60)
Once we have created a pen, we can change its
properties and reuse it again and again
18The SolidBrush Object I
All the functions we have seen thus far have
solid versions Dim MyBrush As New
SolidBrush(Color.Blue) graphicsPicBox.CreateGraph
ics.FillRectangle(MyBrush, 100, 100, 100, 90)
graphicsPicBox.CreateGraphics.FillEllipse(MyBrush,
200, 100, 100, 90) MyBrush.Color Color.Red
graphicsPicBox.CreateGraphics.FillPie(MyBrush,
300, 20, 350, 150, 0, 160)
19The SolidBrush Object II
A SolidBrush is an object. What properties do
real SolidBrushes have? They have color We can
declare and use a new SolidBrush like this.. Dim
MyBrush As New SolidBrush(Color.Blue) graphicsPicB
ox.CreateGraphics.FillRectangle(MyBrush, 100,
100, 300, 90)
20The Font Object
A Font is an object. What properties do real
Fonts have? They have typeface and size We can
declare and use a new Font like this.. Dim
MyFont New Font("Arial", 46) graphicsPicBox.Crea
teGraphics.DrawString("Pan is Dead", MyFont,
MyBrush, 100, 100)
21(No Transcript)
22(No Transcript)