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