TextureBrush.jvm.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // System.Drawing.TextureBrush.cs
  3. //
  4. // Author:
  5. // Dennis Hayes ([email protected])
  6. // Ravindra ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc
  9. // (C) 2004 Novell, Inc.
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Drawing.Drawing2D;
  35. using System.Drawing.Imaging;
  36. using awt = java.awt;
  37. using geom = java.awt.geom;
  38. using image = java.awt.image;
  39. namespace System.Drawing {
  40. /// <summary>
  41. /// Summary description for TextureBrush.
  42. /// </summary>
  43. public sealed class TextureBrush : Brush {
  44. readonly awt.TexturePaint _nativeObject;
  45. RectangleF _sourceRectangle;
  46. Image _texture = null;
  47. WrapMode _wrapMode;
  48. protected override java.awt.Paint NativeObject {
  49. get {
  50. return _nativeObject;
  51. }
  52. }
  53. #region ctors
  54. public TextureBrush (Image image) : this (image, WrapMode.Tile) {
  55. }
  56. public TextureBrush (Image image, WrapMode wrapMode) :
  57. this( image, wrapMode, new RectangleF(0, 0, image.Width, image.Height )){
  58. }
  59. public TextureBrush (Image image, Rectangle dstRect) :
  60. this( image, WrapMode.Tile, dstRect ) {
  61. }
  62. public TextureBrush (Image image, RectangleF dstRect) :
  63. this( image, WrapMode.Tile, dstRect ) {
  64. }
  65. [MonoTODO]
  66. public TextureBrush (Image image, Rectangle dstRect, ImageAttributes imageAttr) : this( image, dstRect ) {
  67. // TBD: Implement ImageAttributes
  68. }
  69. [MonoTODO]
  70. public TextureBrush (Image image, RectangleF dstRect, ImageAttributes imageAttr) : this( image, dstRect ) {
  71. // TBD: Implement ImageAttributes
  72. }
  73. public TextureBrush (Image image, WrapMode wrapMode, Rectangle dstRect) :
  74. this( image, wrapMode, new RectangleF(dstRect.X, dstRect.Y, dstRect.Width, dstRect.Height )){
  75. }
  76. [MonoTODO]
  77. public TextureBrush (Image image, WrapMode wrapMode, RectangleF dstRect) {
  78. // TBD: check if not metafile
  79. _sourceRectangle = dstRect;
  80. _texture = (Image)((Bitmap)image).Clone(dstRect, image.PixelFormat);
  81. _wrapMode = wrapMode;
  82. if (wrapMode != Drawing2D.WrapMode.Tile)
  83. image = CreateWrappedImage(_texture, wrapMode);
  84. else
  85. image = _texture;
  86. _nativeObject = new awt.TexturePaint((image.BufferedImage)image.NativeObject.CurrentImage.NativeImage,
  87. new geom.Rectangle2D.Float(0, 0, image.Width, image.Height));
  88. }
  89. #endregion
  90. #region CreateWrappedImage
  91. private Image CreateWrappedImage(Image image, WrapMode wrapMode) {
  92. Image b = null;
  93. Graphics g = null;
  94. switch (wrapMode) {
  95. case Drawing2D.WrapMode.TileFlipX :
  96. b = new Bitmap(image.Width * 2, image.Height);
  97. g = Graphics.FromImage( b );
  98. g.DrawImage(image, new Matrix());
  99. g.DrawImage(image, new Matrix(-1, 0, 0, 1, image.Width * 2 - 1, 0));
  100. break;
  101. case Drawing2D.WrapMode.TileFlipY :
  102. b = new Bitmap(image.Width, image.Height * 2);
  103. g = Graphics.FromImage( b );
  104. g.DrawImage(image, new Matrix());
  105. g.DrawImage(image, new Matrix(1, 0, 0, -1, 0, image.Height * 2 - 1));
  106. break;
  107. case Drawing2D.WrapMode.TileFlipXY :
  108. b = new Bitmap(image.Width * 2, image.Height * 2);
  109. g = Graphics.FromImage( b );
  110. g.DrawImage(image, new Matrix());
  111. g.DrawImage(image, new Matrix(-1, 0, 0, 1, image.Width * 2 - 1, 0));
  112. g.DrawImage(image, new Matrix(1, 0, 0, -1, 0, image.Height * 2 - 1));
  113. g.DrawImage(image, new Matrix(-1, 0, 0, -1, image.Width * 2 - 1, image.Height * 2 - 1));
  114. break;
  115. case Drawing2D.WrapMode.Clamp :
  116. // TBD: Implement WrapMode.Clamp
  117. return image;
  118. default :
  119. b = image;
  120. break;
  121. }
  122. return b;
  123. }
  124. #endregion
  125. #region properties
  126. public Image Image {
  127. get {
  128. return (Image)_texture.Clone();
  129. }
  130. }
  131. public Matrix Transform {
  132. get {
  133. return BrushTransform;
  134. }
  135. set {
  136. BrushTransform = value;
  137. }
  138. }
  139. [MonoTODO]
  140. public WrapMode WrapMode {
  141. get {
  142. return _wrapMode;
  143. }
  144. set {
  145. _wrapMode = value;
  146. }
  147. }
  148. #endregion
  149. #region public methods
  150. public override object Clone () {
  151. TextureBrush copy = (TextureBrush)InternalClone();
  152. if (_texture != null)
  153. copy._texture = (Image)_texture.Clone();
  154. return copy;
  155. }
  156. public void MultiplyTransform (Matrix matrix) {
  157. base.BrushMultiplyTransform( matrix );
  158. }
  159. public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
  160. base.BrushMultiplyTransform( matrix, order );
  161. }
  162. public void ResetTransform () {
  163. base.BrushResetTransform();
  164. }
  165. public void RotateTransform (float angle) {
  166. base.BrushRotateTransform( angle );
  167. }
  168. public void RotateTransform (float angle, MatrixOrder order) {
  169. base.BrushRotateTransform( angle, order );
  170. }
  171. public void ScaleTransform (float sx, float sy) {
  172. base.BrushScaleTransform( sx, sy );
  173. }
  174. public void ScaleTransform (float sx, float sy, MatrixOrder order) {
  175. base.BrushScaleTransform( sx, sy, order );
  176. }
  177. public void TranslateTransform (float dx, float dy) {
  178. base.BrushTranslateTransform( dx, dy );
  179. }
  180. public void TranslateTransform (float dx, float dy, MatrixOrder order) {
  181. base.BrushTranslateTransform( dx, dy, order );
  182. }
  183. #endregion
  184. }
  185. }