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

    1   /*
    2    * Copyright (c) 2010, 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.BasicStroke;
   28   import java.awt.Component;
   29   import java.awt.Graphics;
   30   import java.awt.Graphics2D;
   31   import java.awt.Insets;
   32   import java.awt.Paint;
   33   import java.awt.RenderingHints;
   34   import java.awt.geom.Rectangle2D;
   35   import java.beans.ConstructorProperties;
   36   
   37   /**
   38    * A class which implements a border of an arbitrary stroke.
   39    * <p>
   40    * <strong>Warning:</strong>
   41    * Serialized objects of this class will not be compatible with
   42    * future Swing releases. The current serialization support is
   43    * appropriate for short term storage or RMI
   44    * between applications running the same version of Swing.
   45    * As of 1.4, support for long term storage of all JavaBeans&trade;
   46    * has been added to the <code>java.beans</code> package.
   47    * Please see {@link java.beans.XMLEncoder}.
   48    *
   49    * @author Sergey A. Malenkov
   50    *
   51    * @since 1.7
   52    */
   53   public class StrokeBorder extends AbstractBorder {
   54       private final BasicStroke stroke;
   55       private final Paint paint;
   56   
   57       /**
   58        * Creates a border of the specified {@code stroke}.
   59        * The component's foreground color will be used to render the border.
   60        *
   61        * @param stroke  the {@link BasicStroke} object used to stroke a shape
   62        *
   63        * @throws NullPointerException if the specified {@code stroke} is {@code null}
   64        */
   65       public StrokeBorder(BasicStroke stroke) {
   66           this(stroke, null);
   67       }
   68   
   69       /**
   70        * Creates a border of the specified {@code stroke} and {@code paint}.
   71        * If the specified {@code paint} is {@code null},
   72        * the component's foreground color will be used to render the border.
   73        *
   74        * @param stroke  the {@link BasicStroke} object used to stroke a shape
   75        * @param paint   the {@link Paint} object used to generate a color
   76        *
   77        * @throws NullPointerException if the specified {@code stroke} is {@code null}
   78        */
   79       @ConstructorProperties({ "stroke", "paint" })
   80       public StrokeBorder(BasicStroke stroke, Paint paint) {
   81           if (stroke == null) {
   82               throw new NullPointerException("border's stroke");
   83           }
   84           this.stroke = stroke;
   85           this.paint = paint;
   86       }
   87   
   88       /**
   89        * Paints the border for the specified component
   90        * with the specified position and size.
   91        * If the border was not specified with a {@link Paint} object,
   92        * the component's foreground color will be used to render the border.
   93        * If the component's foreground color is not available,
   94        * the default color of the {@link Graphics} object will be used.
   95        *
   96        * @param c       the component for which this border is being painted
   97        * @param g       the paint graphics
   98        * @param x       the x position of the painted border
   99        * @param y       the y position of the painted border
  100        * @param width   the width of the painted border
  101        * @param height  the height of the painted border
  102        *
  103        * @throws NullPointerException if the specified {@code g} is {@code null}
  104        */
  105       @Override
  106       public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  107           float size = this.stroke.getLineWidth();
  108           if (size > 0.0f) {
  109               g = g.create();
  110               if (g instanceof Graphics2D) {
  111                   Graphics2D g2d = (Graphics2D) g;
  112                   g2d.setStroke(this.stroke);
  113                   g2d.setPaint(this.paint != null ? this.paint : c == null ? null : c.getForeground());
  114                   g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  115                                        RenderingHints.VALUE_ANTIALIAS_ON);
  116                   g2d.draw(new Rectangle2D.Float(x + size / 2, y + size / 2, width - size, height - size));
  117               }
  118               g.dispose();
  119           }
  120       }
  121   
  122       /**
  123        * Reinitializes the {@code insets} parameter
  124        * with this border's current insets.
  125        * Every inset is the smallest (closest to negative infinity) integer value
  126        * that is greater than or equal to the line width of the stroke
  127        * that is used to paint the border.
  128        *
  129        * @param c       the component for which this border insets value applies
  130        * @param insets  the {@code Insets} object to be reinitialized
  131        * @return the reinitialized {@code insets} parameter
  132        *
  133        * @throws NullPointerException if the specified {@code insets} is {@code null}
  134        *
  135        * @see Math#ceil
  136        */
  137       @Override
  138       public Insets getBorderInsets(Component c, Insets insets) {
  139           int size = (int) Math.ceil(this.stroke.getLineWidth());
  140           insets.set(size, size, size, size);
  141           return insets;
  142       }
  143   
  144       /**
  145        * Returns the {@link BasicStroke} object used to stroke a shape
  146        * during the border rendering.
  147        *
  148        * @return the {@link BasicStroke} object
  149        */
  150       public BasicStroke getStroke() {
  151           return this.stroke;
  152       }
  153   
  154       /**
  155        * Returns the {@link Paint} object used to generate a color
  156        * during the border rendering.
  157        *
  158        * @return the {@link Paint} object or {@code null}
  159        *         if the {@code paint} parameter is not set
  160        */
  161       public Paint getPaint() {
  162           return this.paint;
  163       }
  164   }

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