View Javadoc
1   /*
2    * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  package javax.swing.border;
26  
27  import java.awt.Graphics;
28  import java.awt.Insets;
29  import java.awt.Color;
30  import java.awt.Component;
31  import java.awt.Graphics2D;
32  import java.awt.Shape;
33  import java.awt.geom.Path2D;
34  import java.awt.geom.Rectangle2D;
35  import java.awt.geom.RoundRectangle2D;
36  import java.beans.ConstructorProperties;
37  
38  /**
39   * A class which implements a line border of arbitrary thickness
40   * and of a single color.
41   * <p>
42   * <strong>Warning:</strong>
43   * Serialized objects of this class will not be compatible with
44   * future Swing releases. The current serialization support is
45   * appropriate for short term storage or RMI between applications running
46   * the same version of Swing.  As of 1.4, support for long term storage
47   * of all JavaBeans&trade;
48   * has been added to the <code>java.beans</code> package.
49   * Please see {@link java.beans.XMLEncoder}.
50   *
51   * @author David Kloba
52   */
53  public class LineBorder extends AbstractBorder
54  {
55      private static Border blackLine;
56      private static Border grayLine;
57  
58      protected int thickness;
59      protected Color lineColor;
60      protected boolean roundedCorners;
61  
62      /** Convenience method for getting the Color.black LineBorder of thickness 1.
63        */
64      public static Border createBlackLineBorder() {
65          if (blackLine == null) {
66              blackLine = new LineBorder(Color.black, 1);
67          }
68          return blackLine;
69      }
70  
71      /** Convenience method for getting the Color.gray LineBorder of thickness 1.
72        */
73      public static Border createGrayLineBorder() {
74          if (grayLine == null) {
75              grayLine = new LineBorder(Color.gray, 1);
76          }
77          return grayLine;
78      }
79  
80      /**
81       * Creates a line border with the specified color and a
82       * thickness = 1.
83       * @param color the color for the border
84       */
85      public LineBorder(Color color) {
86          this(color, 1, false);
87      }
88  
89      /**
90       * Creates a line border with the specified color and thickness.
91       * @param color the color of the border
92       * @param thickness the thickness of the border
93       */
94      public LineBorder(Color color, int thickness)  {
95          this(color, thickness, false);
96      }
97  
98      /**
99       * Creates a line border with the specified color, thickness,
100      * and corner shape.
101      * @param color the color of the border
102      * @param thickness the thickness of the border
103      * @param roundedCorners whether or not border corners should be round
104      * @since 1.3
105      */
106     @ConstructorProperties({"lineColor", "thickness", "roundedCorners"})
107     public LineBorder(Color color, int thickness, boolean roundedCorners)  {
108         lineColor = color;
109         this.thickness = thickness;
110         this.roundedCorners = roundedCorners;
111     }
112 
113     /**
114      * Paints the border for the specified component with the
115      * specified position and size.
116      * @param c the component for which this border is being painted
117      * @param g the paint graphics
118      * @param x the x position of the painted border
119      * @param y the y position of the painted border
120      * @param width the width of the painted border
121      * @param height the height of the painted border
122      */
123     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
124         if ((this.thickness > 0) && (g instanceof Graphics2D)) {
125             Graphics2D g2d = (Graphics2D) g;
126 
127             Color oldColor = g2d.getColor();
128             g2d.setColor(this.lineColor);
129 
130             Shape outer;
131             Shape inner;
132 
133             int offs = this.thickness;
134             int size = offs + offs;
135             if (this.roundedCorners) {
136                 int arc = offs + size;
137                 outer = new RoundRectangle2D.Float(x, y, width, height, arc, arc);
138                 inner = new RoundRectangle2D.Float(x + offs, y + offs, width - size, height - size, arc, arc);
139             }
140             else {
141                 outer = new Rectangle2D.Float(x, y, width, height);
142                 inner = new Rectangle2D.Float(x + offs, y + offs, width - size, height - size);
143             }
144             Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
145             path.append(outer, false);
146             path.append(inner, false);
147             g2d.fill(path);
148             g2d.setColor(oldColor);
149         }
150     }
151 
152     /**
153      * Reinitialize the insets parameter with this Border's current Insets.
154      * @param c the component for which this border insets value applies
155      * @param insets the object to be reinitialized
156      */
157     public Insets getBorderInsets(Component c, Insets insets) {
158         insets.set(thickness, thickness, thickness, thickness);
159         return insets;
160     }
161 
162     /**
163      * Returns the color of the border.
164      */
165     public Color getLineColor()     {
166         return lineColor;
167     }
168 
169     /**
170      * Returns the thickness of the border.
171      */
172     public int getThickness()       {
173         return thickness;
174     }
175 
176     /**
177      * Returns whether this border will be drawn with rounded corners.
178      * @since 1.3
179      */
180     public boolean getRoundedCorners() {
181         return roundedCorners;
182     }
183 
184     /**
185      * Returns whether or not the border is opaque.
186      */
187     public boolean isBorderOpaque() {
188         return !roundedCorners;
189     }
190 
191 }