1 /* 2 * Copyright (c) 1997, 2014, 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™ 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 @SuppressWarnings("serial") // Same-version serialization only 54 public class LineBorder extends AbstractBorder 55 { 56 private static Border blackLine; 57 private static Border grayLine; 58 59 protected int thickness; 60 protected Color lineColor; 61 protected boolean roundedCorners; 62 63 /** Convenience method for getting the Color.black LineBorder of thickness 1. 64 */ 65 public static Border createBlackLineBorder() { 66 if (blackLine == null) { 67 blackLine = new LineBorder(Color.black, 1); 68 } 69 return blackLine; 70 } 71 72 /** Convenience method for getting the Color.gray LineBorder of thickness 1. 73 */ 74 public static Border createGrayLineBorder() { 75 if (grayLine == null) { 76 grayLine = new LineBorder(Color.gray, 1); 77 } 78 return grayLine; 79 } 80 81 /** 82 * Creates a line border with the specified color and a 83 * thickness = 1. 84 * @param color the color for the border 85 */ 86 public LineBorder(Color color) { 87 this(color, 1, false); 88 } 89 90 /** 91 * Creates a line border with the specified color and thickness. 92 * @param color the color of the border 93 * @param thickness the thickness of the border 94 */ 95 public LineBorder(Color color, int thickness) { 96 this(color, thickness, false); 97 } 98 99 /** 100 * Creates a line border with the specified color, thickness, 101 * and corner shape. 102 * @param color the color of the border 103 * @param thickness the thickness of the border 104 * @param roundedCorners whether or not border corners should be round 105 * @since 1.3 106 */ 107 @ConstructorProperties({"lineColor", "thickness", "roundedCorners"}) 108 public LineBorder(Color color, int thickness, boolean roundedCorners) { 109 lineColor = color; 110 this.thickness = thickness; 111 this.roundedCorners = roundedCorners; 112 } 113 114 /** 115 * Paints the border for the specified component with the 116 * specified position and size. 117 * @param c the component for which this border is being painted 118 * @param g the paint graphics 119 * @param x the x position of the painted border 120 * @param y the y position of the painted border 121 * @param width the width of the painted border 122 * @param height the height of the painted border 123 */ 124 public void paintBorder(Component c, Graphics g, int x, int y, 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, arc, arc); 139 inner = new RoundRectangle2D.Float(x + offs, y + offs, width - size, height - size, arc, arc); 140 } 141 else { 142 outer = new Rectangle2D.Float(x, y, width, height); 143 inner = new Rectangle2D.Float(x + offs, y + offs, width - size, height - size); 144 } 145 Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD); 146 path.append(outer, false); 147 path.append(inner, false); 148 g2d.fill(path); 149 g2d.setColor(oldColor); 150 } 151 } 152 153 /** 154 * Reinitialize the insets parameter with this Border's current Insets. 155 * @param c the component for which this border insets value applies 156 * @param insets the object to be reinitialized 157 */ 158 public Insets getBorderInsets(Component c, Insets insets) { 159 insets.set(thickness, thickness, thickness, thickness); 160 return insets; 161 } 162 163 /** 164 * Returns the color of the border. 165 */ 166 public Color getLineColor() { 167 return lineColor; 168 } 169 170 /** 171 * Returns the thickness of the border. 172 */ 173 public int getThickness() { 174 return thickness; 175 } 176 177 /** 178 * Returns whether this border will be drawn with rounded corners. 179 * @since 1.3 180 */ 181 public boolean getRoundedCorners() { 182 return roundedCorners; 183 } 184 185 /** 186 * Returns whether or not the border is opaque. 187 */ 188 public boolean isBorderOpaque() { 189 return !roundedCorners; 190 } 191 192 }