Multimedia-Engineering-Lab-Problem-02
Multimedia-Engineering-Lab-Problem-02
Figure 28.1
Therefore, we need to create a Rectangle object before we can draw an ellipse in visual basic
2017. This rectangle serves as a bounding rectangle for the ellipse. However, you still need to
use the DrawEllipse method to finish the job. On the other hand, we can also draw an ellipse
with the DrawEllipse method without first creating a rectangle. We shall show you both
ways.In the first method, let's say you have created a rectangle object known as myRectangle
and a pen object as myPen, then you can draw an ellipse using the following statement:
myGraphics.DrawEllipse(myPen, myRectangle)
* Assume you have also already created the Graphics object myGraphics.
Example 28.1(a)
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
myRectangle.X = 40
myRectangle.Y = 30
myRectangle.Width = 200
myRectangle.Height = 100
myGraphics.DrawEllipse(myPen, myRectangle)
End Sub
The output image is shown in Figure 28.2
Figure 28.2
The second method is using the DrawEllipse method without creating a rectangle object. Of
course, you still have to create the Graphics and the Pen objects. The syntax is:
myGraphics.DrawEllipse(myPen, X,Y,Width, Height)
Where (X, Y) are the coordinates of the upper-left corner of the bounding rectangle, width is
the width of the ellipse and height is the height of the ellipse.
Example 28.1(b)
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
End Sub
28.2 Drawing a Circle
After you have learned how to draw an ellipse, drawing a circle becomes very simple. We use
exactly the same methods used in the preceding section but modify the width and height so
that they are of the same values.
The following examples draw the same circle.
Example 28.2(a)
Dim myPen As Pen
myPen = New Pen(Drawing.Color.DarkTurquoise, 5)
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myRectangle As New Rectangle
myRectangle.X = 90
myRectangle.Y = 30
myRectangle.Width = 100
myRectangle.Height = 100
myGraphics.DrawEllipse(myPen, myRectangle)
Example 28.2(b)
Dim myPen As Pen
myPen = New Pen(Drawing.Color.DarkTurquoise, 5)
Dim myGraphics As Graphics = Me.CreateGraphics
myGraphics.DrawEllipse(myPen, 90, 30, 100, 100)
The output image is shown in Figure 27.3
Figure 28.3
Drawing Text
29.1 Drawing Text
We have learned how to draw rectangle, ellipse, and circle in visual basic 2017 in the
preceding lessons, now we shall learn how to draw text on the screen. In order to draw text
on the screen, we can use the DrawString method. The syntax is as follows:
myGraphics.DrawString(myText, myFont, mybrush, X , Y)
*myGraphics is the Graphics object, myText is the text you wish to display on the screen,
myFont is the font object created by you, myBrush is the brush style created by you and X, Y
are the coordinates of the upper left corner of the Text.
You can create the Font object in visual basic 2017 using the following statement:
myFont = New System.Drawing.Font(“Verdana”, 20)
*The first argument of the font is the font typeface, and the second argument is the font size.
You can add a third argument as font style, either bold, italic, underline.
Here are the examples:
myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Bold)myFont = New
System.Drawing.Font(“Verdana”, 20, FontStyle.Underline)
myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Italic)
End Sub
The runtime interface is as shown in Figure 29.1
Figure 29.1
The preceding example can be modified if you don’t want to create the Font and the Brush
objects. You can use the font of an existing object such as the Form and the System Colors.
Replace the last line in the preceding example with this line(you need to delete the lines that
create the Brush and the Font objects as well)
myGraphics.DrawString(“Visual Basic 2017″, me.Font, System.Drawing.Brushes.DarkOrchid,
10, 10)
You can also add an InputBox which let the user enter his or her message then display the
message on the screen.
This is shown in Example 29.2
Example 29.2
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myFont As Font
Dim myBrush As Brush
Dim userMsg As String
userMsg = InputBox(“What is your message?”, “Message Entry Form”, “Enter your message
here”, 100, 200)
Using Timer
Timer is a control in Visual Basic 2017 that that can be used to create Visual Basic 2017
applications that are time-related. For example, you can use the timer to create a clock, a
stopwatch, a dice, animation and more. The timer is a hidden control at runtime, just like the
engine of a car. We shall illustrate the usage of timer using a few examples.
32.1 Creating a Digital Clock
To create the clock, first of all, start a new project in Visual Basic 2017 and name it MyClock.
Change the text of the Form1 to MyClock in the properties window. Add the Timer control to
the form. Next, insert a label control into the form and change its font size to any size you
wish, and set the text alignment to be the center. Next, set the Interval property of the Timer
control to 1000, which reflects a one-second interval(1 unit is 1 millisecond). Set the
MaximizeBox property of Form1 to false so that the user cannot enlarge the clock. Lastly,
set the Enabled property of the Timer control to True so that the clock starts running as soon
as it is loaded.
Label1.Text = TimeOfDay
*TimeOfDay() is a Visual Basic 2017 built-in function that returns the current time today based
on your computer system time.
Click on the Timer control and enter the code above, as shown below:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Timer1.Tick
LblClock.Text = TimeOfDay
End Sub
The digital clock is as shown in Figure 32.1
Figure 32.1
32.2 Creating a Stopwatch
We can create a simple stopwatch using the Timer control in Visual Basic 2017. Start a new
project and name it stopwatch. Change the Form1 caption to Stopwatch. Insert the Timer
control into the form and set its interval to 1000 which is equal to one second. Besides that,
set the timer Enabled property to False so that it will not start ticking when the program is
started. Insert three buttons and change their names to BtnStart, BtnStop and BtnReset
respectively. Change their texts to “Start”, “Stop” and “Reset” accordingly. Now, enter the code
as follows:
Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles BtnStart.Click
Timer1.Enabled = True
End Sub
Figure 32.2