1. /*
  2. * @(#)BevelBorder.java 1.20 03/12/19
  3. *
  4. * Copyright 2004 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.Color;
  12. import java.awt.Component;
  13. /**
  14. * A class which implements a simple two-line bevel border.
  15. * <p>
  16. * <strong>Warning:</strong>
  17. * Serialized objects of this class will not be compatible with
  18. * future Swing releases. The current serialization support is
  19. * appropriate for short term storage or RMI between applications running
  20. * the same version of Swing. As of 1.4, support for long term storage
  21. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  22. * has been added to the <code>java.beans</code> package.
  23. * Please see {@link java.beans.XMLEncoder}.
  24. *
  25. * @version 1.20 12/19/03
  26. * @author David Kloba
  27. */
  28. public class BevelBorder extends AbstractBorder
  29. {
  30. /** Raised bevel type. */
  31. public static final int RAISED = 0;
  32. /** Lowered bevel type. */
  33. public static final int LOWERED = 1;
  34. protected int bevelType;
  35. protected Color highlightOuter;
  36. protected Color highlightInner;
  37. protected Color shadowInner;
  38. protected Color shadowOuter;
  39. /**
  40. * Creates a bevel border with the specified type and whose
  41. * colors will be derived from the background color of the
  42. * component passed into the paintBorder method.
  43. * @param bevelType the type of bevel for the border
  44. */
  45. public BevelBorder(int bevelType) {
  46. this.bevelType = bevelType;
  47. }
  48. /**
  49. * Creates a bevel border with the specified type, highlight and
  50. * shadow colors.
  51. * @param bevelType the type of bevel for the border
  52. * @param highlight the color to use for the bevel highlight
  53. * @param shadow the color to use for the bevel shadow
  54. */
  55. public BevelBorder(int bevelType, Color highlight, Color shadow) {
  56. this(bevelType, highlight.brighter(), highlight, shadow, shadow.brighter());
  57. }
  58. /**
  59. * Creates a bevel border with the specified type, highlight and
  60. * shadow colors.
  61. * <p>
  62. * Note: The shadow inner and outer colors are
  63. * switched for a lowered bevel border.
  64. *
  65. * @param bevelType the type of bevel for the border
  66. * @param highlightOuterColor the color to use for the bevel outer highlight
  67. * @param highlightInnerColor the color to use for the bevel inner highlight
  68. * @param shadowOuterColor the color to use for the bevel outer shadow
  69. * @param shadowInnerColor the color to use for the bevel inner shadow
  70. */
  71. public BevelBorder(int bevelType, Color highlightOuterColor,
  72. Color highlightInnerColor, Color shadowOuterColor,
  73. Color shadowInnerColor) {
  74. this(bevelType);
  75. this.highlightOuter = highlightOuterColor;
  76. this.highlightInner = highlightInnerColor;
  77. this.shadowOuter = shadowOuterColor;
  78. this.shadowInner = shadowInnerColor;
  79. }
  80. /**
  81. * Paints the border for the specified component with the specified
  82. * position and size.
  83. * @param c the component for which this border is being painted
  84. * @param g the paint graphics
  85. * @param x the x position of the painted border
  86. * @param y the y position of the painted border
  87. * @param width the width of the painted border
  88. * @param height the height of the painted border
  89. */
  90. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  91. if (bevelType == RAISED) {
  92. paintRaisedBevel(c, g, x, y, width, height);
  93. } else if (bevelType == LOWERED) {
  94. paintLoweredBevel(c, g, x, y, width, height);
  95. }
  96. }
  97. /**
  98. * Returns the insets of the border.
  99. * @param c the component for which this border insets value applies
  100. */
  101. public Insets getBorderInsets(Component c) {
  102. return new Insets(2, 2, 2, 2);
  103. }
  104. /**
  105. * Reinitialize the insets parameter with this Border's current Insets.
  106. * @param c the component for which this border insets value applies
  107. * @param insets the object to be reinitialized
  108. */
  109. public Insets getBorderInsets(Component c, Insets insets) {
  110. insets.left = insets.top = insets.right = insets.bottom = 2;
  111. return insets;
  112. }
  113. /**
  114. * Returns the outer highlight color of the bevel border
  115. * when rendered on the specified component. If no highlight
  116. * color was specified at instantiation, the highlight color
  117. * is derived from the specified component's background color.
  118. * @param c the component for which the highlight may be derived
  119. */
  120. public Color getHighlightOuterColor(Component c) {
  121. Color highlight = getHighlightOuterColor();
  122. return highlight != null? highlight :
  123. c.getBackground().brighter().brighter();
  124. }
  125. /**
  126. * Returns the inner highlight color of the bevel border
  127. * when rendered on the specified component. If no highlight
  128. * color was specified at instantiation, the highlight color
  129. * is derived from the specified component's background color.
  130. * @param c the component for which the highlight may be derived
  131. */
  132. public Color getHighlightInnerColor(Component c) {
  133. Color highlight = getHighlightInnerColor();
  134. return highlight != null? highlight :
  135. c.getBackground().brighter();
  136. }
  137. /**
  138. * Returns the inner shadow color of the bevel border
  139. * when rendered on the specified component. If no shadow
  140. * color was specified at instantiation, the shadow color
  141. * is derived from the specified component's background color.
  142. * @param c the component for which the shadow may be derived
  143. */
  144. public Color getShadowInnerColor(Component c) {
  145. Color shadow = getShadowInnerColor();
  146. return shadow != null? shadow :
  147. c.getBackground().darker();
  148. }
  149. /**
  150. * Returns the outer shadow color of the bevel border
  151. * when rendered on the specified component. If no shadow
  152. * color was specified at instantiation, the shadow color
  153. * is derived from the specified component's background color.
  154. * @param c the component for which the shadow may be derived
  155. */
  156. public Color getShadowOuterColor(Component c) {
  157. Color shadow = getShadowOuterColor();
  158. return shadow != null? shadow :
  159. c.getBackground().darker().darker();
  160. }
  161. /**
  162. * Returns the outer highlight color of the bevel border.
  163. * Will return null if no highlight color was specified
  164. * at instantiation.
  165. */
  166. public Color getHighlightOuterColor() {
  167. return highlightOuter;
  168. }
  169. /**
  170. * Returns the inner highlight color of the bevel border.
  171. * Will return null if no highlight color was specified
  172. * at instantiation.
  173. */
  174. public Color getHighlightInnerColor() {
  175. return highlightInner;
  176. }
  177. /**
  178. * Returns the inner shadow color of the bevel border.
  179. * Will return null if no shadow color was specified
  180. * at instantiation.
  181. */
  182. public Color getShadowInnerColor() {
  183. return shadowInner;
  184. }
  185. /**
  186. * Returns the outer shadow color of the bevel border.
  187. * Will return null if no shadow color was specified
  188. * at instantiation.
  189. */
  190. public Color getShadowOuterColor() {
  191. return shadowOuter;
  192. }
  193. /**
  194. * Returns the type of the bevel border.
  195. */
  196. public int getBevelType() {
  197. return bevelType;
  198. }
  199. /**
  200. * Returns whether or not the border is opaque.
  201. */
  202. public boolean isBorderOpaque() { return true; }
  203. protected void paintRaisedBevel(Component c, Graphics g, int x, int y,
  204. int width, int height) {
  205. Color oldColor = g.getColor();
  206. int h = height;
  207. int w = width;
  208. g.translate(x, y);
  209. g.setColor(getHighlightOuterColor(c));
  210. g.drawLine(0, 0, 0, h-2);
  211. g.drawLine(1, 0, w-2, 0);
  212. g.setColor(getHighlightInnerColor(c));
  213. g.drawLine(1, 1, 1, h-3);
  214. g.drawLine(2, 1, w-3, 1);
  215. g.setColor(getShadowOuterColor(c));
  216. g.drawLine(0, h-1, w-1, h-1);
  217. g.drawLine(w-1, 0, w-1, h-2);
  218. g.setColor(getShadowInnerColor(c));
  219. g.drawLine(1, h-2, w-2, h-2);
  220. g.drawLine(w-2, 1, w-2, h-3);
  221. g.translate(-x, -y);
  222. g.setColor(oldColor);
  223. }
  224. protected void paintLoweredBevel(Component c, Graphics g, int x, int y,
  225. int width, int height) {
  226. Color oldColor = g.getColor();
  227. int h = height;
  228. int w = width;
  229. g.translate(x, y);
  230. g.setColor(getShadowInnerColor(c));
  231. g.drawLine(0, 0, 0, h-1);
  232. g.drawLine(1, 0, w-1, 0);
  233. g.setColor(getShadowOuterColor(c));
  234. g.drawLine(1, 1, 1, h-2);
  235. g.drawLine(2, 1, w-2, 1);
  236. g.setColor(getHighlightOuterColor(c));
  237. g.drawLine(1, h-1, w-1, h-1);
  238. g.drawLine(w-1, 1, w-1, h-2);
  239. g.setColor(getHighlightInnerColor(c));
  240. g.drawLine(2, h-2, w-2, h-2);
  241. g.drawLine(w-2, 2, w-2, h-3);
  242. g.translate(-x, -y);
  243. g.setColor(oldColor);
  244. }
  245. }