1. /*
  2. * @(#)LineBorder.java 1.22 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 line border of arbitrary thickness
  15. * and of a single color.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is
  20. * appropriate for short term storage or RMI between applications running
  21. * the same version of Swing. As of 1.4, support for long term storage
  22. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  23. * has been added to the <code>java.beans</code> package.
  24. * Please see {@link java.beans.XMLEncoder}.
  25. *
  26. * @version 1.22 01/23/03
  27. * @author David Kloba
  28. */
  29. public class LineBorder extends AbstractBorder
  30. {
  31. private static Border blackLine;
  32. private static Border grayLine;
  33. protected int thickness;
  34. protected Color lineColor;
  35. protected boolean roundedCorners;
  36. /** Convenience method for getting the Color.black LineBorder of thickness 1.
  37. */
  38. public static Border createBlackLineBorder() {
  39. if (blackLine == null) {
  40. blackLine = new LineBorder(Color.black, 1);
  41. }
  42. return blackLine;
  43. }
  44. /** Convenience method for getting the Color.gray LineBorder of thickness 1.
  45. */
  46. public static Border createGrayLineBorder() {
  47. if (grayLine == null) {
  48. grayLine = new LineBorder(Color.gray, 1);
  49. }
  50. return grayLine;
  51. }
  52. /**
  53. * Creates a line border with the specified color and a
  54. * thickness = 1.
  55. * @param color the color for the border
  56. */
  57. public LineBorder(Color color) {
  58. this(color, 1, false);
  59. }
  60. /**
  61. * Creates a line border with the specified color and thickness.
  62. * @param color the color of the border
  63. * @param thickness the thickness of the border
  64. */
  65. public LineBorder(Color color, int thickness) {
  66. this(color, thickness, false);
  67. }
  68. /**
  69. * Creates a line border with the specified color, thickness,
  70. * and corner shape.
  71. * @param color the color of the border
  72. * @param thickness the thickness of the border
  73. * @param roundedCorners whether or not border corners should be round
  74. * @since 1.3
  75. */
  76. public LineBorder(Color color, int thickness, boolean roundedCorners) {
  77. lineColor = color;
  78. this.thickness = thickness;
  79. this.roundedCorners = roundedCorners;
  80. }
  81. /**
  82. * Paints the border for the specified component with the
  83. * specified position and size.
  84. * @param c the component for which this border is being painted
  85. * @param g the paint graphics
  86. * @param x the x position of the painted border
  87. * @param y the y position of the painted border
  88. * @param width the width of the painted border
  89. * @param height the height of the painted border
  90. */
  91. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  92. Color oldColor = g.getColor();
  93. int i;
  94. /// PENDING(klobad) How/should do we support Roundtangles?
  95. g.setColor(lineColor);
  96. for(i = 0; i < thickness; i++) {
  97. if(!roundedCorners)
  98. g.drawRect(x+i, y+i, width-i-i-1, height-i-i-1);
  99. else
  100. g.drawRoundRect(x+i, y+i, width-i-i-1, height-i-i-1, thickness, thickness);
  101. }
  102. g.setColor(oldColor);
  103. }
  104. /**
  105. * Returns the insets of the border.
  106. * @param c the component for which this border insets value applies
  107. */
  108. public Insets getBorderInsets(Component c) {
  109. return new Insets(thickness, thickness, thickness, thickness);
  110. }
  111. /**
  112. * Reinitialize the insets parameter with this Border's current Insets.
  113. * @param c the component for which this border insets value applies
  114. * @param insets the object to be reinitialized
  115. */
  116. public Insets getBorderInsets(Component c, Insets insets) {
  117. insets.left = insets.top = insets.right = insets.bottom = thickness;
  118. return insets;
  119. }
  120. /**
  121. * Returns the color of the border.
  122. */
  123. public Color getLineColor() {
  124. return lineColor;
  125. }
  126. /**
  127. * Returns the thickness of the border.
  128. */
  129. public int getThickness() {
  130. return thickness;
  131. }
  132. /**
  133. * Returns whether this border will be drawn with rounded corners.
  134. */
  135. public boolean getRoundedCorners() {
  136. return roundedCorners;
  137. }
  138. /**
  139. * Returns whether or not the border is opaque.
  140. */
  141. public boolean isBorderOpaque() {
  142. return !roundedCorners;
  143. }
  144. }