GerElement.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /******************************************************************************************
  2. *******************File Name:GerElement.cs,Version 1.0, 8 November 2004********************
  3. *****************************Author: MingZhang***********************
  4. **This class is written to implement various functions in our software like **
  5. ** draw lines, draw rectangle, draw ellipse, insert text box and insert image. **
  6. ** The classes defined here are: **
  7. ** 1.Linegra: This class inherits from GraEleWithPen. **
  8. ** This class will store the start and end points of a line. **
  9. ** 2.RectGra: This class inherits from GraEleWithPenBrush; it stores **
  10. ** the dimensions of rectangle. RectDrawTool inherits from this class. **
  11. ** 3.EllipseGra: This class inherits from GraEleWithPenBrush. **
  12. ** This class stores the dimensions of ellipse. **
  13. ** EllipseDrawTool inherits from this class. ** 5. TextGra: This class inherits from GraEleWithPenBrush. It stores the insert text in this class. TextDrawTool inherits from this class.
  14. ** ** //
  15. ** **
  16. ** **
  17. /******************************************************************************************/
  18. using System;
  19. using System.Drawing;
  20. using System.Drawing.Drawing2D;
  21. using GraEditor.BaseDef;//BaseDef include BrushExt,PenExt,FontData
  22. //Graph element includes Line,Lines,Rectangle,Ellipse,Text,Image
  23. namespace GraEditor.Elements
  24. {
  25. // Line graph element class. This class inherit from GraEleWithPen()t.
  26. // It will store the start and end point of this line in this class. LineDrawTool() inherit from it
  27. public class LineGra : GraEleWithPen
  28. {
  29. //Set start and end point
  30. public LineGra(Pen pen, Point pt1,Point pt2)
  31. :base(pen)
  32. {
  33. this.startPt = pt1;//Start point
  34. this.endPt = pt2; //End point
  35. UpdatePath(); //Store start and end point in to path
  36. }
  37. //Specify Line type
  38. public override GraEleType Type
  39. {
  40. get
  41. {
  42. return GraEleType.Line;
  43. }
  44. }
  45. private Point startPt;//Private start point variable of LineGra
  46. //Start point property of LinGra
  47. public Point StartPt
  48. {
  49. get { return startPt; }
  50. set
  51. {
  52. startPt = value;
  53. UpdatePath();
  54. }
  55. }
  56. private Point endPt;//Private end point variable of LineGra
  57. //End point property of LinGra
  58. public Point EndPt
  59. {
  60. get { return endPt; }
  61. set
  62. {
  63. endPt = value;
  64. UpdatePath();
  65. }
  66. }
  67. //Add start and end point of line into path
  68. public override void UpdatePath()
  69. {
  70. this.path.Reset();//clear GraElement.path
  71. this.path.AddLine(startPt,endPt);//Add line to GraElement.path
  72. }
  73. }
  74. //Lines graph element class. This class inherit from GraEleWithPen()t.
  75. //It will store the start and end point of this line in this class. LinesDrawTool() inherit from it.
  76. public class LinesGra : GraEleWithPen
  77. {
  78. //Set the key points of lines
  79. public LinesGra(Pen pen,Point[] pts)
  80. :base(pen)
  81. {
  82. this.pts = pts;
  83. UpdatePath(); //Save these points into path
  84. }
  85. //Specify Lines type
  86. public override GraEleType Type
  87. {
  88. get
  89. {
  90. return GraEleType.Lines;
  91. }
  92. }
  93. private Point[] pts;//Private key points varible
  94. //Points property of LinsGra
  95. public Point[] Pts
  96. {
  97. get { return pts; }
  98. set //Save key points of lines
  99. {
  100. pts = value;
  101. UpdatePath();
  102. }
  103. }
  104. //Add key points of lines into path
  105. public override void UpdatePath()
  106. {
  107. base.UpdatePath();
  108. this.path.AddLines(this.pts);
  109. }
  110. }
  111. public class EraseLinesGra : LinesGra
  112. {
  113. //Set the key points of lines
  114. public EraseLinesGra(Pen pen,Point[] pts)
  115. :base(pen,pts)
  116. {
  117. }
  118. }
  119. public class FreehandLinesGra : LinesGra
  120. {
  121. //Set the key points of lines
  122. public FreehandLinesGra(Pen pen,Point[] pts)
  123. :base(pen,pts)
  124. {
  125. }
  126. }
  127. //Rectangle draw element class. This class inherit from GraEleWithPenBrush()
  128. //It store the rectangle in this class. RectDrawTool() will inherit from it.
  129. public class RectGra : GraEleWithPenBrush
  130. {
  131. ////Set the size and position of rectangle
  132. public RectGra(Pen pen,Brush brush,GraDrawMode drawMode,Rectangle rect)
  133. :base(pen,brush,drawMode)
  134. {
  135. this.rect = rect;
  136. UpdatePath();
  137. }
  138. //Specify the rectangle drawing element
  139. public override GraEleType Type
  140. {
  141. get
  142. {
  143. return GraEleType.Rect;
  144. }
  145. }
  146. private Rectangle rect;//Private rectangle variable
  147. //Rectangle property of RectGra
  148. public Rectangle Rect
  149. {
  150. get { return rect; }
  151. set
  152. {
  153. rect = value;
  154. UpdatePath();
  155. }
  156. }
  157. //Add a rectangle into path
  158. public override void UpdatePath()
  159. {
  160. base.UpdatePath();
  161. this.path.AddRectangle(this.rect);
  162. }
  163. }
  164. public class EraseRectGra : RectGra
  165. {
  166. public EraseRectGra(Pen pen,Brush brush,GraDrawMode drawMode,Rectangle rect)
  167. :base(pen,brush,drawMode,rect)
  168. {
  169. }
  170. }
  171. //Ellipse draw element class. This class inherit from GraEleWithPenBrush()
  172. //It store the ellipse in this class. EllipseDrawTool() will inherit from it.
  173. public class EllipseGra : GraEleWithPenBrush
  174. {
  175. //Set the size and position of ellipse
  176. public EllipseGra(Pen pen,Brush brush,GraDrawMode drawMode,Rectangle rect)
  177. :base(pen,brush,drawMode)
  178. {
  179. this.rect = rect;
  180. UpdatePath();
  181. }
  182. //Specify the ellipse drawing element
  183. public override GraEleType Type
  184. {
  185. get
  186. {
  187. return GraEleType.Ellipse;
  188. }
  189. }
  190. //Private rectangle variable. We will use a rectangle to decide the position and size of an ellipse.
  191. //The ellipse is in-cut the rectangle
  192. private Rectangle rect;
  193. //Rectangle property of EllipseGra
  194. public Rectangle Rect
  195. {
  196. get { return rect; }
  197. set
  198. {
  199. rect = value;
  200. UpdatePath();
  201. }
  202. }
  203. //Add ellipse(rectangle) into path
  204. public override void UpdatePath()
  205. {
  206. base.UpdatePath();
  207. this.path.AddEllipse(this.rect);
  208. }
  209. }
  210. //Image draw element class. This class inherit from GraElelement()
  211. //It store the insert picture in this class. ImageDrawTool() inherit from it.
  212. public class ImageGra : GraElement
  213. {
  214. //Set the insert picture and its size and position to ImageGra
  215. public ImageGra(Image image, Rectangle rect)
  216. {
  217. this.image = image; //set insert picture to ImageGra
  218. this.rect = rect; //set the size and position of insert picture to ImageGra
  219. }
  220. //Specify the image drawing element
  221. public override GraEleType Type
  222. {
  223. get
  224. {
  225. return GraEleType.Image;
  226. }
  227. }
  228. private Image image;//Private image variable of ImageGra to save insert picture
  229. //Image property of ImageGra
  230. public Image Image
  231. {
  232. get { return image; }
  233. set { image = value; }
  234. }
  235. //Private rectangle variable of ImageGra to save the size and position of rectangle
  236. private Rectangle rect;
  237. //Rectangle property of ImageGra
  238. public Rectangle Rect
  239. {
  240. get { return rect; }
  241. set { rect = value; }
  242. }
  243. //Draw the insert picture
  244. public override void Draw(Graphics g)
  245. {
  246. if(this.image == null || this.rect.IsEmpty)//No image exist
  247. return;
  248. g.DrawImage(this.image,this.rect);// Draw the picture in the area of rect.
  249. }
  250. }
  251. //Text draw element class. This class inherit from GraEleWithPenBrush()
  252. //It store the insert text in this class. TextDrawTool() inherit from it.
  253. public class TextGra : GraEleWithPenBrush
  254. {
  255. //set the insert text and its font to TextGra
  256. public TextGra(string text,Font font,Brush brush,Rectangle rect)
  257. :base(null,brush,GraDrawMode.Fill)
  258. {
  259. this.fontData = new FontData(font);
  260. this.text = text;
  261. this.rect = rect;
  262. }
  263. //Specify the image drawing element
  264. public override GraEleType Type
  265. {
  266. get
  267. {
  268. return GraEleType.Text;
  269. }
  270. }
  271. private Rectangle rect;//private point variable to save the insert point of text
  272. //Position property of TextGra
  273. public Rectangle Rect
  274. {
  275. get { return rect; }
  276. set { rect = value; }
  277. }
  278. private string text; //private text variable to save the insert text
  279. //Text property of TextGra
  280. public string Text
  281. {
  282. get { return text;}
  283. set { text = value;}
  284. }
  285. private FontData fontData;//private font variable to save the font of the insert text
  286. //Font property of TextGra
  287. public FontData FontData
  288. {
  289. get { return fontData; }
  290. set { fontData = value; }
  291. }
  292. //Draw the insert text
  293. public override void Draw(Graphics g)
  294. {
  295. if(fontData.IsEmpty())//No font
  296. return;
  297. using(Font f = new Font(fontData.Name,fontData.Size,fontData.Style))
  298. {
  299. g.DrawString(this.text,f,this.brushExt.Brush,this.rect);
  300. }
  301. }
  302. }
  303. }