PathGradientBrush.jvm.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using awt = java.awt;
  5. namespace System.Drawing.Drawing2D {
  6. /// <summary>
  7. /// Summary description for PathGradientBrush.
  8. /// </summary>
  9. [MonoTODO]
  10. public sealed class PathGradientBrush : Brush {
  11. Brush _nativeObject;
  12. Blend _blend;
  13. Color _centerColor;
  14. PointF _center;
  15. PointF _focus;
  16. RectangleF _rectangle;
  17. Color [] _surroundColors;
  18. ColorBlend _interpolationColors;
  19. WrapMode _wrapMode;
  20. GraphicsPath _texturePath;
  21. bool _triangularShape = false;
  22. protected override java.awt.Paint NativeObject {
  23. get {
  24. return _nativeObject;
  25. }
  26. }
  27. #region initialize
  28. void Initialize(GraphicsPath path, WrapMode wrapMode, bool initColors, bool calcCenter) {
  29. _texturePath = path;
  30. _wrapMode = wrapMode;
  31. _rectangle = path.GetBounds();
  32. if (initColors) {
  33. _centerColor = Color.Black;
  34. _surroundColors = new Color []{ Color.White };
  35. }
  36. Bitmap texture = new Bitmap( (int)_rectangle.Width, (int)_rectangle.Height );
  37. Graphics g = Graphics.FromImage( texture );
  38. PointF [] pathPoints = path.PathPoints;
  39. if (calcCenter) {
  40. for (int i=0; i < pathPoints.Length; i++) {
  41. _center.X += pathPoints[i].X;
  42. _center.Y += pathPoints[i].Y;
  43. }
  44. _center.X /= pathPoints.Length;
  45. _center.Y /= pathPoints.Length;
  46. }
  47. int outerColor = 0;
  48. DrawSector( g, CenterPoint, pathPoints[pathPoints.Length-1], pathPoints[0], CenterColor, SurroundColors[outerColor] );
  49. for(int i=0; i < pathPoints.Length - 1; i++) {
  50. if (outerColor < SurroundColors.Length - 1)
  51. outerColor++;
  52. DrawSector( g, CenterPoint, pathPoints[i], pathPoints[i+1], CenterColor, SurroundColors[outerColor] );
  53. }
  54. _nativeObject = new TextureBrush( texture );
  55. }
  56. private void DrawSector(Graphics g, PointF center, PointF p1, PointF p2, Color innerColor, Color outerColor) {
  57. GraphicsPath pt = new GraphicsPath();
  58. pt.AddLine(p1, center);
  59. pt.AddLine(center, p2);
  60. LinearGradientBrush lgb = new LinearGradientBrush( GetVertical(center, p1, p2) , center, outerColor, innerColor );
  61. if (_triangularShape)
  62. lgb.SetBlendTriangularShape(0.5f);
  63. g.FillPath( lgb, pt );
  64. }
  65. private PointF GetVertical(PointF c, PointF p1, PointF p2) {
  66. if (p1.X == p2.X)
  67. return new PointF(p1.X, c.Y);
  68. if (p1.Y == p2.Y)
  69. return new PointF(c.X, p2.Y);
  70. float a = (float)(p2.Y - p1.Y) / (p2.X - p1.X);
  71. float av = - 1 / a;
  72. float b1 = p1.Y - a * p1.X;
  73. float b2 = c.Y - av * c.X;
  74. float ox = (b1 - b2) / (av - a);
  75. float oy = av * ox + b2;
  76. return new PointF(ox, oy);
  77. }
  78. #endregion
  79. #region ctors
  80. public PathGradientBrush (GraphicsPath path) {
  81. Initialize( path, WrapMode.Clamp, true, true );
  82. }
  83. public PathGradientBrush (Point [] points) : this (points, WrapMode.Clamp) {
  84. }
  85. public PathGradientBrush (PointF [] points) : this (points, WrapMode.Clamp) {
  86. }
  87. public PathGradientBrush (Point [] points, WrapMode wrapMode) {
  88. GraphicsPath path = new GraphicsPath();
  89. path.AddLines( points );
  90. Initialize( path, wrapMode, true, true );
  91. }
  92. public PathGradientBrush (PointF [] points, WrapMode wrapMode) {
  93. GraphicsPath path = new GraphicsPath();
  94. path.AddLines( points );
  95. Initialize( path, wrapMode, true, true );
  96. }
  97. #endregion
  98. #region Properties
  99. [MonoTODO]
  100. public Blend Blend {
  101. get {
  102. return _blend;
  103. }
  104. set {
  105. _blend = value;
  106. }
  107. }
  108. public Color CenterColor {
  109. get {
  110. return _centerColor;
  111. }
  112. set {
  113. _centerColor = value;
  114. Initialize(_texturePath, _wrapMode, false, false );
  115. }
  116. }
  117. public PointF CenterPoint {
  118. get {
  119. return _center;
  120. }
  121. set {
  122. _center = value;
  123. Initialize(_texturePath, _wrapMode, false, false );
  124. }
  125. }
  126. public PointF FocusScales {
  127. get {
  128. return _focus;
  129. }
  130. set {
  131. _focus = value;
  132. }
  133. }
  134. public ColorBlend InterpolationColors {
  135. get {
  136. return _interpolationColors;
  137. }
  138. set {
  139. _interpolationColors = value;
  140. }
  141. }
  142. public RectangleF Rectangle {
  143. get {
  144. return _rectangle;
  145. }
  146. }
  147. public Color [] SurroundColors {
  148. get {
  149. return _surroundColors;
  150. }
  151. set {
  152. _surroundColors = value;
  153. Initialize(_texturePath, _wrapMode, false, false );
  154. }
  155. }
  156. public Matrix Transform {
  157. get {
  158. return BrushTransform;
  159. }
  160. set {
  161. BrushTransform = value;
  162. }
  163. }
  164. public WrapMode WrapMode {
  165. get {
  166. return _wrapMode;
  167. }
  168. set {
  169. _wrapMode = value;
  170. }
  171. }
  172. #endregion
  173. #region Methods
  174. public void MultiplyTransform (Matrix matrix) {
  175. base.BrushMultiplyTransform( matrix );
  176. }
  177. public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
  178. base.BrushMultiplyTransform( matrix, order );
  179. }
  180. public void ResetTransform () {
  181. base.BrushResetTransform();
  182. }
  183. public void RotateTransform (float angle) {
  184. base.BrushRotateTransform( angle );
  185. }
  186. public void RotateTransform (float angle, MatrixOrder order) {
  187. base.BrushRotateTransform( angle, order );
  188. }
  189. public void ScaleTransform (float sx, float sy) {
  190. base.BrushScaleTransform( sx, sy );
  191. }
  192. public void ScaleTransform (float sx, float sy, MatrixOrder order) {
  193. base.BrushScaleTransform( sx, sy, order );
  194. }
  195. public void SetBlendTriangularShape (float focus) {
  196. SetBlendTriangularShape (focus, 1.0F);
  197. }
  198. public void SetBlendTriangularShape (float focus, float scale) {
  199. _triangularShape = true;
  200. Initialize( _texturePath, _wrapMode, false, false );
  201. }
  202. public void SetSigmaBellShape (float focus) {
  203. SetSigmaBellShape (focus, 1.0F);
  204. }
  205. [MonoTODO]
  206. public void SetSigmaBellShape (float focus, float scale) {
  207. // FALLBACK: Triangle shape used
  208. SetBlendTriangularShape (focus, scale);
  209. }
  210. public void TranslateTransform (float dx, float dy) {
  211. base.BrushTranslateTransform( dx, dy );
  212. }
  213. public void TranslateTransform (float dx, float dy, MatrixOrder order) {
  214. base.BrushTranslateTransform( dx, dy, order );
  215. }
  216. public override object Clone () {
  217. PathGradientBrush copy = (PathGradientBrush)InternalClone();
  218. if (copy._nativeObject != null)
  219. copy._nativeObject = (Brush)copy._nativeObject.Clone();
  220. if (copy._surroundColors != null)
  221. copy._surroundColors = (Color[])copy._surroundColors.Clone();
  222. if (copy._texturePath != null)
  223. copy._texturePath = (GraphicsPath)copy._texturePath.Clone();
  224. //TBD: clone _blend, _interpolationColors
  225. //copy._blend = copy._blend
  226. return copy;
  227. }
  228. #endregion
  229. }
  230. }