1. /*
  2. * @(#)BevelBorder.java 1.12 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.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 appropriate
  19. * for short term storage or RMI between applications running the same
  20. * version of Swing. A future release of Swing will provide support for
  21. * long term persistence.
  22. *
  23. * @version 1.12 11/29/01
  24. * @author David Kloba
  25. */
  26. public class BevelBorder extends AbstractBorder
  27. {
  28. /** Raised bevel type. */
  29. public static final int RAISED = 0;
  30. /** Lowered bevel type. */
  31. public static final int LOWERED = 1;
  32. protected int bevelType;
  33. protected Color highlightOuter;
  34. protected Color highlightInner;
  35. protected Color shadowInner;
  36. protected Color shadowOuter;
  37. /**
  38. * Creates a bevel border with the specified type and whose
  39. * colors will be derived from the background color of the
  40. * component passed into the paintBorder method.
  41. * @param bevelType the type of bevel for the border
  42. */
  43. public BevelBorder(int bevelType) {
  44. this.bevelType = bevelType;
  45. }
  46. /**
  47. * Creates a bevel border with the specified type, highlight and
  48. * shadow colors.
  49. * @param bevelType the type of bevel for the border
  50. * @param highlight the color to use for the bevel highlight
  51. * @param shadow the color to use for the bevel shadow
  52. */
  53. public BevelBorder(int bevelType, Color highlight, Color shadow) {
  54. this(bevelType, highlight.brighter(), highlight, shadow, shadow.brighter());
  55. }
  56. /**
  57. * Creates a bevel border with the specified type, highlight
  58. * shadow colors.
  59. * @param bevelType the type of bevel for the border
  60. * @param highlightOuter the color to use for the bevel outer highlight
  61. * @param highlightInner the color to use for the bevel inner highlight
  62. * @param shadowOuter the color to use for the bevel outer shadow
  63. * @param shadowInner the color to use for the bevel inner shadow
  64. */
  65. public BevelBorder(int bevelType, Color highlightOuter, Color highlightInner,
  66. Color shadowOuter, Color shadowInner) {
  67. this(bevelType);
  68. this.highlightOuter = highlightOuter;
  69. this.highlightInner = highlightInner;
  70. this.shadowOuter = shadowOuter;
  71. this.shadowInner = shadowInner;
  72. }
  73. /**
  74. * Paints the border for the specified component with the specified
  75. * position and size.
  76. * @param c the component for which this border is being painted
  77. * @param g the paint graphics
  78. * @param x the x position of the painted border
  79. * @param y the y position of the painted border
  80. * @param width the width of the painted border
  81. * @param height the height of the painted border
  82. */
  83. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  84. if (bevelType == RAISED) {
  85. paintRaisedBevel(c, g, x, y, width, height);
  86. } else if (bevelType == LOWERED) {
  87. paintLoweredBevel(c, g, x, y, width, height);
  88. }
  89. }
  90. /**
  91. * Returns the insets of the border.
  92. * @param c the component for which this border insets value applies
  93. */
  94. public Insets getBorderInsets(Component c) {
  95. return new Insets(2, 2, 2, 2);
  96. }
  97. /**
  98. * Reinitialize the insets parameter with this Border's current Insets.
  99. * @param c the component for which this border insets value applies
  100. * @param insets the object to be reinitialized
  101. */
  102. public Insets getBorderInsets(Component c, Insets insets) {
  103. insets.left = insets.top = insets.right = insets.bottom = 2;
  104. return insets;
  105. }
  106. /**
  107. * Returns the outer highlight color of the bevel border.
  108. */
  109. public Color getHighlightOuterColor(Component c) {
  110. return highlightOuter != null? highlightOuter :
  111. c.getBackground().brighter().brighter();
  112. }
  113. /**
  114. * Returns the inner highlight color of the bevel border.
  115. */
  116. public Color getHighlightInnerColor(Component c) {
  117. return highlightInner != null? highlightInner :
  118. c.getBackground().brighter();
  119. }
  120. /**
  121. * Returns the inner shadow color of the bevel border.
  122. */
  123. public Color getShadowInnerColor(Component c) {
  124. return shadowInner != null? shadowInner :
  125. c.getBackground().darker();
  126. }
  127. /**
  128. * Returns the outer shadow color of the bevel border.
  129. */
  130. public Color getShadowOuterColor(Component c) {
  131. return shadowOuter != null? shadowOuter :
  132. c.getBackground().darker().darker();
  133. }
  134. /**
  135. * Returns the type of the bevel border.
  136. */
  137. public int getBevelType() {
  138. return bevelType;
  139. }
  140. /**
  141. * Returns whether or not the border is opaque.
  142. */
  143. public boolean isBorderOpaque() { return true; }
  144. protected void paintRaisedBevel(Component c, Graphics g, int x, int y,
  145. int width, int height) {
  146. Color oldColor = g.getColor();
  147. int h = height;
  148. int w = width;
  149. g.translate(x, y);
  150. g.setColor(getHighlightOuterColor(c));
  151. g.drawLine(0, 0, 0, h-1);
  152. g.drawLine(1, 0, w-1, 0);
  153. g.setColor(getHighlightInnerColor(c));
  154. g.drawLine(1, 1, 1, h-2);
  155. g.drawLine(2, 1, w-2, 1);
  156. g.setColor(getShadowOuterColor(c));
  157. g.drawLine(1, h-1, w-1, h-1);
  158. g.drawLine(w-1, 1, w-1, h-2);
  159. g.setColor(getShadowInnerColor(c));
  160. g.drawLine(2, h-2, w-2, h-2);
  161. g.drawLine(w-2, 2, w-2, h-3);
  162. g.translate(-x, -y);
  163. g.setColor(oldColor);
  164. }
  165. protected void paintLoweredBevel(Component c, Graphics g, int x, int y,
  166. int width, int height) {
  167. Color oldColor = g.getColor();
  168. int h = height;
  169. int w = width;
  170. g.translate(x, y);
  171. g.setColor(getShadowInnerColor(c));
  172. g.drawLine(0, 0, 0, h-1);
  173. g.drawLine(1, 0, w-1, 0);
  174. g.setColor(getShadowOuterColor(c));
  175. g.drawLine(1, 1, 1, h-2);
  176. g.drawLine(2, 1, w-2, 1);
  177. g.setColor(getHighlightOuterColor(c));
  178. g.drawLine(1, h-1, w-1, h-1);
  179. g.drawLine(w-1, 1, w-1, h-2);
  180. g.setColor(getHighlightInnerColor(c));
  181. g.drawLine(2, h-2, w-2, h-2);
  182. g.drawLine(w-2, 2, w-2, h-3);
  183. g.translate(-x, -y);
  184. g.setColor(oldColor);
  185. }
  186. }