0% found this document useful (0 votes)
14 views7 pages

Multimedia-Engineering-Lab-Problem-02

Multimedia-Engineering-Lab-Problem-02

Uploaded by

Noor Alamin 99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Multimedia-Engineering-Lab-Problem-02

Multimedia-Engineering-Lab-Problem-02

Uploaded by

Noor Alamin 99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Drawing Ellipse & Circle

28.1 Drawing An Ellipse


In this lesson, we shall learn how to draw ellipse and circle.First of all, we need to understand
the principle behind drawing an ellipse in Visual Basic 2017. The basic structure of most
shapes is a rectangle, the ellipse is no exception. Ellipse is an oval shape that is bounded by
a rectangle, as shown in Figure 28.1.

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

Dim myPen As Pen


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

Dim myGraphics As Graphics = Me.CreateGraphics


Dim myRectangle As New Rectangle

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

Dim myPen As Pen


myPen = New Pen(Drawing.Color.DarkTurquoise, 5)
Dim myGraphics As Graphics = Me.CreateGraphics
myGraphics.DrawEllipse(myPen, 40, 30, 200, 100)

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)

myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Regular)


To create your Brush object, you can use the following statement:
Dim myBrush As Brush

myBrush = New Drawing.SolidBrush(Color.BrushColor)


Besides the seven colors, some of the common Brush Colors are AliceBlue, AquaMarine Beige,
DarkMagenta, DrarkOliveGreen, SkyBlue and more. You don’t have to remember the names
of all the colors, the IntelliSense will let you browse through the colors in a drop-down menu
once you type the dot after the word Color.
Now we shall proceed to draw the font using the sample code below:
Example 29.1
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click

Dim myGraphics As Graphics = Me.CreateGraphics


Dim myFont As Font
Dim myBrush As Brush

myBrush = New Drawing.SolidBrush(Color.DarkOrchid)


myFont = New System.Drawing.Font("Verdana", 20, FontStyle.Underline)
myGraphics.DrawString("Visual Basic 2017", myFont, myBrush, 10, 10)

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)

myBrush = New Drawing.SolidBrush(Color.DarkOrchid)


myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Underline)
myGraphics.DrawString(userMsg, myFont, myBrush, 10, 10)

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

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick


LblPanel.Text = Val(LblPanel.Text) + 1
End Sub

Private Sub BtnStop_Click(sender As Object, e As EventArgs) Handles BtnStop.Click


Timer1.Enabled = False
End Sub
Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click
LblPanel.Text = 0
End Sub
The Interface of the Stopwatch is as shown in Figure 32.2

Figure 32.2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy