جامعة نزوى · web viewdim mygraphics as drawing.graphics = me.creategraphics dim mypen as...
TRANSCRIPT
Introduction to Visual Programming–COMP300LAB 8_graphics
Graphics Project.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim mygraphics As Graphics = Me.CreateGraphics
Dim mypen As Pen
mypen = New Pen(Brushes.Blue, 10)
mygraphics.DrawLine(mypen, 10, 10, 100, 10)
End Sub
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Brushes.DarkMagenta, 10)
myGraphics.DrawLine(myPen, 10, 10, 100, 10)
myGraphics.DrawLine(myPen, 10, 10, 10, 100)
myGraphics.DrawLine(myPen, 10, 100, 100, 100)
myGraphics.DrawLine(myPen, 100, 100, 100, 10)
Pg-1 Department of Computer Science, College of Art and Sciences, University of NIZWA
Introduction to Visual Programming–COMP300LAB 8_graphics
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen1 As Pen
myPen1 = New Pen(Drawing.Color.Blue, 5)
myGraphics.DrawRectangle(myPen1, 10, 10, 100, 50)
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Red, 5)
myPen.DashStyle = Drawing.Drawing2D.DashStyle.Dot
myGraphics.DrawRectangle(myPen, 10, 10, 100, 50)
The possible values of the line DashStyle of the Pen are listed in the below:
Dot, Dash, DashDot, DashDotDot, Solid, Custom
Pg-2 Department of Computer Science, College of Art and Sciences, University of NIZWA
Introduction to Visual Programming–COMP300LAB 8_graphics
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen As Pen
Dim myRectangle As New Rectangle
myPen = New Pen(Drawing.Color.Blue, 5)
myRectangle.X = 10
myRectangle.Y = 10
myRectangle.Width = 120
myRectangle.Height = 70
myGraphics.DrawEllipse(myPen, myRectangle)
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Blue, 5)
myGraphics.DrawEllipse(myPen, 10, 10, 120, 70)
Pg-3 Department of Computer Science, College of Art and Sciences, University of NIZWA
Introduction to Visual Programming–COMP300LAB 8_graphics
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen As Pen
Dim myRectangle As New Rectangle
myPen = New Pen(Drawing.Color.Blue, 5)
myRectangle.X = 10
myRectangle.Y = 10
myRectangle.Width = 100
myRectangle.Height = 100
myGraphics.DrawEllipse(myPen, myRectangle)
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Blue, 5)
myGraphics.DrawEllipse(myPen, 10, 10, 100, 100)
Pg-4 Department of Computer Science, College of Art and Sciences, University of NIZWA
Introduction to Visual Programming–COMP300LAB 8_graphics
To create the Font object in Visual Basic 2010
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 2010", myFont, myBrush, 10, 10)
InputBoxYou can also add a InputBox which let the user enter his or her message then display the
message on the screen.
Example.Dim myGraphics As Graphics = Me.CreateGraphics
Dim myFont As Font
Dim myBrush As Brush
Dim userMsg As String
userMsg = InputBox("your message?", "Message Entry Form", "Enter your message", 100, 200)
myBrush = New Drawing.SolidBrush(Color.DarkOrchid)
myFont = New System.Drawing.Font("Verdana", 20, FontStyle.Underline)
myGraphics.DrawString(userMsg, myFont, myBrush, 10, 10)
End Sub
Pg-5 Department of Computer Science, College of Art and Sciences, University of NIZWA
Introduction to Visual Programming–COMP300LAB 8_graphics
Drawing a QuadrilateralA quadrilateral is a polygon consists of four sides, so you need to define four vertices. The
following is the code:
Example.Dim myGraphics As Drawing.Graphics = Me.CreateGraphics
Dim myPen As Pen
Dim A As New Point(10, 10)
Dim B As New Point(100, 50)
Dim C As New Point(120, 150)
Dim D As New Point(60, 200)
Dim myPoints As Point() = {A, B, C, D}
myPen = New Pen(Drawing.Color.Blue, 5)
myGraphics.DrawPolygon(myPen, myPoints)
Drawing a pie that starts with 0 degree and sweep clockwise through 60 degree.
ExampleDim myGraphics As Drawing.Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Blue, 5)
myGraphics.DrawPie(myPen, 50,50, 150,150,0,60)
Pg-6 Department of Computer Science, College of Art and Sciences, University of NIZWA
Introduction to Visual Programming–COMP300LAB 8_graphics
Drawing and Filling a Rectangle
The syntax to fill a rectangle with the color defined by the brush object is:
myGraphics.FillRectangle (myBrush, 0, 0, 150, 150)ExampleDim myGraphics As Drawing.Graphics = Me.CreateGraphics
Dim myPen As Pen
Dim myBrush As Brush
myPen = New Pen(Drawing.Color.Blue, 5)
myBrush = New SolidBrush(Color.Coral)
myGraphics.DrawRectangle(myPen, 20, 20, 50, 50)
myGraphics.FillRectangle(myBrush, 20, 20, 50, 50)
Example
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles
Me.Paint
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(1, 1, 100, 100))
e.Graphics.DrawLine(Pens.Blue, New Point(120, 120), New Point(200, 120))
e.Graphics.DrawString("string is text", Me.Font, Brushes.Brown, New Point(120, 120))
e.Graphics.FillRectangle(Brushes.Aqua, New Rectangle(20, 20, 50, 50))
' e.Graphics.Clear(Me.BackColor)
End Sub
Pg-7 Department of Computer Science, College of Art and Sciences, University of NIZWA