001 /*
002 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.border;
026
027 import java.awt.Graphics;
028 import java.awt.Insets;
029 import java.awt.Color;
030 import java.awt.Component;
031 import java.awt.Graphics2D;
032 import java.awt.Shape;
033 import java.awt.geom.Path2D;
034 import java.awt.geom.Rectangle2D;
035 import java.awt.geom.RoundRectangle2D;
036 import java.beans.ConstructorProperties;
037
038 /**
039 * A class which implements a line border of arbitrary thickness
040 * and of a single color.
041 * <p>
042 * <strong>Warning:</strong>
043 * Serialized objects of this class will not be compatible with
044 * future Swing releases. The current serialization support is
045 * appropriate for short term storage or RMI between applications running
046 * the same version of Swing. As of 1.4, support for long term storage
047 * of all JavaBeans<sup><font size="-2">TM</font></sup>
048 * has been added to the <code>java.beans</code> package.
049 * Please see {@link java.beans.XMLEncoder}.
050 *
051 * @version 1.31 05/09/07
052 * @author David Kloba
053 */
054 public class LineBorder extends AbstractBorder {
055 private static Border blackLine;
056 private static Border grayLine;
057
058 protected int thickness;
059 protected Color lineColor;
060 protected boolean roundedCorners;
061
062 /** Convenience method for getting the Color.black LineBorder of thickness 1.
063 */
064 public static Border createBlackLineBorder() {
065 if (blackLine == null) {
066 blackLine = new LineBorder(Color.black, 1);
067 }
068 return blackLine;
069 }
070
071 /** Convenience method for getting the Color.gray LineBorder of thickness 1.
072 */
073 public static Border createGrayLineBorder() {
074 if (grayLine == null) {
075 grayLine = new LineBorder(Color.gray, 1);
076 }
077 return grayLine;
078 }
079
080 /**
081 * Creates a line border with the specified color and a
082 * thickness = 1.
083 * @param color the color for the border
084 */
085 public LineBorder(Color color) {
086 this (color, 1, false);
087 }
088
089 /**
090 * Creates a line border with the specified color and thickness.
091 * @param color the color of the border
092 * @param thickness the thickness of the border
093 */
094 public LineBorder(Color color, int thickness) {
095 this (color, thickness, false);
096 }
097
098 /**
099 * 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,
124 int width, int height) {
125 if ((this .thickness > 0) && (g instanceof Graphics2D)) {
126 Graphics2D g2d = (Graphics2D) g;
127
128 Color oldColor = g2d.getColor();
129 g2d.setColor(this .lineColor);
130
131 Shape outer;
132 Shape inner;
133
134 int offs = this .thickness;
135 int size = offs + offs;
136 if (this .roundedCorners) {
137 int arc = offs + size;
138 outer = new RoundRectangle2D.Float(x, y, width, height,
139 arc, arc);
140 inner = new RoundRectangle2D.Float(x + offs, y + offs,
141 width - size, height - size, arc, arc);
142 } else {
143 outer = new Rectangle2D.Float(x, y, width, height);
144 inner = new Rectangle2D.Float(x + offs, y + offs, width
145 - size, height - size);
146 }
147 Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
148 path.append(outer, false);
149 path.append(inner, false);
150 g2d.fill(path);
151 g2d.setColor(oldColor);
152 }
153 }
154
155 /**
156 * Returns the insets of the border.
157 * @param c the component for which this border insets value applies
158 */
159 public Insets getBorderInsets(Component c) {
160 return new Insets(thickness, thickness, thickness, thickness);
161 }
162
163 /**
164 * Reinitialize the insets parameter with this Border's current Insets.
165 * @param c the component for which this border insets value applies
166 * @param insets the object to be reinitialized
167 */
168 public Insets getBorderInsets(Component c, Insets insets) {
169 insets.left = insets.top = insets.right = insets.bottom = thickness;
170 return insets;
171 }
172
173 /**
174 * Returns the color of the border.
175 */
176 public Color getLineColor() {
177 return lineColor;
178 }
179
180 /**
181 * Returns the thickness of the border.
182 */
183 public int getThickness() {
184 return thickness;
185 }
186
187 /**
188 * Returns whether this border will be drawn with rounded corners.
189 * @since 1.3
190 */
191 public boolean getRoundedCorners() {
192 return roundedCorners;
193 }
194
195 /**
196 * Returns whether or not the border is opaque.
197 */
198 public boolean isBorderOpaque() {
199 return !roundedCorners;
200 }
201
202 }
|