Showing the changes to java/pkg/javax/swing/BorderFactory.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,759 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997, 2011, 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; | ||
| 26 | |||
| 27 | import java.awt.BasicStroke; | ||
| 28 | import java.awt.Color; | ||
| 29 | import java.awt.Font; | ||
| 30 | import java.awt.Paint; | ||
| 31 | import javax.swing.border.*; | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Factory class for vending standard <code>Border</code> objects. Wherever | ||
| 35 | * possible, this factory will hand out references to shared | ||
| 36 | * <code>Border</code> instances. | ||
| 37 | * For further information and examples see | ||
| 38 | * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/border.html">How | ||
| 39 | to Use Borders</a>, | ||
| 40 | * a section in <em>The Java Tutorial</em>. | ||
| 41 | * | ||
| 42 | * @author David Kloba | ||
| 43 | */ | ||
| 44 | public class BorderFactory | ||
| 45 | { | ||
| 46 | |||
| 47 | /** Don't let anyone instantiate this class */ | ||
| 48 | private BorderFactory() { | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | //// LineBorder /////////////////////////////////////////////////////////////// | ||
| 53 | /** | ||
| 54 | * Creates a line border withe the specified color. | ||
| 55 | * | ||
| 56 | * @param color a <code>Color</code> to use for the line | ||
| 57 | * @return the <code>Border</code> object | ||
| 58 | */ | ||
| 59 | public static Border createLineBorder(Color color) { | ||
| 60 | return new LineBorder(color, 1); | ||
| 61 | } | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Creates a line border with the specified color | ||
| 65 | * and width. The width applies to all four sides of the | ||
| 66 | * border. To specify widths individually for the top, | ||
| 67 | * bottom, left, and right, use | ||
| 68 | * {@link #createMatteBorder(int,int,int,int,Color)}. | ||
| 69 | * | ||
| 70 | * @param color a <code>Color</code> to use for the line | ||
| 71 | * @param thickness an integer specifying the width in pixels | ||
| 72 | * @return the <code>Border</code> object | ||
| 73 | */ | ||
| 74 | public static Border createLineBorder(Color color, int thickness) { | ||
| 75 | return new LineBorder(color, thickness); | ||
| 76 | } | ||
| 77 | |||
| 78 | /** | ||
| 79 | * Creates a line border with the specified color, thickness, and corner shape. | ||
| 80 | * | ||
| 81 | * @param color the color of the border | ||
| 82 | * @param thickness the thickness of the border | ||
| 83 | * @param rounded whether or not border corners should be round | ||
| 84 | * @return the {@code Border} object | ||
| 85 | * | ||
| 86 | * @see LineBorder#LineBorder(Color, int, boolean) | ||
| 87 | * @since 1.7 | ||
| 88 | */ | ||
| 89 | public static Border createLineBorder(Color color, int thickness, boolean rounded) { | ||
| 90 | return new LineBorder(color, thickness, rounded); | ||
| 91 | } | ||
| 92 | |||
| 93 | //// BevelBorder ///////////////////////////////////////////////////////////// | ||
| 94 | /////////////////////////////////////////////////////////////////////////////// | ||
| 95 | static final Border sharedRaisedBevel = new BevelBorder(BevelBorder.RAISED); | ||
| 96 | static final Border sharedLoweredBevel = new BevelBorder(BevelBorder.LOWERED); | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Creates a border with a raised beveled edge, using | ||
| 100 | * brighter shades of the component's current background color | ||
| 101 | * for highlighting, and darker shading for shadows. | ||
| 102 | * (In a raised border, highlights are on top and shadows | ||
| 103 | * are underneath.) | ||
| 104 | * | ||
| 105 | * @return the <code>Border</code> object | ||
| 106 | */ | ||
| 107 | public static Border createRaisedBevelBorder() { | ||
| 108 | return createSharedBevel(BevelBorder.RAISED); | ||
| 109 | } | ||
| 110 | |||
| 111 | /** | ||
| 112 | * Creates a border with a lowered beveled edge, using | ||
| 113 | * brighter shades of the component's current background color | ||
| 114 | * for highlighting, and darker shading for shadows. | ||
| 115 | * (In a lowered border, shadows are on top and highlights | ||
| 116 | * are underneath.) | ||
| 117 | * | ||
| 118 | * @return the <code>Border</code> object | ||
| 119 | */ | ||
| 120 | public static Border createLoweredBevelBorder() { | ||
| 121 | return createSharedBevel(BevelBorder.LOWERED); | ||
| 122 | } | ||
| 123 | |||
| 124 | /** | ||
| 125 | * Creates a beveled border of the specified type, using | ||
| 126 | * brighter shades of the component's current background color | ||
| 127 | * for highlighting, and darker shading for shadows. | ||
| 128 | * (In a lowered border, shadows are on top and highlights | ||
| 129 | * are underneath.) | ||
| 130 | * | ||
| 131 | * @param type an integer specifying either | ||
| 132 | * <code>BevelBorder.LOWERED</code> or | ||
| 133 | * <code>BevelBorder.RAISED</code> | ||
| 134 | * @return the <code>Border</code> object | ||
| 135 | */ | ||
| 136 | public static Border createBevelBorder(int type) { | ||
| 137 | return createSharedBevel(type); | ||
| 138 | } | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Creates a beveled border of the specified type, using | ||
| 142 | * the specified highlighting and shadowing. The outer | ||
| 143 | * edge of the highlighted area uses a brighter shade of | ||
| 144 | * the highlight color. The inner edge of the shadow area | ||
| 145 | * uses a brighter shade of the shadow color. | ||
| 146 | * | ||
| 147 | * @param type an integer specifying either | ||
| 148 | * <code>BevelBorder.LOWERED</code> or | ||
| 149 | * <code>BevelBorder.RAISED</code> | ||
| 150 | * @param highlight a <code>Color</code> object for highlights | ||
| 151 | * @param shadow a <code>Color</code> object for shadows | ||
| 152 | * @return the <code>Border</code> object | ||
| 153 | */ | ||
| 154 | public static Border createBevelBorder(int type, Color highlight, Color shadow) { | ||
| 155 | return new BevelBorder(type, highlight, shadow); | ||
| 156 | } | ||
| 157 | |||
| 158 | /** | ||
| 159 | * Creates a beveled border of the specified type, using | ||
| 160 | * the specified colors for the inner and outer highlight | ||
| 161 | * and shadow areas. | ||
| 162 | * | ||
| 163 | * @param type an integer specifying either | ||
| 164 | * <code>BevelBorder.LOWERED</code> or | ||
| 165 | * <code>BevelBorder.RAISED</code> | ||
| 166 | * @param highlightOuter a <code>Color</code> object for the | ||
| 167 | * outer edge of the highlight area | ||
| 168 | * @param highlightInner a <code>Color</code> object for the | ||
| 169 | * inner edge of the highlight area | ||
| 170 | * @param shadowOuter a <code>Color</code> object for the | ||
| 171 | * outer edge of the shadow area | ||
| 172 | * @param shadowInner a <code>Color</code> object for the | ||
| 173 | * inner edge of the shadow area | ||
| 174 | * @return the <code>Border</code> object | ||
| 175 | */ | ||
| 176 | public static Border createBevelBorder(int type, | ||
| 177 | Color highlightOuter, Color highlightInner, | ||
| 178 | Color shadowOuter, Color shadowInner) { | ||
| 179 | return new BevelBorder(type, highlightOuter, highlightInner, | ||
| 180 | shadowOuter, shadowInner); | ||
| 181 | } | ||
| 182 | |||
| 183 | static Border createSharedBevel(int type) { | ||
| 184 | if(type == BevelBorder.RAISED) { | ||
| 185 | return sharedRaisedBevel; | ||
| 186 | } else if(type == BevelBorder.LOWERED) { | ||
| 187 | return sharedLoweredBevel; | ||
| 188 | } | ||
| 189 | return null; | ||
| 190 | } | ||
| 191 | |||
| 192 | //// SoftBevelBorder /////////////////////////////////////////////////////////// | ||
| 193 | //////////////////////////////////////////////////////////////////////////////// | ||
| 194 | |||
| 195 | private static Border sharedSoftRaisedBevel; | ||
| 196 | private static Border sharedSoftLoweredBevel; | ||
| 197 | |||
| 198 | /** | ||
| 199 | * Creates a beveled border with a raised edge and softened corners, | ||
| 200 | * using brighter shades of the component's current background color | ||
| 201 | * for highlighting, and darker shading for shadows. | ||
| 202 | * In a raised border, highlights are on top and shadows are underneath. | ||
| 203 | * | ||
| 204 | * @return the {@code Border} object | ||
| 205 | * | ||
| 206 | * @since 1.7 | ||
| 207 | */ | ||
| 208 | public static Border createRaisedSoftBevelBorder() { | ||
| 209 | if (sharedSoftRaisedBevel == null) { | ||
| 210 | sharedSoftRaisedBevel = new SoftBevelBorder(BevelBorder.RAISED); | ||
| 211 | } | ||
| 212 | return sharedSoftRaisedBevel; | ||
| 213 | } | ||
| 214 | |||
| 215 | /** | ||
| 216 | * Creates a beveled border with a lowered edge and softened corners, | ||
| 217 | * using brighter shades of the component's current background color | ||
| 218 | * for highlighting, and darker shading for shadows. | ||
| 219 | * In a lowered border, shadows are on top and highlights are underneath. | ||
| 220 | * | ||
| 221 | * @return the {@code Border} object | ||
| 222 | * | ||
| 223 | * @since 1.7 | ||
| 224 | */ | ||
| 225 | public static Border createLoweredSoftBevelBorder() { | ||
| 226 | if (sharedSoftLoweredBevel == null) { | ||
| 227 | sharedSoftLoweredBevel = new SoftBevelBorder(BevelBorder.LOWERED); | ||
| 228 | } | ||
| 229 | return sharedSoftLoweredBevel; | ||
| 230 | } | ||
| 231 | |||
| 232 | /** | ||
| 233 | * Creates a beveled border of the specified type with softened corners, | ||
| 234 | * using brighter shades of the component's current background color | ||
| 235 | * for highlighting, and darker shading for shadows. | ||
| 236 | * The type is either {@link BevelBorder#RAISED} or {@link BevelBorder#LOWERED}. | ||
| 237 | * | ||
| 238 | * @param type a type of a bevel | ||
| 239 | * @return the {@code Border} object or {@code null} | ||
| 240 | * if the specified type is not valid | ||
| 241 | * | ||
| 242 | * @see BevelBorder#BevelBorder(int) | ||
| 243 | * @since 1.7 | ||
| 244 | */ | ||
| 245 | public static Border createSoftBevelBorder(int type) { | ||
| 246 | if (type == BevelBorder.RAISED) { | ||
| 247 | return createRaisedSoftBevelBorder(); | ||
| 248 | } | ||
| 249 | if (type == BevelBorder.LOWERED) { | ||
| 250 | return createLoweredSoftBevelBorder(); | ||
| 251 | } | ||
| 252 | return null; | ||
| 253 | } | ||
| 254 | |||
| 255 | /** | ||
| 256 | * Creates a beveled border of the specified type with softened corners, | ||
| 257 | * using the specified highlighting and shadowing. | ||
| 258 | * The type is either {@link BevelBorder#RAISED} or {@link BevelBorder#LOWERED}. | ||
| 259 | * The outer edge of the highlight area uses | ||
| 260 | * a brighter shade of the {@code highlight} color. | ||
| 261 | * The inner edge of the shadow area uses | ||
| 262 | * a brighter shade of the {@code shadow} color. | ||
| 263 | * | ||
| 264 | * @param type a type of a bevel | ||
| 265 | * @param highlight a basic color of the highlight area | ||
| 266 | * @param shadow a basic color of the shadow area | ||
| 267 | * @return the {@code Border} object | ||
| 268 | * | ||
| 269 | * @see BevelBorder#BevelBorder(int, Color, Color) | ||
| 270 | * @since 1.7 | ||
| 271 | */ | ||
| 272 | public static Border createSoftBevelBorder(int type, Color highlight, Color shadow) { | ||
| 273 | return new SoftBevelBorder(type, highlight, shadow); | ||
| 274 | } | ||
| 275 | |||
| 276 | /** | ||
| 277 | * Creates a beveled border of the specified type with softened corners, | ||
| 278 | * using the specified colors for the inner and outer edges | ||
| 279 | * of the highlight and the shadow areas. | ||
| 280 | * The type is either {@link BevelBorder#RAISED} or {@link BevelBorder#LOWERED}. | ||
| 281 | * Note: The shadow inner and outer colors are switched | ||
| 282 | * for a lowered bevel border. | ||
| 283 | * | ||
| 284 | * @param type a type of a bevel | ||
| 285 | * @param highlightOuter a color of the outer edge of the highlight area | ||
| 286 | * @param highlightInner a color of the inner edge of the highlight area | ||
| 287 | * @param shadowOuter a color of the outer edge of the shadow area | ||
| 288 | * @param shadowInner a color of the inner edge of the shadow area | ||
| 289 | * @return the {@code Border} object | ||
| 290 | * | ||
| 291 | * @see BevelBorder#BevelBorder(int, Color, Color, Color, Color) | ||
| 292 | * @since 1.7 | ||
| 293 | */ | ||
| 294 | public static Border createSoftBevelBorder(int type, Color highlightOuter, Color highlightInner, Color shadowOuter, Color shadowInner) { | ||
| 295 | return new SoftBevelBorder(type, highlightOuter, highlightInner, shadowOuter, shadowInner); | ||
| 296 | } | ||
| 297 | |||
| 298 | //// EtchedBorder /////////////////////////////////////////////////////////// | ||
| 299 | |||
| 300 | static final Border sharedEtchedBorder = new EtchedBorder(); | ||
| 301 | private static Border sharedRaisedEtchedBorder; | ||
| 302 | |||
| 303 | /** | ||
| 304 | * Creates a border with an "etched" look using | ||
| 305 | * the component's current background color for | ||
| 306 | * highlighting and shading. | ||
| 307 | * | ||
| 308 | * @return the <code>Border</code> object | ||
| 309 | */ | ||
| 310 | public static Border createEtchedBorder() { | ||
| 311 | return sharedEtchedBorder; | ||
| 312 | } | ||
| 313 | |||
| 314 | /** | ||
| 315 | * Creates a border with an "etched" look using | ||
| 316 | * the specified highlighting and shading colors. | ||
| 317 | * | ||
| 318 | * @param highlight a <code>Color</code> object for the border highlights | ||
| 319 | * @param shadow a <code>Color</code> object for the border shadows | ||
| 320 | * @return the <code>Border</code> object | ||
| 321 | */ | ||
| 322 | public static Border createEtchedBorder(Color highlight, Color shadow) { | ||
| 323 | return new EtchedBorder(highlight, shadow); | ||
| 324 | } | ||
| 325 | |||
| 326 | /** | ||
| 327 | * Creates a border with an "etched" look using | ||
| 328 | * the component's current background color for | ||
| 329 | * highlighting and shading. | ||
| 330 | * | ||
| 331 | * @param type one of <code>EtchedBorder.RAISED</code>, or | ||
| 332 | * <code>EtchedBorder.LOWERED</code> | ||
| 333 | * @return the <code>Border</code> object | ||
| 334 | * @exception IllegalArgumentException if type is not either | ||
| 335 | * <code>EtchedBorder.RAISED</code> or | ||
| 336 | * <code>EtchedBorder.LOWERED</code> | ||
| 337 | * @since 1.3 | ||
| 338 | */ | ||
| 339 | public static Border createEtchedBorder(int type) { | ||
| 340 | switch (type) { | ||
| 341 | case EtchedBorder.RAISED: | ||
| 342 | if (sharedRaisedEtchedBorder == null) { | ||
| 343 | sharedRaisedEtchedBorder = new EtchedBorder | ||
| 344 | (EtchedBorder.RAISED); | ||
| 345 | } | ||
| 346 | return sharedRaisedEtchedBorder; | ||
| 347 | case EtchedBorder.LOWERED: | ||
| 348 | return sharedEtchedBorder; | ||
| 349 | default: | ||
| 350 | throw new IllegalArgumentException("type must be one of EtchedBorder.RAISED or EtchedBorder.LOWERED"); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | /** | ||
| 355 | * Creates a border with an "etched" look using | ||
| 356 | * the specified highlighting and shading colors. | ||
| 357 | * | ||
| 358 | * @param type one of <code>EtchedBorder.RAISED</code>, or | ||
| 359 | * <code>EtchedBorder.LOWERED</code> | ||
| 360 | * @param highlight a <code>Color</code> object for the border highlights | ||
| 361 | * @param shadow a <code>Color</code> object for the border shadows | ||
| 362 | * @return the <code>Border</code> object | ||
| 363 | * @since 1.3 | ||
| 364 | */ | ||
| 365 | public static Border createEtchedBorder(int type, Color highlight, | ||
| 366 | Color shadow) { | ||
| 367 | return new EtchedBorder(type, highlight, shadow); | ||
| 368 | } | ||
| 369 | |||
| 370 | //// TitledBorder //////////////////////////////////////////////////////////// | ||
| 371 | /** | ||
| 372 | * Creates a new titled border with the specified title, | ||
| 373 | * the default border type (determined by the current look and feel), | ||
| 374 | * the default text position (sitting on the top line), | ||
| 375 | * the default justification (leading), and the default | ||
| 376 | * font and text color (determined by the current look and feel). | ||
| 377 | * | ||
| 378 | * @param title a <code>String</code> containing the text of the title | ||
| 379 | * @return the <code>TitledBorder</code> object | ||
| 380 | */ | ||
| 381 | public static TitledBorder createTitledBorder(String title) { | ||
| 382 | return new TitledBorder(title); | ||
| 383 | } | ||
| 384 | |||
| 385 | /** | ||
| 386 | * Creates a new titled border with an empty title, | ||
| 387 | * the specified border object, | ||
| 388 | * the default text position (sitting on the top line), | ||
| 389 | * the default justification (leading), and the default | ||
| 390 | * font and text color (determined by the current look and feel). | ||
| 391 | * | ||
| 392 | * @param border the <code>Border</code> object to add the title to; if | ||
| 393 | * <code>null</code> the <code>Border</code> is determined | ||
| 394 | * by the current look and feel. | ||
| 395 | * @return the <code>TitledBorder</code> object | ||
| 396 | */ | ||
| 397 | public static TitledBorder createTitledBorder(Border border) { | ||
| 398 | return new TitledBorder(border); | ||
| 399 | } | ||
| 400 | |||
| 401 | /** | ||
| 402 | * Adds a title to an existing border, | ||
| 403 | * with default positioning (sitting on the top line), | ||
| 404 | * default justification (leading) and the default | ||
| 405 | * font and text color (determined by the current look and feel). | ||
| 406 | * | ||
| 407 | * @param border the <code>Border</code> object to add the title to | ||
| 408 | * @param title a <code>String</code> containing the text of the title | ||
| 409 | * @return the <code>TitledBorder</code> object | ||
| 410 | */ | ||
| 411 | public static TitledBorder createTitledBorder(Border border, | ||
| 412 | String title) { | ||
| 413 | return new TitledBorder(border, title); | ||
| 414 | } | ||
| 415 | |||
| 416 | /** | ||
| 417 | * Adds a title to an existing border, with the specified | ||
| 418 | * positioning and using the default | ||
| 419 | * font and text color (determined by the current look and feel). | ||
| 420 | * | ||
| 421 | * @param border the <code>Border</code> object to add the title to | ||
| 422 | * @param title a <code>String</code> containing the text of the title | ||
| 423 | * @param titleJustification an integer specifying the justification | ||
| 424 | * of the title -- one of the following: | ||
| 425 | *<ul> | ||
| 426 | *<li><code>TitledBorder.LEFT</code> | ||
| 427 | *<li><code>TitledBorder.CENTER</code> | ||
| 428 | *<li><code>TitledBorder.RIGHT</code> | ||
| 429 | *<li><code>TitledBorder.LEADING</code> | ||
| 430 | *<li><code>TitledBorder.TRAILING</code> | ||
| 431 | *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading) | ||
| 432 | *</ul> | ||
| 433 | * @param titlePosition an integer specifying the vertical position of | ||
| 434 | * the text in relation to the border -- one of the following: | ||
| 435 | *<ul> | ||
| 436 | *<li><code> TitledBorder.ABOVE_TOP</code> | ||
| 437 | *<li><code>TitledBorder.TOP</code> (sitting on the top line) | ||
| 438 | *<li><code>TitledBorder.BELOW_TOP</code> | ||
| 439 | *<li><code>TitledBorder.ABOVE_BOTTOM</code> | ||
| 440 | *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line) | ||
| 441 | *<li><code>TitledBorder.BELOW_BOTTOM</code> | ||
| 442 | *<li><code>TitledBorder.DEFAULT_POSITION</code> (top) | ||
| 443 | *</ul> | ||
| 444 | * @return the <code>TitledBorder</code> object | ||
| 445 | */ | ||
| 446 | public static TitledBorder createTitledBorder(Border border, | ||
| 447 | String title, | ||
| 448 | int titleJustification, | ||
| 449 | int titlePosition) { | ||
| 450 | return new TitledBorder(border, title, titleJustification, | ||
| 451 | titlePosition); | ||
| 452 | } | ||
| 453 | |||
| 454 | /** | ||
| 455 | * Adds a title to an existing border, with the specified | ||
| 456 | * positioning and font, and using the default text color | ||
| 457 | * (determined by the current look and feel). | ||
| 458 | * | ||
| 459 | * @param border the <code>Border</code> object to add the title to | ||
| 460 | * @param title a <code>String</code> containing the text of the title | ||
| 461 | * @param titleJustification an integer specifying the justification | ||
| 462 | * of the title -- one of the following: | ||
| 463 | *<ul> | ||
| 464 | *<li><code>TitledBorder.LEFT</code> | ||
| 465 | *<li><code>TitledBorder.CENTER</code> | ||
| 466 | *<li><code>TitledBorder.RIGHT</code> | ||
| 467 | *<li><code>TitledBorder.LEADING</code> | ||
| 468 | *<li><code>TitledBorder.TRAILING</code> | ||
| 469 | *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading) | ||
| 470 | *</ul> | ||
| 471 | * @param titlePosition an integer specifying the vertical position of | ||
| 472 | * the text in relation to the border -- one of the following: | ||
| 473 | *<ul> | ||
| 474 | *<li><code> TitledBorder.ABOVE_TOP</code> | ||
| 475 | *<li><code>TitledBorder.TOP</code> (sitting on the top line) | ||
| 476 | *<li><code>TitledBorder.BELOW_TOP</code> | ||
| 477 | *<li><code>TitledBorder.ABOVE_BOTTOM</code> | ||
| 478 | *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line) | ||
| 479 | *<li><code>TitledBorder.BELOW_BOTTOM</code> | ||
| 480 | *<li><code>TitledBorder.DEFAULT_POSITION</code> (top) | ||
| 481 | *</ul> | ||
| 482 | * @param titleFont a Font object specifying the title font | ||
| 483 | * @return the TitledBorder object | ||
| 484 | */ | ||
| 485 | public static TitledBorder createTitledBorder(Border border, | ||
| 486 | String title, | ||
| 487 | int titleJustification, | ||
| 488 | int titlePosition, | ||
| 489 | Font titleFont) { | ||
| 490 | return new TitledBorder(border, title, titleJustification, | ||
| 491 | titlePosition, titleFont); | ||
| 492 | } | ||
| 493 | |||
| 494 | /** | ||
| 495 | * Adds a title to an existing border, with the specified | ||
| 496 | * positioning, font and color. | ||
| 497 | * | ||
| 498 | * @param border the <code>Border</code> object to add the title to | ||
| 499 | * @param title a <code>String</code> containing the text of the title | ||
| 500 | * @param titleJustification an integer specifying the justification | ||
| 501 | * of the title -- one of the following: | ||
| 502 | *<ul> | ||
| 503 | *<li><code>TitledBorder.LEFT</code> | ||
| 504 | *<li><code>TitledBorder.CENTER</code> | ||
| 505 | *<li><code>TitledBorder.RIGHT</code> | ||
| 506 | *<li><code>TitledBorder.LEADING</code> | ||
| 507 | *<li><code>TitledBorder.TRAILING</code> | ||
| 508 | *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading) | ||
| 509 | *</ul> | ||
| 510 | * @param titlePosition an integer specifying the vertical position of | ||
| 511 | * the text in relation to the border -- one of the following: | ||
| 512 | *<ul> | ||
| 513 | *<li><code> TitledBorder.ABOVE_TOP</code> | ||
| 514 | *<li><code>TitledBorder.TOP</code> (sitting on the top line) | ||
| 515 | *<li><code>TitledBorder.BELOW_TOP</code> | ||
| 516 | *<li><code>TitledBorder.ABOVE_BOTTOM</code> | ||
| 517 | *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line) | ||
| 518 | *<li><code>TitledBorder.BELOW_BOTTOM</code> | ||
| 519 | *<li><code>TitledBorder.DEFAULT_POSITION</code> (top) | ||
| 520 | *</ul> | ||
| 521 | * @param titleFont a <code>Font</code> object specifying the title font | ||
| 522 | * @param titleColor a <code>Color</code> object specifying the title color | ||
| 523 | * @return the <code>TitledBorder</code> object | ||
| 524 | */ | ||
| 525 | public static TitledBorder createTitledBorder(Border border, | ||
| 526 | String title, | ||
| 527 | int titleJustification, | ||
| 528 | int titlePosition, | ||
| 529 | Font titleFont, | ||
| 530 | Color titleColor) { | ||
| 531 | return new TitledBorder(border, title, titleJustification, | ||
| 532 | titlePosition, titleFont, titleColor); | ||
| 533 | } | ||
| 534 | //// EmptyBorder /////////////////////////////////////////////////////////// | ||
| 535 | final static Border emptyBorder = new EmptyBorder(0, 0, 0, 0); | ||
| 536 | |||
| 537 | /** | ||
| 538 | * Creates an empty border that takes up no space. (The width | ||
| 539 | * of the top, bottom, left, and right sides are all zero.) | ||
| 540 | * | ||
| 541 | * @return the <code>Border</code> object | ||
| 542 | */ | ||
| 543 | public static Border createEmptyBorder() { | ||
| 544 | return emptyBorder; | ||
| 545 | } | ||
| 546 | |||
| 547 | /** | ||
| 548 | * Creates an empty border that takes up space but which does | ||
| 549 | * no drawing, specifying the width of the top, left, bottom, and | ||
| 550 | * right sides. | ||
| 551 | * | ||
| 552 | * @param top an integer specifying the width of the top, | ||
| 553 | * in pixels | ||
| 554 | * @param left an integer specifying the width of the left side, | ||
| 555 | * in pixels | ||
| 556 | * @param bottom an integer specifying the width of the bottom, | ||
| 557 | * in pixels | ||
| 558 | * @param right an integer specifying the width of the right side, | ||
| 559 | * in pixels | ||
| 560 | * @return the <code>Border</code> object | ||
| 561 | */ | ||
| 562 | public static Border createEmptyBorder(int top, int left, | ||
| 563 | int bottom, int right) { | ||
| 564 | return new EmptyBorder(top, left, bottom, right); | ||
| 565 | } | ||
| 566 | |||
| 567 | //// CompoundBorder //////////////////////////////////////////////////////// | ||
| 568 | /** | ||
| 569 | * Creates a compound border with a <code>null</code> inside edge and a | ||
| 570 | * <code>null</code> outside edge. | ||
| 571 | * | ||
| 572 | * @return the <code>CompoundBorder</code> object | ||
| 573 | */ | ||
| 574 | public static CompoundBorder createCompoundBorder() { | ||
| 575 | return new CompoundBorder(); | ||
| 576 | } | ||
| 577 | |||
| 578 | /** | ||
| 579 | * Creates a compound border specifying the border objects to use | ||
| 580 | * for the outside and inside edges. | ||
| 581 | * | ||
| 582 | * @param outsideBorder a <code>Border</code> object for the outer | ||
| 583 | * edge of the compound border | ||
| 584 | * @param insideBorder a <code>Border</code> object for the inner | ||
| 585 | * edge of the compound border | ||
| 586 | * @return the <code>CompoundBorder</code> object | ||
| 587 | */ | ||
| 588 | public static CompoundBorder createCompoundBorder(Border outsideBorder, | ||
| 589 | Border insideBorder) { | ||
| 590 | return new CompoundBorder(outsideBorder, insideBorder); | ||
| 591 | } | ||
| 592 | |||
| 593 | //// MatteBorder //////////////////////////////////////////////////////// | ||
| 594 | /** | ||
| 595 | * Creates a matte-look border using a solid color. (The difference between | ||
| 596 | * this border and a line border is that you can specify the individual | ||
| 597 | * border dimensions.) | ||
| 598 | * | ||
| 599 | * @param top an integer specifying the width of the top, | ||
| 600 | * in pixels | ||
| 601 | * @param left an integer specifying the width of the left side, | ||
| 602 | * in pixels | ||
| 603 | * @param bottom an integer specifying the width of the right side, | ||
| 604 | * in pixels | ||
| 605 | * @param right an integer specifying the width of the bottom, | ||
| 606 | * in pixels | ||
| 607 | * @param color a <code>Color</code> to use for the border | ||
| 608 | * @return the <code>MatteBorder</code> object | ||
| 609 | */ | ||
| 610 | public static MatteBorder createMatteBorder(int top, int left, int bottom, int right, | ||
| 611 | Color color) { | ||
| 612 | return new MatteBorder(top, left, bottom, right, color); | ||
| 613 | } | ||
| 614 | |||
| 615 | /** | ||
| 616 | * Creates a matte-look border that consists of multiple tiles of a | ||
| 617 | * specified icon. Multiple copies of the icon are placed side-by-side | ||
| 618 | * to fill up the border area. | ||
| 619 | * <p> | ||
| 620 | * Note:<br> | ||
| 621 | * If the icon doesn't load, the border area is painted gray. | ||
| 622 | * | ||
| 623 | * @param top an integer specifying the width of the top, | ||
| 624 | * in pixels | ||
| 625 | * @param left an integer specifying the width of the left side, | ||
| 626 | * in pixels | ||
| 627 | * @param bottom an integer specifying the width of the right side, | ||
| 628 | * in pixels | ||
| 629 | * @param right an integer specifying the width of the bottom, | ||
| 630 | * in pixels | ||
| 631 | * @param tileIcon the <code>Icon</code> object used for the border tiles | ||
| 632 | * @return the <code>MatteBorder</code> object | ||
| 633 | */ | ||
| 634 | public static MatteBorder createMatteBorder(int top, int left, int bottom, int right, | ||
| 635 | Icon tileIcon) { | ||
| 636 | return new MatteBorder(top, left, bottom, right, tileIcon); | ||
| 637 | } | ||
| 638 | |||
| 639 | //// StrokeBorder ////////////////////////////////////////////////////////////// | ||
| 640 | //////////////////////////////////////////////////////////////////////////////// | ||
| 641 | |||
| 642 | /** | ||
| 643 | * Creates a border of the specified {@code stroke}. | ||
| 644 | * The component's foreground color will be used to render the border. | ||
| 645 | * | ||
| 646 | * @param stroke the {@link BasicStroke} object used to stroke a shape | ||
| 647 | * @return the {@code Border} object | ||
| 648 | * | ||
| 649 | * @throws NullPointerException if the specified {@code stroke} is {@code null} | ||
| 650 | * | ||
| 651 | * @since 1.7 | ||
| 652 | */ | ||
| 653 | public static Border createStrokeBorder(BasicStroke stroke) { | ||
| 654 | return new StrokeBorder(stroke); | ||
| 655 | } | ||
| 656 | |||
| 657 | /** | ||
| 658 | * Creates a border of the specified {@code stroke} and {@code paint}. | ||
| 659 | * If the specified {@code paint} is {@code null}, | ||
| 660 | * the component's foreground color will be used to render the border. | ||
| 661 | * | ||
| 662 | * @param stroke the {@link BasicStroke} object used to stroke a shape | ||
| 663 | * @param paint the {@link Paint} object used to generate a color | ||
| 664 | * @return the {@code Border} object | ||
| 665 | * | ||
| 666 | * @throws NullPointerException if the specified {@code stroke} is {@code null} | ||
| 667 | * | ||
| 668 | * @since 1.7 | ||
| 669 | */ | ||
| 670 | public static Border createStrokeBorder(BasicStroke stroke, Paint paint) { | ||
| 671 | return new StrokeBorder(stroke, paint); | ||
| 672 | } | ||
| 673 | |||
| 674 | //// DashedBorder ////////////////////////////////////////////////////////////// | ||
| 675 | //////////////////////////////////////////////////////////////////////////////// | ||
| 676 | |||
| 677 | private static Border sharedDashedBorder; | ||
| 678 | |||
| 679 | /** | ||
| 680 | * Creates a dashed border of the specified {@code paint}. | ||
| 681 | * If the specified {@code paint} is {@code null}, | ||
| 682 | * the component's foreground color will be used to render the border. | ||
| 683 | * The width of a dash line is equal to {@code 1}. | ||
| 684 | * The relative length of a dash line and | ||
| 685 | * the relative spacing between dash lines are equal to {@code 1}. | ||
| 686 | * A dash line is not rounded. | ||
| 687 | * | ||
| 688 | * @param paint the {@link Paint} object used to generate a color | ||
| 689 | * @return the {@code Border} object | ||
| 690 | * | ||
| 691 | * @since 1.7 | ||
| 692 | */ | ||
| 693 | public static Border createDashedBorder(Paint paint) { | ||
| 694 | return createDashedBorder(paint, 1.0f, 1.0f, 1.0f, false); | ||
| 695 | } | ||
| 696 | |||
| 697 | /** | ||
| 698 | * Creates a dashed border of the specified {@code paint}, | ||
| 699 | * relative {@code length}, and relative {@code spacing}. | ||
| 700 | * If the specified {@code paint} is {@code null}, | ||
| 701 | * the component's foreground color will be used to render the border. | ||
| 702 | * The width of a dash line is equal to {@code 1}. | ||
| 703 | * A dash line is not rounded. | ||
| 704 | * | ||
| 705 | * @param paint the {@link Paint} object used to generate a color | ||
| 706 | * @param length the relative length of a dash line | ||
| 707 | * @param spacing the relative spacing between dash lines | ||
| 708 | * @return the {@code Border} object | ||
| 709 | * | ||
| 710 | * @throws IllegalArgumentException if the specified {@code length} is less than {@code 1}, or | ||
| 711 | * if the specified {@code spacing} is less than {@code 0} | ||
| 712 | * @since 1.7 | ||
| 713 | */ | ||
| 714 | public static Border createDashedBorder(Paint paint, float length, float spacing) { | ||
| 715 | return createDashedBorder(paint, 1.0f, length, spacing, false); | ||
| 716 | } | ||
| 717 | |||
| 718 | /** | ||
| 719 | * Creates a dashed border of the specified {@code paint}, {@code thickness}, | ||
| 720 | * line shape, relative {@code length}, and relative {@code spacing}. | ||
| 721 | * If the specified {@code paint} is {@code null}, | ||
| 722 | * the component's foreground color will be used to render the border. | ||
| 723 | * | ||
| 724 | * @param paint the {@link Paint} object used to generate a color | ||
| 725 | * @param thickness the width of a dash line | ||
| 726 | * @param length the relative length of a dash line | ||
| 727 | * @param spacing the relative spacing between dash lines | ||
| 728 | * @param rounded whether or not line ends should be round | ||
| 729 | * @return the {@code Border} object | ||
| 730 | * | ||
| 731 | * @throws IllegalArgumentException if the specified {@code thickness} is less than {@code 1}, or | ||
| 732 | * if the specified {@code length} is less than {@code 1}, or | ||
| 733 | * if the specified {@code spacing} is less than {@code 0} | ||
| 734 | * @since 1.7 | ||
| 735 | */ | ||
| 736 | public static Border createDashedBorder(Paint paint, float thickness, float length, float spacing, boolean rounded) { | ||
| 737 | boolean shared = !rounded && (paint == null) && (thickness == 1.0f) && (length == 1.0f) && (spacing == 1.0f); | ||
| 738 | if (shared && (sharedDashedBorder != null)) { | ||
| 739 | return sharedDashedBorder; | ||
| 740 | } | ||
| 741 | if (thickness < 1.0f) { | ||
| 742 | throw new IllegalArgumentException("thickness is less than 1"); | ||
| 743 | } | ||
| 744 | if (length < 1.0f) { | ||
| 745 | throw new IllegalArgumentException("length is less than 1"); | ||
| 746 | } | ||
| 747 | if (spacing < 0.0f) { | ||
| 748 | throw new IllegalArgumentException("spacing is less than 0"); | ||
| 749 | } | ||
| 750 | int cap = rounded ? BasicStroke.CAP_ROUND : BasicStroke.CAP_SQUARE; | ||
| 751 | int join = rounded ? BasicStroke.JOIN_ROUND : BasicStroke.JOIN_MITER; | ||
| 752 | float[] array = { thickness * (length - 1.0f), thickness * (spacing + 1.0f) }; | ||
| 753 | Border border = createStrokeBorder(new BasicStroke(thickness, cap, join, thickness * 2.0f, array, 0.0f), paint); | ||
| 754 | if (shared) { | ||
| 755 | sharedDashedBorder = border; | ||
| 756 | } | ||
| 757 | return border; | ||
| 758 | } | ||
| 759 | } | ||