-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRectangle.cs
120 lines (104 loc) · 3.46 KB
/
Rectangle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Drawing;
namespace ShapesGraphics
{
[Serializable]
public class Rectangle : Shape
{
// ----
// Data
// ----
private float width;
private float height;
// -------
// Methods
// -------
public Rectangle(Point p, Color c, bool fill, Pen pen, Brush brush, float width, float height, Image image)
: base(p, c, fill, pen, brush, image)
{
// Throw exception if invalid width
if (width <= 0)
{
ShapesException ex = new ShapesException("Rectangle Shape Constructor Exception",
"Error: Invalid width", DateTime.Now);
ex.Data.Add("Width", width.ToString());
throw ex;
}
else
this.width = width;
// Throw exception if invalid height
if (height <= 0)
{
ShapesException ex = new ShapesException("Rectangle Shape Constructor Exception",
"Error: Invalid height", DateTime.Now);
ex.Data.Add("Height", height.ToString());
throw ex;
}
else
this.height = height;
area = CalcArea();
}
public override void Draw(Graphics g)
{
if (Show)
{
if (Fill)
{
if (Image != null)
{
// Fill with image
g.DrawImage(Image, base.Position.X, base.Position.Y, width, height);
}
else
// Fill with chosen brush
g.FillRectangle(Brush, base.Position.X, base.Position.Y, width, height);
}
else
// Draw border only
g.DrawRectangle(Pen, base.Position.X, base.Position.Y, width, height);
}
}
public override void Resize(int percent)
{
width += (width / 100) * percent;
height += (height / 100) * percent;
CalcArea();
}
public override double CalcArea()
{
area = width * height;
return area;
}
public override double CalcPerimeter()
{
return (width * 2) + (height * 2);
}
public override bool Contains(Point p)
{
if ((p.X >= Position.X) && (p.X <= Position.X + width) &&
(p.Y >= Position.Y) && (p.Y <= Position.Y + height))
return true;
else
return false;
}
public override string ToString()
{
return "Rectangle :: " + base.ToString() + String.Format(", Width: {0}, Height: {1}", width, height);
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
if(obj.GetType().Name != "Rectangle")
return false;
Rectangle r = (Rectangle)obj;
return (r.width == this.width) &&
(r.height == this.height) &&
base.Equals(r);
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
}
}