In: Computer Science
: Let’s say we are working with shapes having different types like 2d, 3D and so on. Using private access to fields of class design a class for rectangle.
c# for both 2d and 3d rectangle
private void Draw2d3d(Graphics gr, Rectangle rect,
rectStyle rect_style, bool sunken)
{
if (rect_style == RectStyle.FixedSingle)
{
rect.Width -= 1;
rect.Height -= 1;
gr.DrawRectangle(Pens.Black, rect);
}
else if (rect_style == RectStyle.Fixed3D)
{
Color[] colors;
if (sunken)
{
colors = new Color[]
{
SystemColors.ControlDark,
SystemColors.ControlDarkDark,
SystemColors.ControlLightLight,
SystemColors.ControlLight
};
}
else
{
colors = new Color[]
{
SystemColors.ControlLightLight,
SystemColors.ControlLight,
SystemColors.ControlDark,
SystemColors.ControlDarkDark
};
}
using (Pen p = new Pen(colors[0]))
{
gr.DrawLine(p, rect.X, rect.Bottom - 1,
rect.X, rect.Y);
gr.DrawLine(p, rect.X, rect.Y,
rect.Right - 1, rect.Y);
}
using (Pen p = new Pen(colors[1]))
{
gr.DrawLine(p, rect.X + 1, rect.Bottom - 2,
rect.X + 1, rect.Y + 1);
gr.DrawLine(p, rect.X + 1, rect.Y + 1,
rect.Right - 2, rect.Y + 1);
}
using (Pen p = new Pen(colors[2]))
{
gr.DrawLine(p, rect.X, rect.Bottom - 1,
rect.Right - 1, rect.Bottom - 1);
gr.DrawLine(p, rect.Right - 1, rect.Bottom - 1,
rect.Right - 1, rect.Y);
}
using (Pen p = new Pen(colors[3]))
{
gr.DrawLine(p, rect.X + 1, rect.Bottom - 2,
rect.Right - 2, rect.Bottom - 2);
gr.DrawLine(p, rect.Right - 2, rect.Bottom - 2,
rect.Right - 2, rect.Y + 1);
}
}
}