Private myPen as Pen Private formGraphics As System.Drawing.Graphics Private p2vbW As Integer Private p2vbH As Integer Private p2vbX1 As Integer Private p2vbY1 As Integer Private p2vbX2 As Integer Private p2vbY2 As Integer Private myBrush As System.Drawing.SolidBrush ' Function allowsInteractively chose polygon vertices, ending when click above line. ' Illustrates use of while loops, Text, Line, Polygon, getMouse. ' ' from graphics import * Public Function isBetween(x As Object, end1 As Object, end2 As Object) ' Return True if x is between the ends or equal to either. ' The ends do not need to be in increasing order. Return end1 <= x <= end2 or end2 <= x <= end1 End Function Public Function isInside(point As Object, rect As Object) ' Return True if the point is inside the Rectangle rect. Dim pt1 As Object pt1 = rect.getP1() Dim pt2 As Object pt2 = rect.getP2() Return isBetween(point.getX(), pt1.getX(), pt2.getX()) and isBetween(point.getY(), pt1.getY(), pt2.getY()) End Function Public Function polyHere(rect As Object, win As Object) ' Draw a polygon interactively in Rectangle rect, in GraphWin win. ' Collect mouse clicks inside rect into a Polygon. ' When a click goes outside rect, stop and Return the final polygon. ' The polygon ends up drawn. The method draws and undraws rect. Form1.Controls.Add(rect) Dim vertices As List(Of Object) vertices = New List(Of Object) Dim pt As Object Dim poly As Point() = {vertices} while isInside(pt, rect) vertices.add(pt) formGraphics = Form1.CreateGraphics() myPen = New Pen(Drawing.Color.Black, 1) formGraphics.DrawPolygon(myPen, poly) End While Return poly End Function Public Sub main() Dim winWidth As Integer winWidth = 400 Dim winHeight As Integer winHeight = 400 Form1.Text = "Drawing Polygons" Form1.Width = winWidth + 100 p2vbW = winWidth Form1.Height = winHeight + 100 p2vbH = winHeight p2vbX1 = 0 p2vbY1 = 0 p2vbX2 = winWidth p2vbY2 = winHeight Dim instructions As New Label instructions.Left = p2vbW * ((winWidth/2) / (p2vbX2 - p2vbX1)) instructions.Top = p2vbH - (p2vbH * (( 30) / (p2vbY2 - p2vbY1))) instructions.Text = "Click vertices inside the red rectangle."+"" + vbNewLine + "Click outside the rectangle to stop." instructions.AutoSize = True Form1.Controls.Add(instructions) Dim rect1 As Rectangle rect1 = New Rectangle(New Point(5, 55), New Point(200, 120)) myPen = New Pen(Drawing.Color.Black, 1) formGraphics = Form1.CreateGraphics() formGraphics.DrawRectangle(myPen, rect1) Dim poly1 As Object Dim win As Object poly1 = polyHere(rect1, win) myBrush = New System.Drawing.SolidBrush(System.Drawing.Color.green) formGraphics.FillRectangle(myBrush, New Rectangle(5, 55, 200, 120)) Dim rect2 As Rectangle rect2 = New Rectangle(New Point(210, 50), New Point(350, 350)) myPen = New Pen(Drawing.Color.Black, 1) formGraphics.DrawRectangle(myPen, rect2) Dim poly2 As Object poly2 = polyHere(rect2, win) instructions.Text = "Click anywhere to quit." End Sub 'main()