1. /*
  2. * @(#)EtchedBorder.java 1.18 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 etched border which can
  15. * either be etched-in or etched-out. If no highlight/shadow
  16. * colors are initialized when the border is created, then
  17. * these colors will be dynamically derived from the background
  18. * color of the component argument passed into the paintBorder()
  19. * method.
  20. * <p>
  21. * <strong>Warning:</strong>
  22. * Serialized objects of this class will not be compatible with
  23. * future Swing releases. The current serialization support is
  24. * appropriate for short term storage or RMI between applications running
  25. * the same version of Swing. As of 1.4, support for long term storage
  26. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  27. * has been added to the <code>java.beans</code> package.
  28. * Please see {@link java.beans.XMLEncoder}.
  29. *
  30. * @version 1.18 12/19/03
  31. * @author David Kloba
  32. * @author Amy Fowler
  33. */
  34. public class EtchedBorder extends AbstractBorder
  35. {
  36. /** Raised etched type. */
  37. public static final int RAISED = 0;
  38. /** Lowered etched type. */
  39. public static final int LOWERED = 1;
  40. protected int etchType;
  41. protected Color highlight;
  42. protected Color shadow;
  43. /**
  44. * Creates a lowered etched border whose colors will be derived
  45. * from the background color of the component passed into
  46. * the paintBorder method.
  47. */
  48. public EtchedBorder() {
  49. this(LOWERED);
  50. }
  51. /**
  52. * Creates an etched border with the specified etch-type
  53. * whose colors will be derived
  54. * from the background color of the component passed into
  55. * the paintBorder method.
  56. * @param etchType the type of etch to be drawn by the border
  57. */
  58. public EtchedBorder(int etchType) {
  59. this(etchType, null, null);
  60. }
  61. /**
  62. * Creates a lowered etched border with the specified highlight and
  63. * shadow colors.
  64. * @param highlight the color to use for the etched highlight
  65. * @param shadow the color to use for the etched shadow
  66. */
  67. public EtchedBorder(Color highlight, Color shadow) {
  68. this(LOWERED, highlight, shadow);
  69. }
  70. /**
  71. * Creates an etched border with the specified etch-type,
  72. * highlight and shadow colors.
  73. * @param etchType the type of etch to be drawn by the border
  74. * @param highlight the color to use for the etched highlight
  75. * @param shadow the color to use for the etched shadow
  76. */
  77. public EtchedBorder(int etchType, Color highlight, Color shadow) {
  78. this.etchType = etchType;
  79. this.highlight = highlight;
  80. this.shadow = shadow;
  81. }
  82. /**
  83. * Paints the border for the specified component with the
  84. * specified position and size.
  85. * @param c the component for which this border is being painted
  86. * @param g the paint graphics
  87. * @param x the x position of the painted border
  88. * @param y the y position of the painted border
  89. * @param width the width of the painted border
  90. * @param height the height of the painted border
  91. */
  92. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  93. int w = width;
  94. int h = height;
  95. g.translate(x, y);
  96. g.setColor(etchType == LOWERED? getShadowColor(c) : getHighlightColor(c));
  97. g.drawRect(0, 0, w-2, h-2);
  98. g.setColor(etchType == LOWERED? getHighlightColor(c) : getShadowColor(c));
  99. g.drawLine(1, h-3, 1, 1);
  100. g.drawLine(1, 1, w-3, 1);
  101. g.drawLine(0, h-1, w-1, h-1);
  102. g.drawLine(w-1, h-1, w-1, 0);
  103. g.translate(-x, -y);
  104. }
  105. /**
  106. * Returns the insets of the border.
  107. * @param c the component for which this border insets value applies
  108. */
  109. public Insets getBorderInsets(Component c) {
  110. return new Insets(2, 2, 2, 2);
  111. }
  112. /**
  113. * Reinitialize the insets parameter with this Border's current Insets.
  114. * @param c the component for which this border insets value applies
  115. * @param insets the object to be reinitialized
  116. */
  117. public Insets getBorderInsets(Component c, Insets insets) {
  118. insets.left = insets.top = insets.right = insets.bottom = 2;
  119. return insets;
  120. }
  121. /**
  122. * Returns whether or not the border is opaque.
  123. */
  124. public boolean isBorderOpaque() { return true; }
  125. /**
  126. * Returns which etch-type is set on the etched border.
  127. */
  128. public int getEtchType() {
  129. return etchType;
  130. }
  131. /**
  132. * Returns the highlight color of the etched border
  133. * when rendered on the specified component. If no highlight
  134. * color was specified at instantiation, the highlight color
  135. * is derived from the specified component's background color.
  136. * @param c the component for which the highlight may be derived
  137. */
  138. public Color getHighlightColor(Component c) {
  139. return highlight != null? highlight :
  140. c.getBackground().brighter();
  141. }
  142. /**
  143. * Returns the highlight color of the etched border.
  144. * Will return null if no highlight color was specified
  145. * at instantiation.
  146. */
  147. public Color getHighlightColor() {
  148. return highlight;
  149. }
  150. /**
  151. * Returns the shadow color of the etched border
  152. * when rendered on the specified component. If no shadow
  153. * color was specified at instantiation, the shadow color
  154. * is derived from the specified component's background color.
  155. * @param c the component for which the shadow may be derived
  156. */
  157. public Color getShadowColor(Component c) {
  158. return shadow != null? shadow : c.getBackground().darker();
  159. }
  160. /**
  161. * Returns the shadow color of the etched border.
  162. * Will return null if no shadow color was specified
  163. * at instantiation.
  164. */
  165. public Color getShadowColor() {
  166. return shadow;
  167. }
  168. }