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