LinearGradientBrush.jvm.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using System;
  2. using java.awt;
  3. using awt = java.awt;
  4. using geom = java.awt.geom;
  5. using image = java.awt.image;
  6. namespace System.Drawing.Drawing2D {
  7. /// <summary>
  8. /// Summary description for LinearGradientBrush.
  9. /// </summary>
  10. public sealed class LinearGradientBrush : Brush {
  11. Blend _blend;
  12. bool _gammaCorrection;
  13. ColorBlend _interpolationColors;
  14. WrapMode _wrapmode;
  15. RectangleF _gradientRectangle;
  16. // gradient brush data
  17. float _x1 = 0;
  18. float _y1 = 0;
  19. float _x2 = 0;
  20. float _y2 = 0;
  21. Color _color1, _color2;
  22. bool _cyclic;
  23. #region NativeObject
  24. GradientPaint _nativeObject = null;
  25. protected override Paint NativeObject {
  26. get {
  27. if ( _nativeObject == null )
  28. _nativeObject = new GradientPaint(
  29. _x1, _y1,
  30. new java.awt.Color(_color1.R, _color1.G, _color1.B, _color1.A),
  31. _x2, _y2,
  32. new java.awt.Color(_color2.R, _color2.G, _color2.B, _color2.A), _cyclic);
  33. return _nativeObject;
  34. }
  35. }
  36. #endregion
  37. #region Initialization
  38. private void Init(float x1, float y1, Color color1, float x2, float y2, Color color2, bool cyclic) {
  39. _x1 = x1;
  40. _y1 = y1;
  41. _color1 = color1;
  42. _x2 = x2;
  43. _y2 = y2;
  44. _color2 = color2;
  45. _cyclic = cyclic;
  46. _nativeObject = null;
  47. }
  48. private void Init(float x1, float y1, Color color1, float x2, float y2, Color color2, float angle) {
  49. _gradientRectangle = new RectangleF(x1, y1, x2-x1, y2-y1);
  50. PointF [] points = GetMedianeEnclosingRect(x1, y1, x2, y2, angle);
  51. Init(points[0].X, points[0].Y, color1, points[1].X, points[1].Y, color2, false);
  52. }
  53. private void Init(float x1, float y1, Color color1, float x2, float y2, Color color2, LinearGradientMode linearGradientMode) {
  54. _gradientRectangle = new RectangleF(x1, y1, x2-x1, y2-y1);
  55. PointF [] points;
  56. switch (linearGradientMode) {
  57. case LinearGradientMode.Horizontal :
  58. Init(x1, y1, color1, x2, y1, color2, false);
  59. break;
  60. case LinearGradientMode.Vertical :
  61. Init(x1, y1, color1, x1, y2, color2, false);
  62. break;
  63. case LinearGradientMode.BackwardDiagonal :
  64. points = GetMedianeEnclosingRect(x1, y1, x2, y2, false);
  65. Init(points[0].X, points[0].Y, color2, points[1].X, points[1].Y, color1, false);
  66. break;
  67. case LinearGradientMode.ForwardDiagonal :
  68. points = GetMedianeEnclosingRect(x1, y1, x2, y2, true);
  69. Init(points[0].X, points[0].Y, color1, points[1].X, points[1].Y, color2, false);
  70. break;
  71. default :
  72. throw new ArgumentException("LinearGradientMode");
  73. }
  74. }
  75. #endregion
  76. #region Constructors
  77. private LinearGradientBrush (float x1, float y1, Color color1, float x2, float y2, Color color2, bool cyclic) {
  78. Init(x1, y1, color1, x2, y2, color2, cyclic);
  79. }
  80. internal LinearGradientBrush (float x1, float y1, Color color1, float x2, float y2, Color color2, LinearGradientMode mode) {
  81. Init(x1, y1, color1, x2, y2, color2, mode);
  82. }
  83. internal LinearGradientBrush (float x1, float y1, Color color1, float x2, float y2, Color color2) {
  84. Init(x1, y2, color1, x1, y2, color2, false);
  85. }
  86. public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2) {
  87. _gradientRectangle = new RectangleF(point1.X, point1.Y, point2.X - point1.X, point2.Y - point2.Y);
  88. Init(point1.X, point1.Y, color1, point2.X, point2.Y, color2, false);
  89. }
  90. public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2) {
  91. _gradientRectangle = new RectangleF(point1.X, point1.Y, point2.X - point1.X, point2.Y - point2.Y);
  92. Init(point1.X, point1.Y, color1, point2.X, point2.Y, color2, false);
  93. }
  94. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode) {
  95. Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, linearGradientMode);
  96. }
  97. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode) {
  98. Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, linearGradientMode);
  99. }
  100. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) {
  101. Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, angle);
  102. }
  103. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) {
  104. Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, angle);
  105. }
  106. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable):
  107. this(rect, color1, color2, angle) {
  108. }
  109. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable):
  110. this(rect, color1, color2, angle) {
  111. }
  112. #endregion
  113. #region GetMedianeEnclosingRect
  114. internal PointF [] GetMedianeEnclosingRect(float x1, float y1, float x2, float y2, float rotateAngle) {
  115. float width = x2 - x1;
  116. float height = y2 - y1;
  117. PointF rectCenter = new PointF(x1 + width/2, y1 + height/2);
  118. float gradLen = width * Math.Abs((float)Math.Cos(rotateAngle * Math.PI / 180)) +
  119. height * Math.Abs((float)Math.Sin(rotateAngle * Math.PI / 180));
  120. PointF [] points = new PointF []{ new PointF(rectCenter.X - gradLen/2, rectCenter.Y),
  121. new PointF(rectCenter.X + gradLen/2, rectCenter.Y) };
  122. Matrix mx = new Matrix();
  123. mx.RotateAt((float)rotateAngle, rectCenter);
  124. mx.TransformPoints(points);
  125. return points;
  126. }
  127. internal PointF [] GetMedianeEnclosingRect(float x1, float y1, float x2, float y2, bool forwardDiagonal) {
  128. float width = x2 - x1;
  129. float height = y2 - y1;
  130. PointF rectCenter = new PointF(x1 + width/2, y1 + height/2);
  131. float rotateAngle = (float)Math.Atan2(width, height);
  132. float gradLen = width * (float)Math.Cos(rotateAngle);
  133. if (!forwardDiagonal)
  134. rotateAngle = -rotateAngle;
  135. PointF [] points = new PointF []{ new PointF(rectCenter.X - gradLen, rectCenter.Y),
  136. new PointF(rectCenter.X + gradLen, rectCenter.Y) };
  137. Matrix mx = new Matrix();
  138. mx.RotateAt((float)rotateAngle * (float)(180/Math.PI), rectCenter);
  139. mx.TransformPoints(points);
  140. return points;
  141. }
  142. #endregion
  143. #region Public Properties
  144. // FALLBACK: no functionality implemented for this property
  145. [MonoTODO]
  146. public Blend Blend {
  147. get {
  148. return _blend;
  149. }
  150. set {
  151. _blend = value;
  152. }
  153. }
  154. // FALLBACK: no functionality implemented for this property
  155. [MonoTODO]
  156. public bool GammaCorrection {
  157. get {
  158. return _gammaCorrection;
  159. }
  160. set {
  161. _gammaCorrection = value;
  162. }
  163. }
  164. // FALLBACK: functionality of two color gradient is implemented
  165. [MonoTODO]
  166. public ColorBlend InterpolationColors {
  167. get {
  168. return _interpolationColors;
  169. }
  170. set {
  171. if (value == null)
  172. throw new ArgumentNullException("ColorBlend");
  173. if ( (value.Colors == null) || (value.Colors.Length == 0) )
  174. throw new ArgumentException("ColorBlend");
  175. _interpolationColors = value;
  176. _color1 = value.Colors[0];
  177. _color2 = value.Colors[value.Colors.Length - 1];
  178. _nativeObject = null;
  179. }
  180. }
  181. public Color [] LinearColors {
  182. get {
  183. Color [] cl = new Color[2];
  184. cl[0] = _color1;
  185. cl[1] = _color2;
  186. return cl;
  187. }
  188. set {
  189. if (value == null)
  190. throw new ArgumentNullException("colors");
  191. _color1 = value[0];
  192. _color2 = value[1];
  193. _nativeObject = null;
  194. }
  195. }
  196. public RectangleF Rectangle {
  197. get {
  198. return _gradientRectangle;
  199. }
  200. }
  201. public Matrix Transform {
  202. get { return BrushTransform; }
  203. set { BrushTransform = value; }
  204. }
  205. // FALLBACK: not functionality implemented for this property
  206. [MonoTODO]
  207. public WrapMode WrapMode {
  208. get {
  209. return _wrapmode;
  210. }
  211. set {
  212. _wrapmode = value;
  213. }
  214. }
  215. #endregion
  216. #region Public Methods
  217. public void MultiplyTransform (Matrix matrix) {
  218. BrushMultiplyTransform(matrix, MatrixOrder.Prepend);
  219. }
  220. public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
  221. BrushMultiplyTransform(matrix, order);
  222. }
  223. public void ResetTransform () {
  224. BrushResetTransform();
  225. }
  226. public void RotateTransform (float angle) {
  227. BrushRotateTransform(angle, MatrixOrder.Prepend);
  228. }
  229. public void RotateTransform (float angle, MatrixOrder order) {
  230. BrushRotateTransform(angle, order);
  231. }
  232. public void ScaleTransform (float sx, float sy) {
  233. BrushScaleTransform(sx, sy, MatrixOrder.Prepend);
  234. }
  235. public void ScaleTransform (float sx, float sy, MatrixOrder order) {
  236. BrushScaleTransform(sx, sy, order);
  237. }
  238. public void SetBlendTriangularShape (float focus) {
  239. SetBlendTriangularShape (focus, 1.0F);
  240. }
  241. public void SetBlendTriangularShape (float focus, float scale) {
  242. _x2 = (_x1 + _x2) / 2;
  243. _y2 = (_y1 + _y2) / 2;
  244. _cyclic = true;
  245. _nativeObject = null;
  246. }
  247. public void SetSigmaBellShape (float focus) {
  248. SetSigmaBellShape (focus, 1.0F);
  249. }
  250. [MonoTODO]
  251. public void SetSigmaBellShape (float focus, float scale) {
  252. // FALLBACK: Triangle shape used
  253. SetBlendTriangularShape (focus, scale);
  254. }
  255. public void TranslateTransform (float dx, float dy) {
  256. BrushTranslateTransform (dx, dy);
  257. }
  258. public void TranslateTransform (float dx, float dy, MatrixOrder order) {
  259. BrushTranslateTransform(dx, dy, order);
  260. }
  261. public override object Clone () {
  262. LinearGradientBrush copy = (LinearGradientBrush)InternalClone();
  263. if (copy._nativeObject != null)
  264. copy._nativeObject = null;
  265. if (_interpolationColors != null) {
  266. copy._interpolationColors = new ColorBlend();
  267. if (_interpolationColors.Colors != null)
  268. copy._interpolationColors.Colors = (Color[])_interpolationColors.Colors.Clone();
  269. if (_interpolationColors.Positions != null)
  270. copy._interpolationColors.Positions = (float[])_interpolationColors.Positions.Clone();
  271. }
  272. if (_blend != null) {
  273. copy._blend = new Blend();
  274. if (_blend.Factors != null)
  275. copy._blend.Factors = (float[])_blend.Factors.Clone();
  276. if (_blend.Positions != null)
  277. copy._blend.Positions = (float[])_blend.Positions.Clone();
  278. }
  279. copy.LinearColors = (Color[])LinearColors.Clone();
  280. return copy;
  281. }
  282. #endregion
  283. }
  284. }