LinearGradientBrush.jvm.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. public Blend Blend {
  146. get {
  147. return _blend;
  148. }
  149. set {
  150. _blend = value;
  151. }
  152. }
  153. // FALLBACK: no functionality implemented for this property
  154. public bool GammaCorrection {
  155. get {
  156. return _gammaCorrection;
  157. }
  158. set {
  159. _gammaCorrection = value;
  160. }
  161. }
  162. // FALLBACK: functionality of two color gradient is implemented
  163. public ColorBlend InterpolationColors {
  164. get {
  165. return _interpolationColors;
  166. }
  167. set {
  168. if (value == null)
  169. throw new ArgumentNullException("ColorBlend");
  170. if ( (value.Colors == null) || (value.Colors.Length == 0) )
  171. throw new ArgumentException("ColorBlend");
  172. _interpolationColors = value;
  173. _color1 = value.Colors[0];
  174. _color2 = value.Colors[value.Colors.Length - 1];
  175. _nativeObject = null;
  176. }
  177. }
  178. public Color [] LinearColors {
  179. get {
  180. Color [] cl = new Color[2];
  181. cl[0] = _color1;
  182. cl[1] = _color2;
  183. return cl;
  184. }
  185. set {
  186. if (value == null)
  187. throw new ArgumentNullException("colors");
  188. _color1 = value[0];
  189. _color2 = value[1];
  190. _nativeObject = null;
  191. }
  192. }
  193. public RectangleF Rectangle {
  194. get {
  195. return _gradientRectangle;
  196. }
  197. }
  198. public Matrix Transform {
  199. get { return BrushTransform; }
  200. set { BrushTransform = value; }
  201. }
  202. // FALLBACK: not functionality implemented for this property
  203. public WrapMode WrapMode {
  204. get {
  205. return _wrapmode;
  206. }
  207. set {
  208. _wrapmode = value;
  209. }
  210. }
  211. #endregion
  212. #region Public Methods
  213. public void MultiplyTransform (Matrix matrix) {
  214. BrushMultiplyTransform(matrix, MatrixOrder.Prepend);
  215. }
  216. public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
  217. BrushMultiplyTransform(matrix, order);
  218. }
  219. public void ResetTransform () {
  220. BrushResetTransform();
  221. }
  222. public void RotateTransform (float angle) {
  223. BrushRotateTransform(angle, MatrixOrder.Prepend);
  224. }
  225. public void RotateTransform (float angle, MatrixOrder order) {
  226. BrushRotateTransform(angle, order);
  227. }
  228. public void ScaleTransform (float sx, float sy) {
  229. BrushScaleTransform(sx, sy, MatrixOrder.Prepend);
  230. }
  231. public void ScaleTransform (float sx, float sy, MatrixOrder order) {
  232. BrushScaleTransform(sx, sy, order);
  233. }
  234. public void SetBlendTriangularShape (float focus) {
  235. SetBlendTriangularShape (focus, 1.0F);
  236. }
  237. public void SetBlendTriangularShape (float focus, float scale) {
  238. _x2 = (_x1 + _x2) / 2;
  239. _y2 = (_y1 + _y2) / 2;
  240. _cyclic = true;
  241. _nativeObject = null;
  242. }
  243. public void SetSigmaBellShape (float focus) {
  244. SetSigmaBellShape (focus, 1.0F);
  245. }
  246. public void SetSigmaBellShape (float focus, float scale) {
  247. // FALLBACK: Triangle shape used
  248. SetBlendTriangularShape (focus, scale);
  249. }
  250. public void TranslateTransform (float dx, float dy) {
  251. BrushTranslateTransform (dx, dy);
  252. }
  253. public void TranslateTransform (float dx, float dy, MatrixOrder order) {
  254. BrushTranslateTransform(dx, dy, order);
  255. }
  256. public override object Clone () {
  257. LinearGradientBrush copy = (LinearGradientBrush)InternalClone();
  258. if (copy._nativeObject != null)
  259. copy._nativeObject = null;
  260. if (_interpolationColors != null) {
  261. copy._interpolationColors = new ColorBlend();
  262. if (_interpolationColors.Colors != null)
  263. copy._interpolationColors.Colors = (Color[])_interpolationColors.Colors.Clone();
  264. if (_interpolationColors.Positions != null)
  265. copy._interpolationColors.Positions = (float[])_interpolationColors.Positions.Clone();
  266. }
  267. if (_blend != null) {
  268. copy._blend = new Blend();
  269. if (_blend.Factors != null)
  270. copy._blend.Factors = (float[])_blend.Factors.Clone();
  271. if (_blend.Positions != null)
  272. copy._blend.Positions = (float[])_blend.Positions.Clone();
  273. }
  274. copy.LinearColors = (Color[])LinearColors.Clone();
  275. return copy;
  276. }
  277. #endregion
  278. }
  279. }