1. /*
  2. * @(#)BevelBorder.java 1.18 03/01/23
  3. *
  4. * Copyright 2003 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 2 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.18 01/23/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
  60. * shadow colors.
  61. * @param bevelType the type of bevel for the border
  62. * @param highlightOuterColor the color to use for the bevel outer highlight
  63. * @param highlightInnerColor the color to use for the bevel inner highlight
  64. * @param shadowOuterColor the color to use for the bevel outer shadow
  65. * @param shadowInnerColor the color to use for the bevel inner shadow
  66. */
  67. public BevelBorder(int bevelType, Color highlightOuterColor,
  68. Color highlightInnerColor, Color shadowOuterColor,
  69. Color shadowInnerColor) {
  70. this(bevelType);
  71. this.highlightOuter = highlightOuterColor;
  72. this.highlightInner = highlightInnerColor;
  73. this.shadowOuter = shadowOuterColor;
  74. this.shadowInner = shadowInnerColor;
  75. }
  76. /**
  77. * Paints the border for the specified component with the specified
  78. * position and size.
  79. * @param c the component for which this border is being painted
  80. * @param g the paint graphics
  81. * @param x the x position of the painted border
  82. * @param y the y position of the painted border
  83. * @param width the width of the painted border
  84. * @param height the height of the painted border
  85. */
  86. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  87. if (bevelType == RAISED) {
  88. paintRaisedBevel(c, g, x, y, width, height);
  89. } else if (bevelType == LOWERED) {
  90. paintLoweredBevel(c, g, x, y, width, height);
  91. }
  92. }
  93. /**
  94. * Returns the insets of the border.
  95. * @param c the component for which this border insets value applies
  96. */
  97. public Insets getBorderInsets(Component c) {
  98. return new Insets(2, 2, 2, 2);
  99. }
  100. /**
  101. * Reinitialize the insets parameter with this Border's current Insets.
  102. * @param c the component for which this border insets value applies
  103. * @param insets the object to be reinitialized
  104. */
  105. public Insets getBorderInsets(Component c, Insets insets) {
  106. insets.left = insets.top = insets.right = insets.bottom = 2;
  107. return insets;
  108. }
  109. /**
  110. * Returns the outer highlight color of the bevel border
  111. * when rendered on the specified component. If no highlight
  112. * color was specified at instantiation, the highlight color
  113. * is derived from the specified component's background color.
  114. * @param c the component for which the highlight may be derived
  115. */
  116. public Color getHighlightOuterColor(Component c) {
  117. Color highlight = getHighlightOuterColor();
  118. return highlight != null? highlight :
  119. c.getBackground().brighter().brighter();
  120. }
  121. /**
  122. * Returns the inner highlight color of the bevel border
  123. * when rendered on the specified component. If no highlight
  124. * color was specified at instantiation, the highlight color
  125. * is derived from the specified component's background color.
  126. * @param c the component for which the highlight may be derived
  127. */
  128. public Color getHighlightInnerColor(Component c) {
  129. Color highlight = getHighlightInnerColor();
  130. return highlight != null? highlight :
  131. c.getBackground().brighter();
  132. }
  133. /**
  134. * Returns the inner shadow color of the bevel border
  135. * when rendered on the specified component. If no shadow
  136. * color was specified at instantiation, the shadow color
  137. * is derived from the specified component's background color.
  138. * @param c the component for which the shadow may be derived
  139. */
  140. public Color getShadowInnerColor(Component c) {
  141. Color shadow = getShadowInnerColor();
  142. return shadow != null? shadow :
  143. c.getBackground().darker();
  144. }
  145. /**
  146. * Returns the outer shadow color of the bevel border
  147. * when rendered on the specified component. If no shadow
  148. * color was specified at instantiation, the shadow color
  149. * is derived from the specified component's background color.
  150. * @param c the component for which the shadow may be derived
  151. */
  152. public Color getShadowOuterColor(Component c) {
  153. Color shadow = getShadowOuterColor();
  154. return shadow != null? shadow :
  155. c.getBackground().darker().darker();
  156. }
  157. /**
  158. * Returns the outer highlight color of the bevel border.
  159. * Will return null if no highlight color was specified
  160. * at instantiation.
  161. */
  162. public Color getHighlightOuterColor() {
  163. return highlightOuter;
  164. }
  165. /**
  166. * Returns the inner highlight color of the bevel border.
  167. * Will return null if no highlight color was specified
  168. * at instantiation.
  169. */
  170. public Color getHighlightInnerColor() {
  171. return highlightInner;
  172. }
  173. /**
  174. * Returns the inner shadow color of the bevel border.
  175. * Will return null if no shadow color was specified
  176. * at instantiation.
  177. */
  178. public Color getShadowInnerColor() {
  179. return shadowInner;
  180. }
  181. /**
  182. * Returns the outer shadow color of the bevel border.
  183. * Will return null if no shadow color was specified
  184. * at instantiation.
  185. */
  186. public Color getShadowOuterColor() {
  187. return shadowOuter;
  188. }
  189. /**
  190. * Returns the type of the bevel border.
  191. */
  192. public int getBevelType() {
  193. return bevelType;
  194. }
  195. /**
  196. * Returns whether or not the border is opaque.
  197. */
  198. public boolean isBorderOpaque() { return true; }
  199. protected void paintRaisedBevel(Component c, Graphics g, int x, int y,
  200. int width, int height) {
  201. Color oldColor = g.getColor();
  202. int h = height;
  203. int w = width;
  204. g.translate(x, y);
  205. g.setColor(getHighlightOuterColor(c));
  206. g.drawLine(0, 0, 0, h-2);
  207. g.drawLine(1, 0, w-2, 0);
  208. g.setColor(getHighlightInnerColor(c));
  209. g.drawLine(1, 1, 1, h-3);
  210. g.drawLine(2, 1, w-3, 1);
  211. g.setColor(getShadowOuterColor(c));
  212. g.drawLine(0, h-1, w-1, h-1);
  213. g.drawLine(w-1, 0, w-1, h-2);
  214. g.setColor(getShadowInnerColor(c));
  215. g.drawLine(1, h-2, w-2, h-2);
  216. g.drawLine(w-2, 1, w-2, h-3);
  217. g.translate(-x, -y);
  218. g.setColor(oldColor);
  219. }
  220. protected void paintLoweredBevel(Component c, Graphics g, int x, int y,
  221. int width, int height) {
  222. Color oldColor = g.getColor();
  223. int h = height;
  224. int w = width;
  225. g.translate(x, y);
  226. g.setColor(getShadowInnerColor(c));
  227. g.drawLine(0, 0, 0, h-1);
  228. g.drawLine(1, 0, w-1, 0);
  229. g.setColor(getShadowOuterColor(c));
  230. g.drawLine(1, 1, 1, h-2);
  231. g.drawLine(2, 1, w-2, 1);
  232. g.setColor(getHighlightOuterColor(c));
  233. g.drawLine(1, h-1, w-1, h-1);
  234. g.drawLine(w-1, 1, w-1, h-2);
  235. g.setColor(getHighlightInnerColor(c));
  236. g.drawLine(2, h-2, w-2, h-2);
  237. g.drawLine(w-2, 2, w-2, h-3);
  238. g.translate(-x, -y);
  239. g.setColor(oldColor);
  240. }
  241. }