On this article we will show you how to create/draw basic shapes (that would be Circle, Triangle, Rectangle, and Square) using ASP.Net on a website. We will also set/compute the area, radius (for Circle), base(for Triangle), and some other operations that is related to a specific shape.
To get started we will create a Point2d class, this class handles the creation of X and Y coordinates.
/// <summary>
/// Class for creating x and y coordinates.
/// </summary>
public class Point2d {
#region Fields
// x dimention
private int _x;
// y dimention
private int _y;
#endregion
#region Constructors
/// <summary>
/// Initializes Point2D with X = 0 and Y = 0.
/// </summary>
public Point2d() {
_x = 0;
_y = 0;
}
/// <summary>
/// Initializes Point2D specifying the X and Y coordinates.
/// </summary>
/// <param name="xPos">The X coordinate.</param>
/// <param name="yPos">The Y coordinate.</param>
public Point2d(int xPos, int yPos) {
_x = xPos;
_y = yPos;
}
/// <summary>
/// Initializes Point2D inheriting another Point2D object.
/// </summary>
/// <param name="pt">The Point2D object.</param>
public Point2d(Point2d pt) {
_x = pt.X;
_y = pt.Y;
}
#endregion
#region Properties
/// <summary>
/// Gets or Sets X coordinates.
/// </summary>
public int X {
get { return _x; }
set { _x = value; }
}
/// <summary>
/// Gets or Sets Y coordinates.
/// </summary>
public int Y {
get { return _y; }
set { _y = value; }
}
#endregion
}
Now we will create the abstract class for all shapes, we will just call it Shape, this class should be inherit by the basic shape that we will create. It contains common methods for all of our shapes, feel free to add some more. You will notice that the created shape images is saved on the temp folder with the name of shape.jpg.
/// <summary>
/// Abstract class for shapes.
/// </summary>
public abstract class Shape : Point2d{
protected Color _color;
protected Color _backColor;
protected System.Drawing.Graphics _graphics;
protected int _frameWith;
protected int _frameHeight;
protected int _penWidth;
protected string _imageFile = "shape.jpg";
protected static string _imageDir = "~/temp/";
public abstract void Draw();
/// <summary>
/// Gets or Sets the area of the shape.
/// </summary>
public abstract double Area {
get ;
set ;
}
/// <summary>
/// Gets or Sets the Shape Image's File Name.
/// </summary>
public string ImageFile {
get { return _imageFile; }
set { _imageFile = value; }
}
/// <summary>
/// Gets or Sets the Color of the Shape.
/// </summary>
public Color Color {
get { return _color; }
set { _color = value; }
}
/// <summary>
/// Gets or Sets the Frame's Width.
/// </summary>
public int FrameWith {
get { return _frameWith; }
set { _frameWith = value; }
}
/// <summary>
/// Gets or Sets the Frame's Height.
/// </summary>
public int FrameHeight {
get { return _frameHeight; }
set { _frameHeight = value; }
}
/// <summary>
/// Gets or Sets the BackColor of the Shape.
/// </summary>
public Color BackColor {
get { return _backColor; }
set { _backColor = value; }
}
/// <summary>
/// Gets or Sets the Pen's width.
/// </summary>
public int PenWidth {
get { return _penWidth; }
set { _penWidth = value; }
}
/// <summary>
/// Gets the Shape's full file path.
/// </summary>
public string ImageFilePath {
get { return _imageDir + _imageFile; }
}
}
And now for the creation of the shapes, first is the Square shape.
/// <summary>
/// Helper utility class for common square functions.
/// </summary>
public class Square : Shape {
#region Fields
double _length;
#endregion
#region Properties
/// <summary>
/// Gets or Sets the Length of the Square.
/// </summary>
public double Length {
get { return _length; }
set { _length = value; }
}
#endregion
#region Constructors
/// <summary>
/// Initialize a Square object.
/// </summary>
public Square() {
}
/// <summary>
/// Initialize a Square object.
/// </summary>
/// <param name="length">The Length of the Square.</param>
public Square(double length) {
_length = length;
}
#endregion
/// <summary>
/// Gets or Sets the area of a Square.
/// </summary>
public override double Area {
get { return System.Math.Pow(_length, 2); }
set { _length = System.Math.Sqrt(value); }
}
public override void Draw() {
Bitmap image = new Bitmap(_frameWith, _frameHeight);
_graphics = System.Drawing.Graphics.FromImage(image);
_graphics.Clear(_backColor);
_graphics.DrawRectangle(new Pen(_color, _penWidth), (float)X, (float)Y, (float)_length, (float)_length);
Page page = System.Web.HttpContext.Current.CurrentHandler as Page;
string imagePath = page.Server.MapPath(_imageDir);
string imageFile = page.Server.MapPath(ImageFilePath);
if (!Directory.Exists(imagePath)) {
Directory.CreateDirectory(imagePath);
}
image.Save(imageFile, ImageFormat.Jpeg);
}
}
Rectangle Shape:
/// <summary>
/// Helper utility class for common rectangle functions.
/// </summary>
public class Rectangle : Shape{
#region Fields
double _width;
double _height;
#endregion
#region Properties
/// <summary>
/// Gets or Sets the Width of the Rectangle.
/// </summary>
public double Width {
get { return _width; }
set { _width = value; }
}
/// <summary>
/// Gets or Sets the Height of the Rectangle.
/// </summary>
public double Height {
get { return _height; }
set { _height = value; }
}
#endregion
#region Constructors
/// <summary>
/// Initialize a Rectangle object.
/// </summary>
public Rectangle() {
}
/// <summary>
/// Initialize a Rectangle object.
/// </summary>
/// <param name="width">The Width of the Rectangle.</param>
/// <param name="height">The Height of the Rectangle.</param>
public Rectangle(double width, double height) {
_width = width;
_height = height;
}
#endregion
/// <summary>
/// Gets or Sets the area of a Rectangle.
/// </summary>
public override double Area {
get { return _width * _height; }
set { throw new NotImplementedException(); }
}
/// <summary>
/// Draws a Rectangle.
/// </summary>
public override void Draw() {
Bitmap image = new Bitmap(_frameWith, _frameHeight);
_graphics = System.Drawing.Graphics.FromImage(image);
_graphics.Clear(_backColor);
_graphics.DrawRectangle(new Pen(_color, _penWidth), (float)X, (float)Y, (float)_width, (float)_height);
Page page = System.Web.HttpContext.Current.CurrentHandler as Page;
string imagePath = page.Server.MapPath(_imageDir);
string imageFile = page.Server.MapPath(ImageFilePath);
if (!Directory.Exists(imagePath)) {
Directory.CreateDirectory(imagePath);
}
image.Save(imageFile, ImageFormat.Jpeg);
}
}
Traingle Shape:
/// <summary>
/// Helper utility class for common triangle functions.
/// </summary>
public class Triangle : Shape{
#region Fields
double _base;
double _height;
#endregion
#region Properties
/// <summary>
/// The Base of the Triangle.
/// </summary>
public double Base {
get { return _base; }
set { _base = value; }
}
/// <summary>
/// The Height of the Triangle.
/// </summary>
public double Height {
get { return _height; }
set { _height = value; }
}
#endregion
#region Constructors
/// <summary>
/// Initialize a Triangle object.
/// </summary>
public Triangle() {
}
/// <summary>
/// Initialize a Triangle object.
/// </summary>
/// <param name="dblBase">The base of the Triangle.</param>
/// <param name="height">The Height of the Triangle.</param>
public Triangle(double dblBase, double height) {
_base = dblBase;
_height = height;
}
#endregion
/// <summary>
/// Gets or Sets the Area of a Triangle.
/// </summary>
public override double Area {
get { return (0.5 * _base) * _height; }
set { throw new NotImplementedException(); }
}
public override void Draw() {
Bitmap image = new Bitmap(102, 102);
Point[] points = { new Point(X, _frameHeight),
new Point((int)_base / 2, _frameHeight - (int)_height),
new Point((int)_base, _frameHeight) };
_graphics = System.Drawing.Graphics.FromImage(image);
_graphics.Clear(_backColor);
_graphics.DrawPolygon(new Pen(_color, _penWidth), points);
Page page = System.Web.HttpContext.Current.CurrentHandler as Page;
string imagePath = page.Server.MapPath(_imageDir);
string imageFile = page.Server.MapPath(ImageFilePath);
if (!Directory.Exists(imagePath)) {
Directory.CreateDirectory(imagePath);
}
image.Save(imageFile, ImageFormat.Jpeg);
}
}
And the last one but not the least, Circle.
/// <summary>
/// Helper utility class for common circle functions.
/// </summary>
public class Circle : Shape {
#region Fields
double _radius;
#endregion
#region Properties
/// <summary>
/// Gets or Sets the Radius of the Circle.
/// </summary>
public double Radius {
get { return _radius; }
set { _radius = value; }
}
/// <summary>
/// Gets or Sets the Circumference of a Circle.
/// </summary>
public double Circumference {
get { return 2 * System.Math.PI * _radius; }
set { _radius = (value / 2) * System.Math.PI; }
}
#endregion
#region Constructors
/// <summary>
/// Initialize a Circle object.
/// </summary>
public Circle() {
}
/// <summary>
/// Initialize a Circle object.
/// </summary>
/// <param name="radius">The Radius of the Circle.</param>
public Circle(double radius) {
_radius = radius;
}
#endregion
/// <summary>
/// Gets or Sets the area of a Circle.
/// </summary>
public override double Area {
get { return System.Math.PI * System.Math.Pow(_radius, 2); }
set { _radius = System.Math.Sqrt(value / System.Math.PI); }
}
/// <summary>
/// Draws a Circle.
/// </summary>
public override void Draw() {
Bitmap image = new Bitmap(_frameWith, _frameHeight);
_graphics = System.Drawing.Graphics.FromImage(image);
_graphics.Clear(_backColor);
_graphics.DrawArc(new Pen(_color, _penWidth), (float) X, (float) Y, (float)_radius, (float)_radius, 0, 360);
Page page = System.Web.HttpContext.Current.CurrentHandler as Page;
string imagePath = page.Server.MapPath(_imageDir);
string imageFile = page.Server.MapPath(ImageFilePath);
if (!Directory.Exists(imagePath)) {
Directory.CreateDirectory(imagePath);
}
image.Save(imageFile, ImageFormat.Jpeg);
}
}

