PathGradientBrush.jvm.cs 6.4 KB

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