001 /* SoftBevelBorder.java -- 002 Copyright (C) 2003 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package javax.swing.border; 039 040 import java.awt.Color; 041 import java.awt.Component; 042 import java.awt.Graphics; 043 import java.awt.Insets; 044 045 046 /** 047 * A rectangular, three pixel thick border that looks like a BevelBorder 048 * with slightly softened corners. 049 * 050 * <p>Like BevelBorder, SoftBevelBorder has a highlight and a shadow 051 * color. In the raised variant, the highlight color is used for the 052 * top and left edges, and the shadow color is used for the bottom and 053 * right edge. In the lowered variant, color usage is reversed. For 054 * an image, see the documentation of the individual constructors. 055 * 056 * @author Sascha Brawer (brawer@dandelis.ch) 057 */ 058 public class SoftBevelBorder extends BevelBorder 059 { 060 /** 061 * Determined using the <code>serialver</code> tool 062 * of Sun JDK 1.4.1_01 on GNU/Linux 2.4.20. Interestingly, 063 * the Apple/Sun JDK 1.3.1 on MacOS X 10.1.5 gives a different 064 * value, namely -6658357140774549493L. 065 */ 066 static final long serialVersionUID = 5248789787305979975L; 067 068 069 /** 070 * Constructs a SoftBevelBorder whose colors will be derived from the 071 * background of the enclosed component. The background color is 072 * retrieved each time the border is painted, so a SoftBevelBorder 073 * constructed by this method will automatically reflect a change 074 * to the component’s background color. 075 * 076 * <p><img src="doc-files/SoftBevelBorder-1.png" width="500" height="200" 077 * alt="[An illustration showing raised and lowered SoftBevelBorders]" /> 078 * 079 * @param bevelType the desired appearance of the border. The value 080 * must be either {@link BevelBorder#RAISED} 081 * or {@link BevelBorder#LOWERED}. 082 * 083 * @throws IllegalArgumentException if <code>bevelType</code> has 084 * an unsupported value. 085 */ 086 public SoftBevelBorder(int bevelType) 087 { 088 super(bevelType); 089 } 090 091 092 /** 093 * Constructs a SoftBevelBorder given its appearance type and two 094 * colors for its highlight and shadow. 095 * 096 * <p><img src="doc-files/SoftBevelBorder-2.png" width="500" height="150" 097 * alt="[An illustration showing SoftBevelBorders that were 098 * constructed with this method]" /> 099 * 100 * @param bevelType the desired appearance of the border. The value 101 * must be either {@link BevelBorder#RAISED} or {@link 102 * BevelBorder#LOWERED}. 103 * 104 * @param highlight the color that will be used for the inner side 105 * of the highlighted edges (top and left if if 106 * <code>bevelType</code> is {@link BevelBorder#RAISED}; 107 * bottom and right otherwise). The color for the outer side 108 * is a brightened version of this color. 109 * 110 * @param shadow the color that will be used for the outer side of 111 * the shadowed edges (bottom and right if 112 * <code>bevelType</code> is {@link BevelBorder#RAISED}; top 113 * and left otherwise). The color for the inner side is a 114 * brightened version of this color. 115 * 116 * @throws IllegalArgumentException if <code>bevelType</code> has an 117 * unsupported value. 118 * 119 * @throws NullPointerException if <code>highlight</code> or 120 * <code>shadow</code> is <code>null</code>. 121 * 122 * @see java.awt.Color#brighter() 123 */ 124 public SoftBevelBorder(int bevelType, Color highlight, Color shadow) 125 { 126 this(bevelType, 127 /* highlightOuter */ highlight.brighter(), 128 /* highlightInner */ highlight, 129 /* shadowOuter */ shadow, 130 /* shadowInner */ shadow.brighter()); 131 } 132 133 134 /** 135 * Constructs a SoftBevelBorder given its appearance type and all 136 * colors. 137 * 138 * <p><img src="doc-files/SoftBevelBorder-3.png" width="500" height="150" 139 * alt="[An illustration showing SoftBevelBorders that were 140 * constructed with this method]" /> 141 * 142 * @param bevelType the desired appearance of the border. The value 143 * must be either {@link BevelBorder#RAISED} or {@link 144 * BevelBorder#LOWERED}. 145 * 146 * @param highlightOuter the color that will be used for the outer 147 * side of the highlighted edges (top and left if 148 * <code>bevelType</code> is {@link BevelBorder#RAISED}; 149 * bottom and right otherwise). 150 * 151 * @param highlightInner the color that will be used for the inner 152 * side of the highlighted edges. 153 * 154 * @param shadowOuter the color that will be used for the outer side 155 * of the shadowed edges (bottom and right if 156 * <code>bevelType</code> is {@link BevelBorder#RAISED}; top 157 * and left otherwise). 158 * 159 * @param shadowInner the color that will be used for the inner 160 * side of the shadowed edges. 161 * 162 * @throws IllegalArgumentException if <code>bevelType</code> has 163 * an unsupported value. 164 * 165 * @throws NullPointerException if one of the passed colors 166 * is <code>null</code>. 167 */ 168 public SoftBevelBorder(int bevelType, 169 Color highlightOuter, Color highlightInner, 170 Color shadowOuter, Color shadowInner) 171 { 172 super(bevelType, 173 highlightOuter, highlightInner, 174 shadowOuter, shadowInner); 175 } 176 177 178 /** 179 * Paints the border for a given component. 180 * 181 * @param c the component whose border is to be painted. 182 * @param g the graphics for painting. 183 * @param x the horizontal position for painting the border. 184 * @param y the vertical position for painting the border. 185 * @param width the width of the available area for painting the border. 186 * @param height the height of the available area for painting the border. 187 */ 188 public void paintBorder(Component c, Graphics g, 189 int x, int y, int width, int height) 190 { 191 switch (bevelType) 192 { 193 case RAISED: 194 paintSoftBevel(g, x, y, width, height, 195 getHighlightOuterColor(c), getHighlightInnerColor(c), 196 getShadowInnerColor(c), getShadowOuterColor(c)); 197 break; 198 199 case LOWERED: 200 paintSoftBevel(g, x, y, width, height, 201 getShadowOuterColor(c), getShadowInnerColor(c), 202 getHighlightInnerColor(c), getHighlightOuterColor(c)); 203 break; 204 } 205 } 206 207 208 /** 209 * Measures the width of this border. 210 * 211 * @param c the component whose border is to be measured. 212 * 213 * @return an Insets object whose <code>left</code>, <code>right</code>, 214 * <code>top</code> and <code>bottom</code> fields indicate the 215 * width of the border at the respective edge. 216 * 217 * @see #getBorderInsets(java.awt.Component, java.awt.Insets) 218 */ 219 public Insets getBorderInsets(Component c) 220 { 221 return new Insets(3, 3, 3, 3); 222 } 223 224 225 /** 226 * Measures the width of this border, storing the results into a 227 * pre-existing Insets object. 228 * 229 * @param insets an Insets object for holding the result values. 230 * After invoking this method, the <code>left</code>, 231 * <code>right</code>, <code>top</code> and 232 * <code>bottom</code> fields indicate the width of the 233 * border at the respective edge. 234 * 235 * @return the same object that was passed for <code>insets</code>. 236 * 237 * @see #getBorderInsets(Component) 238 */ 239 public Insets getBorderInsets(Component c, Insets insets) 240 { 241 insets.left = insets.right = insets.top = insets.bottom = 3; 242 return insets; 243 } 244 245 246 /** 247 * Determines whether this border fills every pixel in its area 248 * when painting. 249 * 250 * <p>The enlarged view (see documentation for constructors) shows 251 * that a SoftBevelBorder does not paint all pixels. Therefore, 252 * this method always returns <code>false</code>. 253 * 254 * @return <code>false</code>. 255 */ 256 public boolean isBorderOpaque() 257 { 258 return false; 259 } 260 261 262 /** 263 * Paints a soft bevel in four colors. 264 * 265 * <pre> 266 * +++++++++++. 267 * ++.........# + = color a 268 * +.. # . = color b 269 * +. # X = color c 270 * .. X# # = color d 271 * . ##########</pre> 272 * 273 * @param g the graphics for painting. 274 * @param x the horizontal position for painting the border. 275 * @param y the vertical position for painting the border. 276 * @param width the width of the available area for painting the border. 277 * @param height the height of the available area for painting the border. 278 * @param a the color for the outer side of the top and left edges. 279 * @param b the color for the inner side of the top and left edges. 280 * @param c the color for the inner side of the bottom and right edges. 281 * @param d the color for the outer side of the bottom and right edges. 282 */ 283 private static void paintSoftBevel(Graphics g, 284 int x, int y, int width, int height, 285 Color a, Color b, Color c, Color d) 286 { 287 Color oldColor; 288 289 oldColor = g.getColor(); 290 g.translate(x, y); 291 width = width - 1; 292 height = height - 1; 293 294 try 295 { 296 /* To understand this code, it might be helpful to look at the 297 * images that are included with the JavaDoc, especially 298 * SoftBevelBorder-3.png. They are located in the "doc-files" 299 * subdirectory. 300 */ 301 g.setColor(a); 302 g.drawLine(0, 0, width - 1, 0); // a, horizontal 303 g.drawLine(0, 1, 2, 1); // a, horizontal 304 g.drawLine(0, 2, 0, height - 1); // a, vertical 305 306 g.setColor(b); 307 g.drawLine(width, 0, width, 0); // b, horizontal 308 g.drawLine(2, 1, width - 1, 1); // b, horizontal 309 g.drawLine(1, 2, 2, 2); // b, horizontal 310 g.drawLine(1, 3, 1, height - 1); // b, vertical 311 g.drawLine(0, height - 1, 0, height); // b, vertical 312 313 g.setColor(c); 314 g.drawLine(width - 1, height - 1, // c, one pixel 315 width - 1, height - 1); 316 317 g.setColor(d); 318 g.drawLine(2, height, width, height); // d, horizontal 319 g.drawLine(width, 2, width, height - 1); // d, vertical 320 } 321 finally 322 { 323 g.translate(-x, -y); 324 g.setColor(oldColor); 325 } 326 } 327 }