1 /*
   2  * Copyright (c) 2010, 2014, 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 @SuppressWarnings("serial") // Same-version serialization only
  54 public class StrokeBorder extends AbstractBorder {
  55     private final BasicStroke stroke;
  56     private final Paint paint;
  57 
  58     /**
  59      * Creates a border of the specified {@code stroke}.
  60      * The component's foreground color will be used to render the border.
  61      *
  62      * @param stroke  the {@link BasicStroke} object used to stroke a shape
  63      *
  64      * @throws NullPointerException if the specified {@code stroke} is {@code null}
  65      */
  66     public StrokeBorder(BasicStroke stroke) {
  67         this(stroke, null);
  68     }
  69 
  70     /**
  71      * Creates a border of the specified {@code stroke} and {@code paint}.
  72      * If the specified {@code paint} is {@code null},
  73      * the component's foreground color will be used to render the border.
  74      *
  75      * @param stroke  the {@link BasicStroke} object used to stroke a shape
  76      * @param paint   the {@link Paint} object used to generate a color
  77      *
  78      * @throws NullPointerException if the specified {@code stroke} is {@code null}
  79      */
  80     @ConstructorProperties({ "stroke", "paint" })
  81     public StrokeBorder(BasicStroke stroke, Paint paint) {
  82         if (stroke == null) {
  83             throw new NullPointerException("border's stroke");
  84         }
  85         this.stroke = stroke;
  86         this.paint = paint;
  87     }
  88 
  89     /**
  90      * Paints the border for the specified component
  91      * with the specified position and size.
  92      * If the border was not specified with a {@link Paint} object,
  93      * the component's foreground color will be used to render the border.
  94      * If the component's foreground color is not available,
  95      * the default color of the {@link Graphics} object will be used.
  96      *
  97      * @param c       the component for which this border is being painted
  98      * @param g       the paint graphics
  99      * @param x       the x position of the painted border
 100      * @param y       the y position of the painted border
 101      * @param width   the width of the painted border
 102      * @param height  the height of the painted border
 103      *
 104      * @throws NullPointerException if the specified {@code g} is {@code null}
 105      */
 106     @Override
 107     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
 108         float size = this.stroke.getLineWidth();
 109         if (size > 0.0f) {
 110             g = g.create();
 111             if (g instanceof Graphics2D) {
 112                 Graphics2D g2d = (Graphics2D) g;
 113                 g2d.setStroke(this.stroke);
 114                 g2d.setPaint(this.paint != null ? this.paint : c == null ? null : c.getForeground());
 115                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 116                                      RenderingHints.VALUE_ANTIALIAS_ON);
 117                 g2d.draw(new Rectangle2D.Float(x + size / 2, y + size / 2, width - size, height - size));
 118             }
 119             g.dispose();
 120         }
 121     }
 122 
 123     /**
 124      * Reinitializes the {@code insets} parameter
 125      * with this border's current insets.
 126      * Every inset is the smallest (closest to negative infinity) integer value
 127      * that is greater than or equal to the line width of the stroke
 128      * that is used to paint the border.
 129      *
 130      * @param c       the component for which this border insets value applies
 131      * @param insets  the {@code Insets} object to be reinitialized
 132      * @return the reinitialized {@code insets} parameter
 133      *
 134      * @throws NullPointerException if the specified {@code insets} is {@code null}
 135      *
 136      * @see Math#ceil
 137      */
 138     @Override
 139     public Insets getBorderInsets(Component c, Insets insets) {
 140         int size = (int) Math.ceil(this.stroke.getLineWidth());
 141         insets.set(size, size, size, size);
 142         return insets;
 143     }
 144 
 145     /**
 146      * Returns the {@link BasicStroke} object used to stroke a shape
 147      * during the border rendering.
 148      *
 149      * @return the {@link BasicStroke} object
 150      */
 151     public BasicStroke getStroke() {
 152         return this.stroke;
 153     }
 154 
 155     /**
 156      * Returns the {@link Paint} object used to generate a color
 157      * during the border rendering.
 158      *
 159      * @return the {@link Paint} object or {@code null}
 160      *         if the {@code paint} parameter is not set
 161      */
 162     public Paint getPaint() {
 163         return this.paint;
 164     }
 165 }