DrawPolygon() and DrawEllipse() in F#

Introduction

This article explains the DrawPolygon(), FillPolygon(), DrawEllipse() and FillEllipse() functions. Using these functions we can draw ellipses and triangles.

Pen Class

A Pen object draws lines and curves. The Pen object draws a line of a specified width and style. A Pen object can be positioned to draw inside a line or center a line. A Pen object is an instance of the Pen class and is used to render the graphics and text and to draw lines filled with the outlined shapes. Color objects are instances of classes that can be used by pens and brushes to indicate the color of the graphics.

Syntax:

let pen=new Pen(Color.Blue,Width=12.0f)  

 

Brush Class

 

A brush is an instance of any class that derives from the Brush Class and can be used to fill in the shapes or paint text. A Brush object is used to fill in the shapes for the graphics, such as rectangles, circles, triangles ,ellipses, polygons and so on. The SolidBrush class defines a brush for a single color.

Syntax:

let brush=new SolidBrush(Color.Red)  

DrawPolygon( )

The Polygon is drawn by joining each pair of points with a line, and connecting the last point in the array with the first one. The DrawPolygon method draws the outline of the polygon. To fill in the polygon, use the "FillPolygon(pen, point)" method. A polygon can be treated as a closed polyline, where the last point is connected with the first one.

Syntax:

draw.Graphics.DrawPolygon(pen,array))  

FillPolygon( )

The FillPolygon() function fills the interior of a polygon defined by an array of points .

Syntax:

draw.Graphics.FillPolygon(brush,array))  

DrawEllipse( )

The DrawEllipse function draws an ellipse defined by a bounding rectangle specified by a pair of coordinates, a height and a width. The pen determines the color, width and style of the ellipse.

Syntax:

draw.Graphics.DrawEllipse(pen,0.0f,0.0f,160.0f,160.0f))  

FillEllipse( )

The FillEllipse method fills in the interior of an ellipse defined by a bounding rectangle specified by a pair of coordinates , a width, and height.

Syntax:

draw.Graphics.FillEllipse(brush,0.0f,0.0f,160.0f,160.0f)) 

Now  I want to show you how to use the DrawPolygon(), FillPolygon(), DrawEllipse() and FillEllipse() functions in a Windows Forms application. Let's use the following procedure.

Step 1:

Open Visual Studio, then select "Create New Project" --> "F# Console Application".

CreateApplication

Step 2:

Now go to the Solution Explorer on the right side of the application. Right-click on "References" and select "Add references".

SelectReferences


AddReferences

Step 3:

After selecting "Add References", in the framework template you need to select "System.Windows.Forms" and "System.Drawing" while holding down the Ctrl key and Click on "Ok." 

ImortNamespaces

Step 4:

Use the following code to create a triangle using the DrawPolygon method in the F# editor.

open System  

open System.Windows.Forms  

open System.ComponentModel  

open System.Drawing  

let drawtriangleform = new Form(Text="Use DrawPolygon")  

let exitbutton=new Button(Top=190,Left=200)  

exitbutton.Text<-"Exit"

drawtriangleform.Paint.Add(fun draw->  

let array=[|new Point(0,150);new Point(150,10);new Point(300,150)|]  

let pen=new Pen(Color.Blue,Width=12.0f)  

draw.Graphics.DrawPolygon(pen,array))  

drawtriangleform.Controls.Add(exitbutton) 

exitbutton.Click.Add(fun exit->drawtriangleform.Close())     

Application.Run(drawtriangleform)  

 

Step 5:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application you will get a triangle drawn on the Windows Forms application as shown in the following figure.

DrawPolygon

Step 6:

Use the following code to create a triangle using the FillPolygon method in the F# editor.

open System  

open System.Windows.Forms  

open System.ComponentModel  

open System.Drawing  

let filltriangleform = new Form(Text="Use FillPolygon")   

let exitbutton=new Button(Top=190,Left=200)  

exitbutton.Text<-"Exit"

filltriangleform.Paint.Add(fun draw->  

let array=[|new Point(0,150);new Point(150,10);new Point(300,150)|]  

let brush=new SolidBrush(Color.Blue)  

draw.Graphics.FillPolygon(brush,array))  

filltriangleform.Controls.Add(exitbutton)  

exitbutton.Click.Add(fun exit->filltriangleform.Close())    

Application.Run(filltriangleform)  

 

Step 7:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application you will get a Filled Triangle on the Windows Forms application as shown in the following figure.

FillPolygon

Step 8:

Use the following code to create a triangle using the DrawEllipse method in the F# editor.

open System  

open System.Windows.Forms  

open System.ComponentModel  

open System.Drawing  

let drawEllipseform = new Form(Text="Use DrawEllipse")  

let exitbutton=new Button(Top=190,Left=200)  

exitbutton.Text<-"Exit"

drawEllipseform.Paint.Add(fun draw->  

let pen=new Pen(Color.Red,Width=12.0f)  

draw.Graphics.DrawEllipse(pen,0.0f,0.0f,160.0f,160.0f))  

drawEllipseform.Controls.Add(exitbutton)  

exitbutton.Click.Add(fun exit->drawEllipseform.Close()) 

Application.Run(drawEllipseform)  

Step 9:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application you will get a Draw Circle on the Windows Forms application as shown in the following figure.

DrawEllipse

Step 10:

Use the following code to create a triangle using FillEllipse method in  F# editor.

open System  

open System.Windows.Forms  

open System.ComponentModel  

open System.Drawing  

let fillEllipseform = new Form(Text="Use FillEllipse")   

let exitbutton=new Button(Top=190,Left=200)  

exitbutton.Text<-"Exit"

fillEllipseform.Paint.Add(fun draw->  

let brush=new SolidBrush(Color.Red)  

draw.Graphics.FillEllipse(brush,0.0f,0.0f,160.0f,160.0f))  

fillEllipseform.Controls.Add(exitbutton)  

exitbutton.Click.Add(fun exit->fillEllipseform.Close()) 

Application.Run(fillEllipseform)  

Step 11:

Debug the application by press F5 to execute Windows Forms application. After debugging the application you will get a Filled Circle on the Windows Forms application as shown in the following figure.

FillEllipse

Summary

In this article we have explained the  DrawPolygon(), FillPolygon(), DrawEllipse() and FillEllipse() functions in a Windows Forms application.


Similar Articles