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