1. /*
  2. * @(#)LineBorder.java 1.19 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 line border of arbitrary thickness
  18. * and of a single color.
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is appropriate
  23. * for short term storage or RMI between applications running the same
  24. * version of Swing. A future release of Swing will provide support for
  25. * long term persistence.
  26. *
  27. * @version 1.19 02/02/00
  28. * @author David Kloba
  29. */
  30. public class LineBorder extends AbstractBorder
  31. {
  32. private static Border blackLine;
  33. private static Border grayLine;
  34. protected int thickness;
  35. protected Color lineColor;
  36. protected boolean roundedCorners;
  37. /** Convenience method for getting the Color.black LineBorder of thickness 1.
  38. */
  39. public static Border createBlackLineBorder() {
  40. if (blackLine == null) {
  41. blackLine = new LineBorder(Color.black, 1);
  42. }
  43. return blackLine;
  44. }
  45. /** Convenience method for getting the Color.gray LineBorder of thickness 1.
  46. */
  47. public static Border createGrayLineBorder() {
  48. if (grayLine == null) {
  49. grayLine = new LineBorder(Color.gray, 1);
  50. }
  51. return grayLine;
  52. }
  53. /**
  54. * Creates a line border with the specified color and a
  55. * thickness = 1.
  56. * @param color the color for the border
  57. */
  58. public LineBorder(Color color) {
  59. this(color, 1, false);
  60. }
  61. /**
  62. * Creates a line border with the specified color and thickness.
  63. * @param color the color of the border
  64. * @param thickness the thickness of the border
  65. */
  66. public LineBorder(Color color, int thickness) {
  67. this(color, thickness, false);
  68. }
  69. /**
  70. * Creates a line border with the specified color, thickness,
  71. * and corner shape.
  72. * @param color the color of the border
  73. * @param thickness the thickness of the border
  74. * @param roundedCorners whether or not border corners should be round
  75. * @since 1.3
  76. */
  77. public LineBorder(Color color, int thickness, boolean roundedCorners) {
  78. lineColor = color;
  79. this.thickness = thickness;
  80. this.roundedCorners = roundedCorners;
  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. Color oldColor = g.getColor();
  94. int i;
  95. /// PENDING(klobad) How/should do we support Roundtangles?
  96. g.setColor(lineColor);
  97. for(i = 0; i < thickness; i++) {
  98. if(!roundedCorners)
  99. g.drawRect(x+i, y+i, width-i-i-1, height-i-i-1);
  100. else
  101. g.drawRoundRect(x+i, y+i, width-i-i-1, height-i-i-1, thickness, thickness);
  102. }
  103. g.setColor(oldColor);
  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(thickness, thickness, thickness, thickness);
  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 = thickness;
  119. return insets;
  120. }
  121. /**
  122. * Returns the color of the border.
  123. */
  124. public Color getLineColor() {
  125. return lineColor;
  126. }
  127. /**
  128. * Returns the thickness of the border.
  129. */
  130. public int getThickness() {
  131. return thickness;
  132. }
  133. /**
  134. * Returns whether this border will be drawn with rounded corners.
  135. */
  136. public boolean getRoundedCorners() {
  137. return roundedCorners;
  138. }
  139. /**
  140. * Returns whether or not the border is opaque.
  141. */
  142. public boolean isBorderOpaque() {
  143. return !roundedCorners;
  144. }
  145. }