GDI+: Line-Based Shapes |
|
Private Sub Form1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint e.Graphics.DrawRectangle(new Pen(Color.Red), 20, 20, 248, 162) End Sub A square is a rectangle whose four sides are equal.
|
A Series of Rectangles |
The DrawRectangle() method is used to draw one rectangle. If you plan to draw many rectangles, you can proceed in one step by using the Graphics.DrawRectangles() method. It comes in two versions whose syntaxes are: Overloads Public Sub DrawRectangles(ByVal pen As Pen, ByVal rects() As Rectangle) Overloads Public Sub DrawRectangles(ByVal pen As Pen, ByVal rects() As RectangleF) This method requires an array of Rectangle or RectangleF values. When executed, it draws individual rectangles using each member of the array as its own rectangle. Here is an example: Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim penCurrent As Pen = New Pen(Color.Red) Dim rect As Rectangle() = {New Rectangle(20, 20, 120, 20), _ New Rectangle(20, 50, 120, 30), _ New Rectangle(20, 90, 120, 40), _ New Rectangle(20, 140, 120, 60)} e.Graphics.DrawRectangles(penCurrent, Rect) End Sub This would produce:
|
Lines |
A Line |
A line is a junction of two points. This means that a line has a beginning and an end: The beginning and the end are two distinct points. Based on this, a line is represented either with two Point values or by four numbers representing its values on the Cartesian axes. To draw a line, the Graphics class is equipped with the following overloaded DrawLine() method: Overloads Public Sub DrawLine(ByVal pen As Pen, ByVal pt1 As Point, ByVal pt2 As Point) Overloads Public Sub DrawLine(ByVal pen As Pen, ByVal pt1 As PointF, ByVal pt2 As PointF) Overloads Public Sub DrawLine(ByVal pen As Pen, ByVal x1 As Integer, ByVal y1 As Integer, _ ByVal x2 As Integer, ByVal y2 As Integer) Overloads Public Sub DrawLine(ByVal pen As Pen, ByVal x1 As Single, ByVal y1 As Single, _ ByVal x2 As Single, ByVal y2 As Single) If the line is represented with natural numbers, its origin can be specified as a Point pt1 and its end would be represented with a Point pt2. If the line is drawn using floating numbers, you can start it at one PointF pt1 and end it at another PointF pt2. Otherwise, you can specify the starting point with coordinates (x1, y1) and the end would be represented with coordinates (x2, y2). The same type of line can be drawn using decimal values from coordinates (x1, y1) to coordinates (x2, y2). Here is an example that draws three lines: |
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles MyBase.Paint Dim penCurrent As Pen = New Pen(Color.Red) e.Graphics.DrawLine(penCurrent, 20, 20, 205, 20) penCurrent = New Pen(Color.Green) e.Graphics.DrawLine(penCurrent, 40, 40, 225, 40) penCurrent = New Pen(Color.Blue) e.Graphics.DrawLine(penCurrent, 30, 60, 215, 60) End Sub |
A Series of Lines |
The above DrawLine() method is used to draw one line. If you intend to draw a group of lines at once, you can use the Graphics.DrawLines() method. It is overloaded with two versions as follows: Overloads Public Sub DrawLines(ByVal pen As Pen, ByVal points() As Point) Overloads Public Sub DrawLines(ByVal pen As Pen, ByVal points() As PointF) To use this method, you should first define an array of either Point for natural numbers that represent Cartesian coordinates or PointF for floating numbers. Here is an example: Private Sub Form1_Paint(System.Object sender, System.Windows.Forms.PaintEventArgs e) Dim as Point Coordinates() = Point(20, 10), Point(205, 20), Point(40, 40), Point(225, 60), Point(30, 80), Point(215, 100) End; Dim as Pen penCurrent = new Pen(Color.Red) e.Graphics.DrawLines(penCurrent, Coordinates) End This would produce:
A polygon is a series of connected lines with the whole shape being closed. In other words, a polygon is defined a group of lines so that, except for the first line of the group, the starting pointof each line is the same as the end pointof the previous line and the end pointof the last line is connected to the start pointof the first line. To draw a polygon, you can use the Graphics.Polygon() method. It is overloaded with two versions as follows: Public Sub DrawPolygon(ByVal pen As Pen, ByVal points() As Point) Public Sub DrawPolygon(ByVal pen As Pen, ByVal points() As PointF) To use this method, you can first declare a Point or PointF array and pass it as the second argument to the method. Here is an example: Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles MyBase.Paint Dim Pt As Point() = {New Point(20, 50), New Point(180, 50), New Point(180, 20), _ New Point(230, 70), New Point(180, 120), New Point(180, 90), _ New Point(20, 90)} Dim penCurrent As New Pen(Color.Red) e.Graphics.DrawPolygon(penCurrent, Pt) End Sub This would produce:
|
|
||
Home | Copyright © 2004-2010 FunctionX, Inc. | |
|