jdk/src/share/classes/javax/swing/border/LineBorder.java
author darcy
Fri, 24 Jan 2014 07:16:53 -0800
changeset 22574 7f8ce0c8c20a
parent 20458 f2423fb3fd19
child 24500 399b77799d66
child 24544 c0133e7c7162
permissions -rw-r--r--
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes Reviewed-by: alexsch, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.geom.Path2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.geom.RoundRectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A class which implements a line border of arbitrary thickness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and of a single color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    47
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author David Kloba
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    53
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class LineBorder extends AbstractBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static Border blackLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static Border grayLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected int thickness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected Color lineColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected boolean roundedCorners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** Convenience method for getting the Color.black LineBorder of thickness 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static Border createBlackLineBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (blackLine == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            blackLine = new LineBorder(Color.black, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return blackLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /** Convenience method for getting the Color.gray LineBorder of thickness 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static Border createGrayLineBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (grayLine == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            grayLine = new LineBorder(Color.gray, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return grayLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Creates a line border with the specified color and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * thickness = 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param color the color for the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public LineBorder(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this(color, 1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Creates a line border with the specified color and thickness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param color the color of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param thickness the thickness of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public LineBorder(Color color, int thickness)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this(color, thickness, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Creates a line border with the specified color, thickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * and corner shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param color the color of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param thickness the thickness of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param roundedCorners whether or not border corners should be round
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    @ConstructorProperties({"lineColor", "thickness", "roundedCorners"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public LineBorder(Color color, int thickness, boolean roundedCorners)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        lineColor = color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.thickness = thickness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.roundedCorners = roundedCorners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Paints the border for the specified component with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * specified position and size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param c the component for which this border is being painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param g the paint graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param x the x position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param y the y position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param width the width of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param height the height of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if ((this.thickness > 0) && (g instanceof Graphics2D)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            Graphics2D g2d = (Graphics2D) g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            Color oldColor = g2d.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            g2d.setColor(this.lineColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Shape outer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            Shape inner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int offs = this.thickness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int size = offs + offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (this.roundedCorners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                int arc = offs + size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                outer = new RoundRectangle2D.Float(x, y, width, height, arc, arc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                inner = new RoundRectangle2D.Float(x + offs, y + offs, width - size, height - size, arc, arc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                outer = new Rectangle2D.Float(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                inner = new Rectangle2D.Float(x + offs, y + offs, width - size, height - size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            path.append(outer, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            path.append(inner, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            g2d.fill(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            g2d.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Reinitialize the insets parameter with this Border's current Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param c the component for which this border insets value applies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param insets the object to be reinitialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        insets.set(thickness, thickness, thickness, thickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns the color of the border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public Color getLineColor()     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return lineColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Returns the thickness of the border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public int getThickness()       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return thickness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns whether this border will be drawn with rounded corners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public boolean getRoundedCorners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return roundedCorners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns whether or not the border is opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public boolean isBorderOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return !roundedCorners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}