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