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.Component; 30 import java.awt.Color; 31 32 import javax.swing.Icon; 33 34 /** 35 * A class which provides a matte-like border of either a solid color 36 * or a tiled icon. 37 * <p> 38 * <strong>Warning:</strong> 39 * Serialized objects of this class will not be compatible with 40 * future Swing releases. The current serialization support is 41 * appropriate for short term storage or RMI between applications running 42 * the same version of Swing. As of 1.4, support for long term storage 43 * of all JavaBeans™ 44 * has been added to the <code>java.beans</code> package. 45 * Please see {@link java.beans.XMLEncoder}. 46 * 47 * @author Amy Fowler 48 */ 49 @SuppressWarnings("serial") 50 public class MatteBorder extends EmptyBorder 51 { 52 protected Color color; 53 protected Icon tileIcon; 54 55 /** 56 * Creates a matte border with the specified insets and color. 57 * @param top the top inset of the border 58 * @param left the left inset of the border 59 * @param bottom the bottom inset of the border 60 * @param right the right inset of the border 61 * @param matteColor the color rendered for the border 62 */ 63 public MatteBorder(int top, int left, int bottom, int right, Color matteColor) { 64 super(top, left, bottom, right); 65 this.color = matteColor; 66 } 67 68 /** 69 * Creates a matte border with the specified insets and color. 70 * @param borderInsets the insets of the border 71 * @param matteColor the color rendered for the border 72 * @since 1.3 73 */ 74 public MatteBorder(Insets borderInsets, Color matteColor) { 75 super(borderInsets); 76 this.color = matteColor; 77 } 78 79 /** 80 * Creates a matte border with the specified insets and tile icon. 81 * @param top the top inset of the border 82 * @param left the left inset of the border 83 * @param bottom the bottom inset of the border 84 * @param right the right inset of the border 85 * @param tileIcon the icon to be used for tiling the border 86 */ 87 public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon) { 88 super(top, left, bottom, right); 89 this.tileIcon = tileIcon; 90 } 91 92 /** 93 * Creates a matte border with the specified insets and tile icon. 94 * @param borderInsets the insets of the border 95 * @param tileIcon the icon to be used for tiling the border 96 * @since 1.3 97 */ 98 public MatteBorder(Insets borderInsets, Icon tileIcon) { 99 super(borderInsets); 100 this.tileIcon = tileIcon; 101 } 102 103 /** 104 * Creates a matte border with the specified tile icon. The 105 * insets will be calculated dynamically based on the size of 106 * the tile icon, where the top and bottom will be equal to the 107 * tile icon's height, and the left and right will be equal to 108 * the tile icon's width. 109 * @param tileIcon the icon to be used for tiling the border 110 */ 111 public MatteBorder(Icon tileIcon) { 112 this(-1,-1,-1,-1, tileIcon); 113 } 114 115 /** 116 * Paints the matte border. 117 */ 118 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 119 Insets insets = getBorderInsets(c); 120 Color oldColor = g.getColor(); 121 g.translate(x, y); 122 123 // If the tileIcon failed loading, paint as gray. 124 if (tileIcon != null) { 125 color = (tileIcon.getIconWidth() == -1) ? Color.gray : null; 126 } 127 128 if (color != null) { 129 g.setColor(color); 130 g.fillRect(0, 0, width - insets.right, insets.top); 131 g.fillRect(0, insets.top, insets.left, height - insets.top); 132 g.fillRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom); 133 g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom); 134 135 } else if (tileIcon != null) { 136 int tileW = tileIcon.getIconWidth(); 137 int tileH = tileIcon.getIconHeight(); 138 paintEdge(c, g, 0, 0, width - insets.right, insets.top, tileW, tileH); 139 paintEdge(c, g, 0, insets.top, insets.left, height - insets.top, tileW, tileH); 140 paintEdge(c, g, insets.left, height - insets.bottom, width - insets.left, insets.bottom, tileW, tileH); 141 paintEdge(c, g, width - insets.right, 0, insets.right, height - insets.bottom, tileW, tileH); 142 } 143 g.translate(-x, -y); 144 g.setColor(oldColor); 145 146 } 147 148 private void paintEdge(Component c, Graphics g, int x, int y, int width, int height, int tileW, int tileH) { 149 g = g.create(x, y, width, height); 150 int sY = -(y % tileH); 151 for (x = -(x % tileW); x < width; x += tileW) { 152 for (y = sY; y < height; y += tileH) { 153 this.tileIcon.paintIcon(c, g, x, y); 154 } 155 } 156 g.dispose(); 157 } 158 159 /** 160 * Reinitialize the insets parameter with this Border's current Insets. 161 * @param c the component for which this border insets value applies 162 * @param insets the object to be reinitialized 163 * @since 1.3 164 */ 165 public Insets getBorderInsets(Component c, Insets insets) { 166 return computeInsets(insets); 167 } 168 169 /** 170 * Returns the insets of the border. 171 * @since 1.3 172 */ 173 public Insets getBorderInsets() { 174 return computeInsets(new Insets(0,0,0,0)); 175 } 176 177 /* should be protected once api changes area allowed */ 178 private Insets computeInsets(Insets insets) { 179 if (tileIcon != null && top == -1 && bottom == -1 && 180 left == -1 && right == -1) { 181 int w = tileIcon.getIconWidth(); 182 int h = tileIcon.getIconHeight(); 183 insets.top = h; 184 insets.right = w; 185 insets.bottom = h; 186 insets.left = w; 187 } else { 188 insets.left = left; 189 insets.top = top; 190 insets.right = right; 191 insets.bottom = bottom; 192 } 193 return insets; 194 } 195 196 /** 197 * Returns the color used for tiling the border or null 198 * if a tile icon is being used. 199 * 200 * @return the {@code Color} object used to render the border or {@code null} 201 * if a tile icon is used 202 * @since 1.3 203 */ 204 public Color getMatteColor() { 205 return color; 206 } 207 208 /** 209 * Returns the icon used for tiling the border or null 210 * if a solid color is being used. 211 * 212 * @return the {@code Icon} used to tile the border or {@code null} if a 213 * solid color is used to fill the border 214 * @since 1.3 215 */ 216 public Icon getTileIcon() { 217 return tileIcon; 218 } 219 220 /** 221 * Returns whether or not the border is opaque. 222 * 223 * @return {@code true} if the border is opaque, {@code false} otherwise 224 */ 225 public boolean isBorderOpaque() { 226 // If a tileIcon is set, then it may contain transparent bits 227 return color != null; 228 } 229 230 }