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