CT243 lab9
http://geminga.it.nuigalway.ie/~gettrick/courses/CT243/labs/l9.html
-
For this lab you must submit
-
Handwritten development notes, including statement of the problem, analysis of the problem, algorithm design (which may include flow charts).
-
A printout of form, image and code of your program, and any numerical answers requested.
-
The above material should be given to David or left in the cardboard box marked CT243 in the mail room (room IT415) on the 4th floor of the IT Building
before
the deadline of 5pm
Monday 14th April 2008. You will lose 20% for each day
(or part of day) the lab is late.
The generic syntax for many (but not all) VB
.NET graphics methods follow the pattern
-
DrawObject(Pen, TopLeftXCoordinate,
TopLeftYCoordinate, Width, Height)
for outline shapes
-
FillObject(Brush, TopLeftXCoordinate,
TopLeftYCoordinate, Width, Height)
for solid shapes
Start a new project, and select a standard windows template. By default
you will have one form called Form1. Re-Size this form (just
by dragging the bottom right corner) to make it fairly big.
In the code window corresponding to Form1, write (or cut/paste)
the following code:
Public Class Form1
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim gr As Graphics = e.Graphics
gr.DrawEllipse(Pens.Red, 10, 70, 60, 200)
End Sub
End Class
Note here that _Paint is an event handler:
when you have the blank form Form1 , you
can simply click on it, and at the bottom right, you will see, under
"Properties" a list of possible events: simply double click on Paint, and it
will automagically insert the outline for the
_Paint event handler for you.
Note also the connection between
e
(of type
System.Windows.Forms.PaintEventArgs
),
gr
(of type
Graphics
),
and the
DrawEllipse
method. Once you have
gr
defined, you can use any of the graphics methods:
gr.DrawLine
,
gr.DrawRectangle
,
gr.DrawArc
,
etc.
-
Run the above short program as is, and see what it produces.
-
Experiment with adding a few other graphics methods, with different parameters, to get a feel for the
graphical objects they produce.
-
Write code to draw:
-
a straight line, in green, of width 7, from (10,20) to (30,120)
-
a solid ("filled in") circle, color brown, radius 45, center (90,180)
-
an outline (not filled in) rectangle, border color purple, border width 3, with center (120,180), width 60
and height 20
-
a filled in ellipse, color blue, border color orange, border width 9, center (200,100), width 90, height 30
(Hint: to get a "filled in" ellipse with a different border color, simply draw 2 shapes: a solid ellipse, and
(on top of it) an outline ellipse of the same dimensions)
-
Write a program in VB .NET which draws 100 random solid ellipses on a form. You should pause (using
System.Threading.Thread.Sleep(200)
- this causes the program to pause for 200 milliseconds) between drawing of each shape. The top left coordinate of the ellipse should be a random number
between (10,10) and (150,150). The width & height should be random numbers between 50 and 150. The color of
the ellipse should be a random number between (alpha, Red, Green Blue) values of (1,1,1,1) and (255,255,255,255). To
generate the random integers, you should use code like
Dim RandomClass As New Random()
Dim RanNumA As Integer
RanNumA = RandomClass.Next(50, 54)
(this code will produce a random integer (
RanNumA
) between 50 and 54: i.e. it gives either 50, 51, 52, 53, 54, all with probability 0.2).
©
NUI, Galway