jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicArrowButton.java
author weijun
Thu, 11 Dec 2014 15:19:12 +0800
changeset 28228 be83f404724d
parent 25859 3317bb8137f4
child 28231 b608ffcaed74
permissions -rw-r--r--
8067207: Replace concat String to append in StringBuilder parameters (client) Reviewed-by: serb Contributed-by: Otavio Santana <otaviojava@java.net>
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.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * JButton object that draws a scaled Arrow in one of the cardinal directions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * 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
    42
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author David Kloba
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    48
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class BasicArrowButton extends JButton implements SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
         * The direction of the arrow. One of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
         * {@code SwingConstants.NORTH}, {@code SwingConstants.SOUTH},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
         * {@code SwingConstants.EAST} or {@code SwingConstants.WEST}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        protected int direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        private Color shadow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        private Color darkShadow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        private Color highlight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * Creates a {@code BasicArrowButton} whose arrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * is drawn in the specified direction and with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * @param direction the direction of the arrow; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         *        {@code SwingConstants.NORTH}, {@code SwingConstants.SOUTH},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         *        {@code SwingConstants.EAST} or {@code SwingConstants.WEST}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         * @param background the background color of the button
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         * @param shadow the color of the shadow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * @param darkShadow the color of the dark shadow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * @param highlight the color of the highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public BasicArrowButton(int direction, Color background, Color shadow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                         Color darkShadow, Color highlight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            setRequestFocusEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            setDirection(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            setBackground(background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            this.shadow = shadow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            this.darkShadow = darkShadow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            this.highlight = highlight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * Creates a {@code BasicArrowButton} whose arrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * is drawn in the specified direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         * @param direction the direction of the arrow; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         *        {@code SwingConstants.NORTH}, {@code SwingConstants.SOUTH},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         *        {@code SwingConstants.EAST} or {@code SwingConstants.WEST}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public BasicArrowButton(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            this(direction, UIManager.getColor("control"), UIManager.getColor("controlShadow"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                 UIManager.getColor("controlDkShadow"), UIManager.getColor("controlLtHighlight"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * Returns the direction of the arrow.
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 22574
diff changeset
   102
         *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 22574
diff changeset
   103
         * @return the direction of the arrow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        public int getDirection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         * Sets the direction of the arrow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * @param direction the direction of the arrow; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         *        of {@code SwingConstants.NORTH},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         *        {@code SwingConstants.SOUTH},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         *        {@code SwingConstants.EAST} or {@code SwingConstants.WEST}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public void setDirection(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            Color origColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            boolean isPressed, isEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            int w, h, size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            w = getSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            h = getSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            origColor = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            isPressed = getModel().isPressed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            isEnabled = isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            g.setColor(getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            g.fillRect(1, 1, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            /// Draw the proper Border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (getBorder() != null && !(getBorder() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                paintBorder(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } else if (isPressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                g.drawRect(0, 0, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // Using the background color set above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                g.drawLine(0, 0, 0, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                g.drawLine(1, 0, w-2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                g.setColor(highlight);    // inner 3D border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                g.drawLine(1, 1, 1, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                g.drawLine(2, 1, w-3, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                g.setColor(shadow);       // inner 3D border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                g.drawLine(1, h-2, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                g.drawLine(w-2, 1, w-2, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                g.setColor(darkShadow);     // black drop shadow  __|
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                g.drawLine(0, h-1, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                g.drawLine(w-1, h-1, w-1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // If there's no room to draw arrow, bail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if(h < 5 || w < 5)      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                g.setColor(origColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (isPressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                g.translate(1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // Draw the arrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            size = Math.min((h - 4) / 3, (w - 4) / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            size = Math.max(size, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            paintTriangle(g, (w - size) / 2, (h - size) / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                size, direction, isEnabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // Reset the Graphics back to it's original settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (isPressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                g.translate(-1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            g.setColor(origColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         * Returns the preferred size of the {@code BasicArrowButton}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         * @return the preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return new Dimension(16, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * Returns the minimum size of the {@code BasicArrowButton}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * @return the minimum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return new Dimension(5, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * Returns the maximum size of the {@code BasicArrowButton}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * @return the maximum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        public Dimension getMaximumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * Returns whether the arrow button should get the focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         * {@code BasicArrowButton}s are used as a child component of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * composite components such as {@code JScrollBar} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * {@code JComboBox}. Since the composite component typically gets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * focus, this method is overriden to return {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         * @return {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        public boolean isFocusTraversable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
          return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * Paints a triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * @param g the {@code Graphics} to draw to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         * @param x the x coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * @param y the y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         * @param size the size of the triangle to draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         * @param direction the direction in which to draw the arrow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         *        one of {@code SwingConstants.NORTH},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         *        {@code SwingConstants.SOUTH}, {@code SwingConstants.EAST} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         *        {@code SwingConstants.WEST}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         * @param isEnabled whether or not the arrow is drawn enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public void paintTriangle(Graphics g, int x, int y, int size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                        int direction, boolean isEnabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            Color oldColor = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int mid, i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            size = Math.max(size, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            mid = (size / 2) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            g.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if(isEnabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            switch(direction)       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            case NORTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                for(i = 0; i < size; i++)      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    g.drawLine(mid-i, i, mid+i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if(!isEnabled)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    g.setColor(highlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    g.drawLine(mid-i+2, i, mid+i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            case SOUTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if(!isEnabled)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    g.translate(1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    g.setColor(highlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    for(i = size-1; i >= 0; i--)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        g.drawLine(mid-i, j, mid+i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    g.translate(-1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                for(i = size-1; i >= 0; i--)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    g.drawLine(mid-i, j, mid+i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            case WEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                for(i = 0; i < size; i++)      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    g.drawLine(i, mid-i, i, mid+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                if(!isEnabled)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    g.setColor(highlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    g.drawLine(i, mid-i+2, i, mid+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            case EAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if(!isEnabled)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    g.translate(1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    g.setColor(highlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    for(i = size-1; i >= 0; i--)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        g.drawLine(j, mid-i, j, mid+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    g.translate(-1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                for(i = size-1; i >= 0; i--)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    g.drawLine(j, mid-i, j, mid+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            g.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}