1. /*
  2. * @(#)MatteBorder.java 1.18 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.border;
  11. import java.awt.Graphics;
  12. import java.awt.Insets;
  13. import java.awt.Rectangle;
  14. import java.awt.Component;
  15. import java.awt.Color;
  16. import javax.swing.Icon;
  17. /**
  18. * A class which provides a matte-like border of either a solid color
  19. * or a tiled icon.
  20. * <p>
  21. * <strong>Warning:</strong>
  22. * Serialized objects of this class will not be compatible with
  23. * future Swing releases. The current serialization support is appropriate
  24. * for short term storage or RMI between applications running the same
  25. * version of Swing. A future release of Swing will provide support for
  26. * long term persistence.
  27. *
  28. * @version 1.18 02/02/00
  29. * @author Amy Fowler
  30. */
  31. public class MatteBorder extends EmptyBorder
  32. {
  33. protected Color color;
  34. protected Icon tileIcon;
  35. /**
  36. * Creates a matte border with the specified insets and color.
  37. * @param top the top inset of the border
  38. * @param left the left inset of the border
  39. * @param bottom the bottom inset of the border
  40. * @param right the right inset of the border
  41. * @param matteColor the color rendered for the border
  42. */
  43. public MatteBorder(int top, int left, int bottom, int right, Color matteColor) {
  44. super(top, left, bottom, right);
  45. this.color = matteColor;
  46. }
  47. /**
  48. * Creates a matte border with the specified insets and color.
  49. * @param borderInsets the insets of the border
  50. * @param matteColor the color rendered for the border
  51. */
  52. public MatteBorder(Insets borderInsets, Color matteColor) {
  53. super(borderInsets);
  54. this.color = matteColor;
  55. }
  56. /**
  57. * Creates a matte border with the specified insets and tile icon.
  58. * @param top the top inset of the border
  59. * @param left the left inset of the border
  60. * @param bottom the bottom inset of the border
  61. * @param right the right inset of the border
  62. * @param tileIcon the icon to be used for tiling the border
  63. */
  64. public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon) {
  65. super(top, left, bottom, right);
  66. this.tileIcon = tileIcon;
  67. }
  68. /**
  69. * Creates a matte border with the specified insets and tile icon.
  70. * @param borderInsets the insets of the border
  71. * @param tileIcon the icon to be used for tiling the border
  72. */
  73. public MatteBorder(Insets borderInsets, Icon tileIcon) {
  74. super(borderInsets);
  75. this.tileIcon = tileIcon;
  76. }
  77. /**
  78. * Creates a matte border with the specified tile icon. The
  79. * insets will be calculated dynamically based on the size of
  80. * the tile icon, where the top and bottom will be equal to the
  81. * tile icon's height, and the left and right will be equal to
  82. * the tile icon's width.
  83. * @param tileIcon the icon to be used for tiling the border
  84. */
  85. public MatteBorder(Icon tileIcon) {
  86. this(-1,-1,-1,-1, tileIcon);
  87. }
  88. /**
  89. * Paints the matte border.
  90. */
  91. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  92. Insets insets = getBorderInsets(c);
  93. Color oldColor = g.getColor();
  94. g.translate(x, y);
  95. // If the tileIcon failed loading, paint as gray.
  96. if (tileIcon != null) {
  97. color = (tileIcon.getIconWidth() == -1) ? Color.gray : null;
  98. }
  99. if (color != null) {
  100. g.setColor(color);
  101. g.fillRect(0, 0, width - insets.right, insets.top);
  102. g.fillRect(0, insets.top, insets.left, height - insets.top);
  103. g.fillRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
  104. g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom);
  105. } else if (tileIcon != null) {
  106. int tileW = tileIcon.getIconWidth();
  107. int tileH = tileIcon.getIconHeight();
  108. int xpos, ypos, startx, starty;
  109. Graphics cg;
  110. // Paint top matte edge
  111. cg = g.create();
  112. cg.setClip(0, 0, width, insets.top);
  113. for (ypos = 0; insets.top - ypos > 0; ypos += tileH) {
  114. for (xpos = 0; width - xpos > 0; xpos += tileW) {
  115. tileIcon.paintIcon(c, cg, xpos, ypos);
  116. }
  117. }
  118. cg.dispose();
  119. // Paint left matte edge
  120. cg = g.create();
  121. cg.setClip(0, insets.top, insets.left, height - insets.top);
  122. starty = insets.top - (insets.top%tileH);
  123. startx = 0;
  124. for (ypos = starty; height - ypos > 0; ypos += tileH) {
  125. for (xpos = startx; insets.left - xpos > 0; xpos += tileW) {
  126. tileIcon.paintIcon(c, cg, xpos, ypos);
  127. }
  128. }
  129. cg.dispose();
  130. // Paint bottom matte edge
  131. cg = g.create();
  132. cg.setClip(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
  133. starty = (height - insets.bottom) - ((height - insets.bottom)%tileH);
  134. startx = insets.left - (insets.left%tileW);
  135. for (ypos = starty; height - ypos > 0; ypos += tileH) {
  136. for (xpos = startx; width - xpos > 0; xpos += tileW) {
  137. tileIcon.paintIcon(c, cg, xpos, ypos);
  138. }
  139. }
  140. cg.dispose();
  141. // Paint right matte edge
  142. cg = g.create();
  143. cg.setClip(width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
  144. starty = insets.top - (insets.top%tileH);
  145. startx = width - insets.right - ((width - insets.right)%tileW);
  146. for (ypos = starty; height - ypos > 0; ypos += tileH) {
  147. for (xpos = startx; width - xpos > 0; xpos += tileW) {
  148. tileIcon.paintIcon(c, cg, xpos, ypos);
  149. }
  150. }
  151. cg.dispose();
  152. }
  153. g.translate(-x, -y);
  154. g.setColor(oldColor);
  155. }
  156. /**
  157. * Returns the insets of the border.
  158. * @param c the component for which this border insets value applies
  159. */
  160. public Insets getBorderInsets(Component c) {
  161. return getBorderInsets();
  162. }
  163. /**
  164. * Reinitialize the insets parameter with this Border's current Insets.
  165. * @param c the component for which this border insets value applies
  166. * @param insets the object to be reinitialized
  167. */
  168. public Insets getBorderInsets(Component c, Insets insets) {
  169. return computeInsets(insets);
  170. }
  171. /**
  172. * Returns the insets of the border.
  173. */
  174. public Insets getBorderInsets() {
  175. return computeInsets(new Insets(0,0,0,0));
  176. }
  177. /* should be protected once api changes area allowed */
  178. private Insets computeInsets(Insets insets) {
  179. if (tileIcon != null && top == -1 && bottom == -1 &&
  180. left == -1 && right == -1) {
  181. int w = tileIcon.getIconWidth();
  182. int h = tileIcon.getIconHeight();
  183. insets.top = h;
  184. insets.right = w;
  185. insets.bottom = h;
  186. insets.left = w;
  187. } else {
  188. insets.left = left;
  189. insets.top = top;
  190. insets.right = right;
  191. insets.bottom = bottom;
  192. }
  193. return insets;
  194. }
  195. /**
  196. * Returns the color used for tiling the border or null
  197. * if a tile icon is being used.
  198. */
  199. public Color getMatteColor() {
  200. return color;
  201. }
  202. /**
  203. * Returns the icon used for tiling the border or null
  204. * if a solid color is being used.
  205. */
  206. public Icon getTileIcon() {
  207. return tileIcon;
  208. }
  209. /**
  210. * Returns whether or not the border is opaque.
  211. */
  212. public boolean isBorderOpaque() {
  213. // If a tileIcon is set, then it may contain transparent bits
  214. return color != null;
  215. }
  216. }