Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / swing / border / EtchedBorder.java
blob22882b78cb1fbdb7d87698546f3216b2da874102
1 /* EtchedBorder.java --
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.border;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Graphics;
44 import java.awt.Insets;
47 /**
48 * A border that looks like an engraving etched into the background
49 * surface, or (in its raised variant) coming out of the surface
50 * plane. Using different constructors, it is possible to either
51 * explicitly specify the border colors, or to let the colors derive
52 * from the background color of the enclosed Component.
54 * <p><img src="doc-files/EtchedBorder-1.png" width="500" height="200"
55 * alt="[An illustration of the two EtchedBorder variants]" />
57 * @author Sascha Brawer (brawer@dandelis.ch)
59 public class EtchedBorder extends AbstractBorder
61 /**
62 * Determined using the <code>serialver</code> tool
63 * of Apple/Sun JDK 1.3.1 on MacOS X 10.1.5.
65 static final long serialVersionUID = 4001244046866360638L;
68 /**
69 * Indicates that the border appears as coming out of the
70 * background.
72 public static final int RAISED = 0;
75 /**
76 * Indicates that the border appears as engraved into the
77 * background.
79 public static final int LOWERED = 1;
82 /**
83 * The type of this EtchedBorder, which is either {@link #RAISED}
84 * or {@link #LOWERED}.
86 protected int etchType;
89 /**
90 * The highlight color, or <code>null</code> to indicate that the
91 * color shall be derived from the background of the enclosed
92 * component.
94 protected Color highlight;
97 /**
98 * The shadow color, or <code>null</code> to indicate that the
99 * color shall be derived from the background of the enclosed
100 * component.
102 protected Color shadow;
106 * Constructs a lowered EtchedBorder. The colors will be derived
107 * from the background color of the enclosed Component when the
108 * border gets painted.
110 public EtchedBorder()
112 this(LOWERED);
117 * Constructs an EtchedBorder with the specified appearance. The
118 * colors will be derived from the background color of the enclosed
119 * Component when the border gets painted.
121 * <p><img src="doc-files/EtchedBorder-1.png" width="500" height="200"
122 * alt="[An illustration of the two EtchedBorder variants]" />
124 * @param etchType the desired appearance of the border. The value
125 * must be either {@link #RAISED} or {@link #LOWERED}.
127 * @throws IllegalArgumentException if <code>etchType</code> has
128 * an unsupported value.
130 public EtchedBorder(int etchType)
132 if ((etchType != RAISED) && (etchType != LOWERED))
133 throw new IllegalArgumentException();
135 this.etchType = etchType;
137 /* The highlight and shadow fields already have a null value
138 * when the constructor gets called, so there is no need to
139 * assign a value here.
145 * Constructs a lowered EtchedBorder, explicitly selecting the
146 * colors that will be used for highlight and shadow.
148 * @param highlight the color that will be used for painting
149 * the highlight part of the border.
151 * @param shadow the color that will be used for painting
152 * the shadow part of the border.
154 * @see #EtchedBorder(int, Color, Color)
156 public EtchedBorder(Color highlight, Color shadow)
158 this(LOWERED, highlight, shadow);
163 * Constructs an EtchedBorder with the specified appearance,
164 * explicitly selecting the colors that will be used for
165 * highlight and shadow.
167 * <p><img src="doc-files/EtchedBorder-2.png" width="500" height="200"
168 * alt="[An illustration that shows which pixels get painted
169 * in what color]" />
171 * @param etchType the desired appearance of the border. The value
172 * must be either {@link #RAISED} or {@link #LOWERED}.
174 * @param highlight the color that will be used for painting
175 * the highlight part of the border.
177 * @param shadow the color that will be used for painting
178 * the shadow part of the border.
180 * @throws IllegalArgumentException if <code>etchType</code> has
181 * an unsupported value.
183 public EtchedBorder(int etchType, Color highlight, Color shadow)
185 this(etchType); // Checks the validity of the value.
186 this.highlight = highlight;
187 this.shadow = shadow;
192 * Paints the border for a given component.
194 * @param c the component whose border is to be painted.
195 * @param g the graphics for painting.
196 * @param x the horizontal position for painting the border.
197 * @param y the vertical position for painting the border.
198 * @param width the width of the available area for painting the border.
199 * @param height the height of the available area for painting the border.
201 public void paintBorder(Component c, Graphics g, int x, int y, int width,
202 int height)
204 switch (etchType)
206 case RAISED:
207 paintEtchedBorder(g, x, y, width, height,
208 getHighlightColor(c), getShadowColor(c));
209 break;
211 case LOWERED:
212 paintEtchedBorder(g, x, y, width, height,
213 getShadowColor(c), getHighlightColor(c));
214 break;
220 * Measures the width of this border.
222 * @param c the component whose border is to be measured.
224 * @return an Insets object whose <code>left</code>, <code>right</code>,
225 * <code>top</code> and <code>bottom</code> fields indicate the
226 * width of the border at the respective edge.
228 * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
230 public Insets getBorderInsets(Component c)
232 return new Insets(2, 2, 2, 2);
237 * Measures the width of this border, storing the results into a
238 * pre-existing Insets object.
240 * @param insets an Insets object for holding the result values.
241 * After invoking this method, the <code>left</code>,
242 * <code>right</code>, <code>top</code> and
243 * <code>bottom</code> fields indicate the width of the
244 * border at the respective edge.
246 * @return the same object that was passed for <code>insets</code>.
248 * @see #getBorderInsets(Component)
250 public Insets getBorderInsets(Component c, Insets insets)
252 insets.left = insets.right = insets.top = insets.bottom = 2;
253 return insets;
258 * Determines whether this border fills every pixel in its area
259 * when painting.
261 * <p>If the border colors are derived from the background color of
262 * the enclosed component, the result is <code>true</code> because
263 * the derivation method always returns opaque colors. Otherwise,
264 * the result depends on the opacity of the individual colors.
266 * @return <code>true</code> if the border is fully opaque, or
267 * <code>false</code> if some pixels of the background
268 * can shine through the border.
270 public boolean isBorderOpaque()
272 // If the colors are to be derived from the enclosed Component's
273 // background color, the border is guaranteed to be fully opaque
274 // because Color.brighten() and Color.darken() always return an
275 // opaque color.
276 return
277 ((highlight == null) || (highlight.getAlpha() == 255))
278 && ((shadow == null) || (shadow.getAlpha() == 255));
282 * Returns the appearance of this EtchedBorder, which is either
283 * {@link #RAISED} or {@link #LOWERED}.
285 public int getEtchType()
287 return etchType;
292 * Determines the color that will be used for highlighted parts when
293 * painting the border around a given component. If a highlight
294 * color has been specified upon constructing the border, that color
295 * is returned. Otherwise, the background color of the enclosed
296 * component is brightened.
298 * @param c the component enclosed by this border.
300 * @see java.awt.Component#getBackground()
301 * @see java.awt.Color#brighter()
303 public Color getHighlightColor(Component c)
305 if (highlight != null)
306 return highlight;
307 else
308 return c.getBackground().brighter();
312 * Returns the color that will be used for highlighted parts when
313 * painting the border, or <code>null</code> if that color will be
314 * derived from the background of the enclosed Component.
316 public Color getHighlightColor()
318 return highlight;
323 * Determines the color that will be used for shadowed parts when
324 * painting the border around a given component. If a shadow color
325 * has been specified upon constructing the border, that color is
326 * returned. Otherwise, the background color of the enclosed
327 * component is darkened.
329 * @param c the component enclosed by this border.
331 * @see java.awt.Component#getBackground()
332 * @see java.awt.Color#darker()
334 public Color getShadowColor(Component c)
336 if (shadow != null)
337 return shadow;
338 else
339 return c.getBackground().darker();
344 * Returns the color that will be used for shadowed parts when
345 * painting the border, or <code>null</code> if that color will be
346 * derived from the background of the enclosed Component.
348 public Color getShadowColor()
350 return shadow;
355 * Paints a two-pixel etching in two colors.
357 * <pre>
358 * +++++++++++.
359 * +.........+. + = color a
360 * +. +. . = color b
361 * +. +.
362 * +++++++++++.
363 * ............</pre>
365 * @param g the graphics for painting.
366 * @param x the horizontal position for painting the border.
367 * @param y the vertical position for painting the border.
368 * @param width the width of the available area for painting the border.
369 * @param height the height of the available area for painting the border.
370 * @param a one of the two colors.
371 * @param b the second of the two colors.
373 private static void paintEtchedBorder(Graphics g, int x, int y, int width,
374 int height, Color a, Color b)
376 Color oldColor;
378 oldColor = g.getColor();
379 g.translate(x, y);
380 width = width - 1;
381 height = height - 1;
385 // To understand this code, it might be helpful to look at the
386 // images that are included with the JavaDoc. They are located
387 // in the "doc-files" subdirectory. EtchedBorder-2.png might
388 // be especially informative.
389 g.setColor(a);
390 g.drawRect(0, 0, width - 1, height - 1);
392 g.setColor(b);
393 g.drawLine(1, 1, width - 2, 1); // top edge
394 g.drawLine(1, 2, 1, height - 2); // left edge
395 g.drawLine(0, height, width, height); // bottom edge
396 g.drawLine(width, 0, width, height - 1); // right edge
398 finally
400 g.translate(-x, -y);
401 g.setColor(oldColor);