Home » openjdk-7 » javax » swing » border » [javadoc | source]

    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.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.beans.ConstructorProperties;
   32   
   33   /**
   34    * A class which implements a simple two-line bevel border.
   35    * <p>
   36    * <strong>Warning:</strong>
   37    * Serialized objects of this class will not be compatible with
   38    * future Swing releases. The current serialization support is
   39    * appropriate for short term storage or RMI between applications running
   40    * the same version of Swing.  As of 1.4, support for long term storage
   41    * of all JavaBeans<sup><font size="-2">TM</font></sup>
   42    * has been added to the <code>java.beans</code> package.
   43    * Please see {@link java.beans.XMLEncoder}.
   44    *
   45    * @author David Kloba
   46    */
   47   public class BevelBorder extends AbstractBorder
   48   {
   49       /** Raised bevel type. */
   50       public static final int RAISED  = 0;
   51       /** Lowered bevel type. */
   52       public static final int LOWERED = 1;
   53   
   54       protected int bevelType;
   55       protected Color highlightOuter;
   56       protected Color highlightInner;
   57       protected Color shadowInner;
   58       protected Color shadowOuter;
   59   
   60       /**
   61        * Creates a bevel border with the specified type and whose
   62        * colors will be derived from the background color of the
   63        * component passed into the paintBorder method.
   64        * @param bevelType the type of bevel for the border
   65        */
   66       public BevelBorder(int bevelType) {
   67           this.bevelType = bevelType;
   68       }
   69   
   70       /**
   71        * Creates a bevel border with the specified type, highlight and
   72        * shadow colors.
   73        * @param bevelType the type of bevel for the border
   74        * @param highlight the color to use for the bevel highlight
   75        * @param shadow the color to use for the bevel shadow
   76        */
   77       public BevelBorder(int bevelType, Color highlight, Color shadow) {
   78           this(bevelType, highlight.brighter(), highlight, shadow, shadow.brighter());
   79       }
   80   
   81       /**
   82        * Creates a bevel border with the specified type, highlight and
   83        * shadow colors.
   84        *
   85        * @param bevelType the type of bevel for the border
   86        * @param highlightOuterColor the color to use for the bevel outer highlight
   87        * @param highlightInnerColor the color to use for the bevel inner highlight
   88        * @param shadowOuterColor the color to use for the bevel outer shadow
   89        * @param shadowInnerColor the color to use for the bevel inner shadow
   90        */
   91       @ConstructorProperties({"bevelType", "highlightOuterColor", "highlightInnerColor", "shadowOuterColor", "shadowInnerColor"})
   92       public BevelBorder(int bevelType, Color highlightOuterColor,
   93                          Color highlightInnerColor, Color shadowOuterColor,
   94                          Color shadowInnerColor) {
   95           this(bevelType);
   96           this.highlightOuter = highlightOuterColor;
   97           this.highlightInner = highlightInnerColor;
   98           this.shadowOuter = shadowOuterColor;
   99           this.shadowInner = shadowInnerColor;
  100       }
  101   
  102       /**
  103        * Paints the border for the specified component with the specified
  104        * position and size.
  105        * @param c the component for which this border is being painted
  106        * @param g the paint graphics
  107        * @param x the x position of the painted border
  108        * @param y the y position of the painted border
  109        * @param width the width of the painted border
  110        * @param height the height of the painted border
  111        */
  112       public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  113           if (bevelType == RAISED) {
  114                paintRaisedBevel(c, g, x, y, width, height);
  115   
  116           } else if (bevelType == LOWERED) {
  117                paintLoweredBevel(c, g, x, y, width, height);
  118           }
  119       }
  120   
  121       /**
  122        * Reinitialize the insets parameter with this Border's current Insets.
  123        * @param c the component for which this border insets value applies
  124        * @param insets the object to be reinitialized
  125        */
  126       public Insets getBorderInsets(Component c, Insets insets) {
  127           insets.set(2, 2, 2, 2);
  128           return insets;
  129       }
  130   
  131       /**
  132        * Returns the outer highlight color of the bevel border
  133        * when rendered on the specified component.  If no highlight
  134        * color was specified at instantiation, the highlight color
  135        * is derived from the specified component's background color.
  136        * @param c the component for which the highlight may be derived
  137        * @since 1.3
  138        */
  139       public Color getHighlightOuterColor(Component c)   {
  140           Color highlight = getHighlightOuterColor();
  141           return highlight != null? highlight :
  142                                          c.getBackground().brighter().brighter();
  143       }
  144   
  145       /**
  146        * Returns the inner highlight color of the bevel border
  147        * when rendered on the specified component.  If no highlight
  148        * color was specified at instantiation, the highlight color
  149        * is derived from the specified component's background color.
  150        * @param c the component for which the highlight may be derived
  151        * @since 1.3
  152        */
  153       public Color getHighlightInnerColor(Component c)   {
  154           Color highlight = getHighlightInnerColor();
  155           return highlight != null? highlight :
  156                                          c.getBackground().brighter();
  157       }
  158   
  159       /**
  160        * Returns the inner shadow color of the bevel border
  161        * when rendered on the specified component.  If no shadow
  162        * color was specified at instantiation, the shadow color
  163        * is derived from the specified component's background color.
  164        * @param c the component for which the shadow may be derived
  165        * @since 1.3
  166        */
  167       public Color getShadowInnerColor(Component c)      {
  168           Color shadow = getShadowInnerColor();
  169           return shadow != null? shadow :
  170                                       c.getBackground().darker();
  171       }
  172   
  173       /**
  174        * Returns the outer shadow color of the bevel border
  175        * when rendered on the specified component.  If no shadow
  176        * color was specified at instantiation, the shadow color
  177        * is derived from the specified component's background color.
  178        * @param c the component for which the shadow may be derived
  179        * @since 1.3
  180        */
  181       public Color getShadowOuterColor(Component c)      {
  182           Color shadow = getShadowOuterColor();
  183           return shadow != null? shadow :
  184                                       c.getBackground().darker().darker();
  185       }
  186   
  187       /**
  188        * Returns the outer highlight color of the bevel border.
  189        * Will return null if no highlight color was specified
  190        * at instantiation.
  191        * @since 1.3
  192        */
  193       public Color getHighlightOuterColor()   {
  194           return highlightOuter;
  195       }
  196   
  197       /**
  198        * Returns the inner highlight color of the bevel border.
  199        * Will return null if no highlight color was specified
  200        * at instantiation.
  201        * @since 1.3
  202        */
  203       public Color getHighlightInnerColor()   {
  204           return highlightInner;
  205       }
  206   
  207       /**
  208        * Returns the inner shadow color of the bevel border.
  209        * Will return null if no shadow color was specified
  210        * at instantiation.
  211        * @since 1.3
  212        */
  213       public Color getShadowInnerColor()      {
  214           return shadowInner;
  215       }
  216   
  217       /**
  218        * Returns the outer shadow color of the bevel border.
  219        * Will return null if no shadow color was specified
  220        * at instantiation.
  221        * @since 1.3
  222        */
  223       public Color getShadowOuterColor()      {
  224           return shadowOuter;
  225       }
  226   
  227       /**
  228        * Returns the type of the bevel border.
  229        */
  230       public int getBevelType()       {
  231           return bevelType;
  232       }
  233   
  234       /**
  235        * Returns whether or not the border is opaque.
  236        */
  237       public boolean isBorderOpaque() { return true; }
  238   
  239       protected void paintRaisedBevel(Component c, Graphics g, int x, int y,
  240                                       int width, int height)  {
  241           Color oldColor = g.getColor();
  242           int h = height;
  243           int w = width;
  244   
  245           g.translate(x, y);
  246   
  247           g.setColor(getHighlightOuterColor(c));
  248           g.drawLine(0, 0, 0, h-2);
  249           g.drawLine(1, 0, w-2, 0);
  250   
  251           g.setColor(getHighlightInnerColor(c));
  252           g.drawLine(1, 1, 1, h-3);
  253           g.drawLine(2, 1, w-3, 1);
  254   
  255           g.setColor(getShadowOuterColor(c));
  256           g.drawLine(0, h-1, w-1, h-1);
  257           g.drawLine(w-1, 0, w-1, h-2);
  258   
  259           g.setColor(getShadowInnerColor(c));
  260           g.drawLine(1, h-2, w-2, h-2);
  261           g.drawLine(w-2, 1, w-2, h-3);
  262   
  263           g.translate(-x, -y);
  264           g.setColor(oldColor);
  265   
  266       }
  267   
  268       protected void paintLoweredBevel(Component c, Graphics g, int x, int y,
  269                                           int width, int height)  {
  270           Color oldColor = g.getColor();
  271           int h = height;
  272           int w = width;
  273   
  274           g.translate(x, y);
  275   
  276           g.setColor(getShadowInnerColor(c));
  277           g.drawLine(0, 0, 0, h-1);
  278           g.drawLine(1, 0, w-1, 0);
  279   
  280           g.setColor(getShadowOuterColor(c));
  281           g.drawLine(1, 1, 1, h-2);
  282           g.drawLine(2, 1, w-2, 1);
  283   
  284           g.setColor(getHighlightOuterColor(c));
  285           g.drawLine(1, h-1, w-1, h-1);
  286           g.drawLine(w-1, 1, w-1, h-2);
  287   
  288           g.setColor(getHighlightInnerColor(c));
  289           g.drawLine(2, h-2, w-2, h-2);
  290           g.drawLine(w-2, 2, w-2, h-3);
  291   
  292           g.translate(-x, -y);
  293           g.setColor(oldColor);
  294   
  295       }
  296   
  297   }

Home » openjdk-7 » javax » swing » border » [javadoc | source]