jdk/src/share/classes/com/sun/java/swing/plaf/gtk/GTKColorChooserPanel.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 438 2ae294e4518c
child 1299 027d966d5658
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package com.sun.java.swing.plaf.gtk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.colorchooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A color chooser panel mimicking that of GTK's: a color wheel showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * hue and a triangle that varies saturation and brightness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class GTKColorChooserPanel extends AbstractColorChooserPanel implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
              ChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final float PI_3 = (float)(Math.PI / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private ColorTriangle triangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private JLabel lastLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private JLabel label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private JSpinner hueSpinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private JSpinner saturationSpinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private JSpinner valueSpinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private JSpinner redSpinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private JSpinner greenSpinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private JSpinner blueSpinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private JTextField colorNameTF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean settingColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // The colors are mirrored to avoid creep in adjusting an individual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private float hue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private float saturation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private float brightness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Convenience method to transfer focus to the next child of component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // PENDING: remove this when a variant of this is added to awt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static void compositeRequestFocus(Component component, boolean direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (component instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            Container container = (Container)component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (container.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                FocusTraversalPolicy policy = container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                              getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                Component comp = policy.getDefaultComponent(container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                if (comp!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    comp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            Container rootAncestor = container.getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (rootAncestor!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                FocusTraversalPolicy policy = rootAncestor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                                  getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                Component comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    comp = policy.getComponentAfter(rootAncestor, container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    comp = policy.getComponentBefore(rootAncestor, container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    comp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        component.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Returns a user presentable description of this GTKColorChooserPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public String getDisplayName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return (String)UIManager.get("GTKColorChooserPanel.nameText");
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
     * Returns the mnemonic to use with <code>getDisplayName</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public int getMnemonic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        String m = (String)UIManager.get("GTKColorChooserPanel.mnemonic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (m != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                int value = Integer.parseInt(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            } catch (NumberFormatException nfe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Character to underline that represents the mnemonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public int getDisplayedMnemonicIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        String m = (String)UIManager.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                           "GTKColorChooserPanel.displayedMnemonicIndex");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (m != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                int value = Integer.parseInt(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } catch (NumberFormatException nfe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public Icon getSmallDisplayIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Icon getLargeDisplayIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void uninstallChooserPanel(JColorChooser enclosingChooser) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        super.uninstallChooserPanel(enclosingChooser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Builds and configures the widgets for the GTKColorChooserPanel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected void buildChooser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        triangle = new ColorTriangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        triangle.setName("GTKColorChooserPanel.triangle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // PENDING: when we straighten out user setting opacity, this should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        label = new OpaqueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        label.setName("GTKColorChooserPanel.colorWell");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        label.setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        label.setMinimumSize(new Dimension(67, 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        label.setPreferredSize(new Dimension(67, 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        label.setMaximumSize(new Dimension(67, 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // PENDING: when we straighten out user setting opacity, this should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        lastLabel = new OpaqueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        lastLabel.setName("GTKColorChooserPanel.lastColorWell");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        lastLabel.setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        lastLabel.setMinimumSize(new Dimension(67, 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        lastLabel.setPreferredSize(new Dimension(67, 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        lastLabel.setMaximumSize(new Dimension(67, 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        hueSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 360, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        configureSpinner(hueSpinner, "GTKColorChooserPanel.hueSpinner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        saturationSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        configureSpinner(saturationSpinner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                         "GTKColorChooserPanel.saturationSpinner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        valueSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        configureSpinner(valueSpinner, "GTKColorChooserPanel.valueSpinner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        redSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        configureSpinner(redSpinner, "GTKColorChooserPanel.redSpinner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        greenSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        configureSpinner(greenSpinner, "GTKColorChooserPanel.greenSpinner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        blueSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        configureSpinner(blueSpinner, "GTKColorChooserPanel.blueSpinner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        colorNameTF = new JTextField(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        setLayout(new GridBagLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        add(this, "GTKColorChooserPanel.hue", hueSpinner, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        add(this, "GTKColorChooserPanel.red", redSpinner, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        add(this, "GTKColorChooserPanel.saturation", saturationSpinner, -1,-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        add(this, "GTKColorChooserPanel.green", greenSpinner, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        add(this, "GTKColorChooserPanel.value", valueSpinner, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        add(this, "GTKColorChooserPanel.blue", blueSpinner, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        add(new JSeparator(SwingConstants.HORIZONTAL), new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                  GridBagConstraints(1, 3, 4, 1, 1, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                  GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                  new Insets(14, 0, 0, 0), 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        add(this, "GTKColorChooserPanel.colorName", colorNameTF, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        add(triangle, new GridBagConstraints(0, 0, 1, 5, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                      GridBagConstraints.LINE_START, GridBagConstraints.NONE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                      new Insets(14, 20, 2, 9), 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        Box hBox = Box.createHorizontalBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        hBox.add(lastLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        hBox.add(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        add(hBox, new GridBagConstraints(0, 5, 1, 1, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                      GridBagConstraints.CENTER, GridBagConstraints.NONE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                      new Insets(0, 0, 0, 0), 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        add(new JSeparator(SwingConstants.HORIZONTAL), new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                  GridBagConstraints(0, 6, 5, 1, 1, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                  GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                  new Insets(12, 0, 0, 0), 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Configures the spinner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private void configureSpinner(JSpinner spinner, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        spinner.addChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        spinner.setName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        JComponent editor = spinner.getEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (editor instanceof JSpinner.DefaultEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            JFormattedTextField ftf = ((JSpinner.DefaultEditor)editor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                                 getTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            ftf.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Adds the widget creating a JLabel with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private void add(Container parent, String key, JComponent widget,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                     int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        JLabel label = new JLabel(UIManager.getString(key + "Text",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                                      getLocale()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        String mnemonic = (String)UIManager.get(key + "Mnemonic", getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (mnemonic != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                label.setDisplayedMnemonic(Integer.parseInt(mnemonic));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            String mnemonicIndex = (String)UIManager.get(key + "MnemonicIndex",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                                    getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (mnemonicIndex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    label.setDisplayedMnemonicIndex(Integer.parseInt(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                                        mnemonicIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        label.setLabelFor(widget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            x = parent.getComponentCount() % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            y = parent.getComponentCount() / 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        GridBagConstraints con = new GridBagConstraints(x + 1, y, 1, 1, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                   GridBagConstraints.FIRST_LINE_END, GridBagConstraints.NONE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                   new Insets(4, 0, 0, 4), 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (y == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            con.insets.top = 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        parent.add(label, con);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        con.gridx++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        parent.add(widget, con);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Refreshes the display from the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public void updateChooser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (!settingColor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            lastLabel.setBackground(getColorFromModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            setColor(getColorFromModel(), true, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Resets the red component of the selected color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private void setRed(int red) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        setRGB(red << 16 | getColor().getGreen() << 8 | getColor().getBlue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Resets the green component of the selected color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private void setGreen(int green) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        setRGB(getColor().getRed() << 16 | green << 8 | getColor().getBlue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Resets the blue component of the selected color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private void setBlue(int blue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        setRGB(getColor().getRed() << 16 | getColor().getGreen() << 8 | blue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Sets the hue of the selected color and updates the display if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private void setHue(float hue, boolean update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        setHSB(hue, saturation, brightness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            settingColor = true;
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   331
            hueSpinner.setValue(Integer.valueOf((int)(hue * 360)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            settingColor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Returns the current amount of hue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private float getHue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return hue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Resets the saturation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private void setSaturation(float saturation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        setHSB(hue, saturation, brightness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Returns the saturation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private float getSaturation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return saturation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Sets the brightness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    private void setBrightness(float brightness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        setHSB(hue, saturation, brightness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Returns the brightness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private float getBrightness() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return brightness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Sets the saturation and brightness and updates the display if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    private void setSaturationAndBrightness(float s, float b, boolean update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        setHSB(hue, s, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            settingColor = true;
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   379
            saturationSpinner.setValue(Integer.valueOf((int)(s * 255)));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   380
            valueSpinner.setValue(Integer.valueOf((int)(b * 255)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            settingColor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Resets the rgb values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private void setRGB(int rgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        Color color = new Color(rgb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        setColor(color, false, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        settingColor = true;
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   394
        hueSpinner.setValue(Integer.valueOf((int)(hue * 360)));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   395
        saturationSpinner.setValue(Integer.valueOf((int)(saturation * 255)));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   396
        valueSpinner.setValue(Integer.valueOf((int)(brightness * 255)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        settingColor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Resets the hsb values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private void setHSB(float h, float s, float b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        Color color = Color.getHSBColor(h, s, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        this.hue = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        this.saturation = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        this.brightness = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        setColor(color, false, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        settingColor = true;
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   412
        redSpinner.setValue(Integer.valueOf(color.getRed()));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   413
        greenSpinner.setValue(Integer.valueOf(color.getGreen()));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   414
        blueSpinner.setValue(Integer.valueOf(color.getBlue()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        settingColor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Rests the color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param color new Color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param updateSpinners whether or not to update the spinners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param updateHSB if true, the hsb fields are updated based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *                  new color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param updateModel if true, the model is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    private void setColor(Color color, boolean updateSpinners,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                          boolean updateHSB, boolean updateModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            color = Color.BLACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        settingColor = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (updateHSB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                         color.getBlue(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            hue = hsb[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            saturation = hsb[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            brightness = hsb[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (updateModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            getColorSelectionModel().setSelectedColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        triangle.setColor(hue, saturation, brightness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        label.setBackground(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // Force Integer to pad the string with 0's by adding 0x1000000 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        // then removing the first character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        String hexString = Integer.toHexString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                  (color.getRGB() & 0xFFFFFF) | 0x1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        colorNameTF.setText("#" + hexString.substring(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (updateSpinners) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   457
            redSpinner.setValue(Integer.valueOf(color.getRed()));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   458
            greenSpinner.setValue(Integer.valueOf(color.getGreen()));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   459
            blueSpinner.setValue(Integer.valueOf(color.getBlue()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   461
            hueSpinner.setValue(Integer.valueOf((int)(hue * 360)));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   462
            saturationSpinner.setValue(Integer.valueOf((int)(saturation * 255)));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   463
            valueSpinner.setValue(Integer.valueOf((int)(brightness * 255)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        settingColor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public Color getColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return label.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * ChangeListener method, updates the necessary display widgets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (settingColor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        Color color = getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (e.getSource() == hueSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            setHue(((Number)hueSpinner.getValue()).floatValue() / 360, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        else if (e.getSource() == saturationSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            setSaturation(((Number)saturationSpinner.getValue()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                          floatValue() / 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        else if (e.getSource() == valueSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            setBrightness(((Number)valueSpinner.getValue()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                          floatValue() / 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        else if (e.getSource() == redSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            setRed(((Number)redSpinner.getValue()).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        else if (e.getSource() == greenSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            setGreen(((Number)greenSpinner.getValue()).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        else if (e.getSource() == blueSpinner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            setBlue(((Number)blueSpinner.getValue()).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Flag indicating the angle, or hue, has changed and the triangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * needs to be recreated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private static final int FLAGS_CHANGED_ANGLE = 1 << 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * Indicates the wheel is being dragged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private static final int FLAGS_DRAGGING = 1 << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Indicates the triangle is being dragged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private static final int FLAGS_DRAGGING_TRIANGLE = 1 << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Indicates a color is being set and we should ignore setColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    private static final int FLAGS_SETTING_COLOR = 1 << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Indicates the wheel has focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    private static final int FLAGS_FOCUSED_WHEEL = 1 << 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Indicates the triangle has focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    private static final int FLAGS_FOCUSED_TRIANGLE = 1 << 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Class responsible for rendering a color wheel and color triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private class ColorTriangle extends JPanel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         * Cached image of the wheel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        private Image wheelImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * Cached image of the triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        private Image triangleImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
         * Angle triangle is rotated by.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        private double angle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         * Boolean bitmask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        private int flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
         * X location of selected color indicator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        private int circleX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * Y location of selected color indicator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        private int circleY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        public ColorTriangle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            enableEvents(AWTEvent.FOCUS_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            enableEvents(AWTEvent.MOUSE_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            setMinimumSize(new Dimension(getWheelRadius() * 2 + 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                         getWheelRadius() * 2 + 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            setPreferredSize(new Dimension(getWheelRadius() * 2 + 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                           getWheelRadius() * 2 + 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            // We want to handle tab ourself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            setFocusTraversalKeysEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            // PENDING: this should come from the style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            getInputMap().put(KeyStroke.getKeyStroke("UP"), "up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "down");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "left");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "right");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            getInputMap().put(KeyStroke.getKeyStroke("KP_UP"), "up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            getInputMap().put(KeyStroke.getKeyStroke("KP_DOWN"), "down");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            getInputMap().put(KeyStroke.getKeyStroke("KP_LEFT"), "left");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            getInputMap().put(KeyStroke.getKeyStroke("KP_RIGHT"), "right");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            getInputMap().put(KeyStroke.getKeyStroke("TAB"), "focusNext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            getInputMap().put(KeyStroke.getKeyStroke("shift TAB"),"focusLast");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            ActionMap map = (ActionMap)UIManager.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                       "GTKColorChooserPanel.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                map = new ActionMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                map.put("left", new ColorAction("left", 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                map.put("right", new ColorAction("right", 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                map.put("up", new ColorAction("up", 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                map.put("down", new ColorAction("down", 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                map.put("focusNext", new ColorAction("focusNext", 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                map.put("focusLast", new ColorAction("focusLast", 5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                UIManager.getLookAndFeelDefaults().put(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                             "GTKColorChooserPanel.actionMap", map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            SwingUtilities.replaceUIActionMap(this, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * Returns the GTKColorChooserPanel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        GTKColorChooserPanel getGTKColorChooserPanel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            return GTKColorChooserPanel.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         * Gives focus to the wheel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        void focusWheel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            setFocusType(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         * Gives focus to the triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        void focusTriangle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            setFocusType(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * Returns true if the wheel currently has focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        boolean isWheelFocused() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            return isSet(FLAGS_FOCUSED_WHEEL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         * Resets the selected color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        public void setColor(float h, float s, float b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (isSet(FLAGS_SETTING_COLOR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            setAngleFromHue(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            setSaturationAndBrightness(s, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * Returns the selected color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        public Color getColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            return GTKColorChooserPanel.this.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * Returns the x location of the selected color indicator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        int getColorX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            return circleX + getIndicatorSize() / 2 - getWheelXOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         * Returns the y location of the selected color indicator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        int getColorY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            return circleY + getIndicatorSize() / 2 - getWheelYOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        protected void processEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            if (e.getID() == MouseEvent.MOUSE_PRESSED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                   ((isSet(FLAGS_DRAGGING) ||isSet(FLAGS_DRAGGING_TRIANGLE)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                   e.getID() == MouseEvent.MOUSE_DRAGGED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                // Assign focus to either the wheel or triangle and attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                // to drag either the wheel or triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                int size = getWheelRadius();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                int x = ((MouseEvent)e).getX() - size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                int y = ((MouseEvent)e).getY() - size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                if (!hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                if (!isSet(FLAGS_DRAGGING_TRIANGLE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                      adjustHue(x, y, e.getID() == MouseEvent.MOUSE_PRESSED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    setFlag(FLAGS_DRAGGING, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    setFocusType(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                else if (adjustSB(x, y, e.getID() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                                        MouseEvent.MOUSE_PRESSED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    setFlag(FLAGS_DRAGGING_TRIANGLE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    setFocusType(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    setFocusType(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                // Stopped dragging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                setFlag(FLAGS_DRAGGING_TRIANGLE, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                setFlag(FLAGS_DRAGGING, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            else if (e.getID() == FocusEvent.FOCUS_LOST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                // Reset the flags to indicate no one has focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                setFocusType(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            else if (e.getID() == FocusEvent.FOCUS_GAINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                // Gained focus, reassign focus to the wheel if no one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                // currently has focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                if (!isSet(FLAGS_FOCUSED_TRIANGLE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                          !isSet(FLAGS_FOCUSED_WHEEL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    setFlag(FLAGS_FOCUSED_WHEEL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    setFocusType(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            super.processEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        public void paintComponent(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            super.paintComponent(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            // Draw the wheel and triangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            int size = getWheelRadius();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            int width = getWheelWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            Image image = getImage(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            g.drawImage(image, getWheelXOrigin() - size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                        getWheelYOrigin() - size, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            // Draw the focus indicator for the wheel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (hasFocus() && isSet(FLAGS_FOCUSED_WHEEL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                g.setColor(Color.BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                g.drawOval(getWheelXOrigin() - size, getWheelYOrigin() - size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                           2 * size, 2 * size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                g.drawOval(getWheelXOrigin() - size + width, getWheelYOrigin()-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                           size + width, 2 * (size - width), 2 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                           (size - width));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            // Draw a line on the wheel indicating the selected hue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            if (Math.toDegrees(Math.PI * 2 - angle) <= 20 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                     Math.toDegrees(Math.PI * 2 - angle) >= 201) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                g.setColor(Color.WHITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                g.setColor(Color.BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            int lineX0 = (int)(Math.cos(angle) * size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            int lineY0 = (int)(Math.sin(angle) * size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            int lineX1 = (int)(Math.cos(angle) * (size - width));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            int lineY1 = (int)(Math.sin(angle) * (size - width));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            g.drawLine(lineX0 + size, lineY0 + size, lineX1 + size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                       lineY1 + size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            // Draw the focus indicator on the triangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (hasFocus() && isSet(FLAGS_FOCUSED_TRIANGLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                Graphics g2 = g.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                int innerR = getTriangleCircumscribedRadius();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                int a = (int)(3 * innerR / Math.sqrt(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                g2.translate(getWheelXOrigin(), getWheelYOrigin());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                ((Graphics2D)g2).rotate(angle + Math.PI / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                g2.setColor(Color.BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                g2.drawLine(0, -innerR, a / 2, innerR / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                g2.drawLine(a / 2, innerR / 2, -a / 2, innerR / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                g2.drawLine(-a / 2, innerR / 2, 0, -innerR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                g2.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            // Draw the selected color indicator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            g.setColor(Color.BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            g.drawOval(circleX, circleY, getIndicatorSize() - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                       getIndicatorSize() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            g.setColor(Color.WHITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            g.drawOval(circleX + 1, circleY + 1, getIndicatorSize() - 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                       getIndicatorSize() - 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         * Returns an image representing the triangle and wheel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        private Image getImage(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            if (!isSet(FLAGS_CHANGED_ANGLE) && wheelImage != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        wheelImage.getWidth(null) == size * 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                return wheelImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            if (wheelImage == null || wheelImage.getWidth(null) != size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                wheelImage = getWheelImage(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            int innerR = getTriangleCircumscribedRadius();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            int triangleSize = (int)(innerR * 3.0 / 2.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            int a = (int)(2 * triangleSize / Math.sqrt(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            if (triangleImage == null || triangleImage.getWidth(null) != a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                triangleImage = new BufferedImage(a, a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                                                  BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            Graphics g = triangleImage.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            g.setColor(new Color(0, 0, 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            g.fillRect(0, 0, a, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            g.translate((int)(a / 2), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            paintTriangle(g, triangleSize, getColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            g.translate((int)(-a / 2), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            g = wheelImage.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            g.setColor(new Color(0, 0, 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            g.fillOval(getWheelWidth(), getWheelWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                       2 * (size - getWheelWidth()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                       2 * (size - getWheelWidth()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            double rotate = Math.toRadians(-30.0) + angle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            g.translate(size, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            ((Graphics2D)g).rotate(rotate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            g.drawImage(triangleImage, -a / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                        getWheelWidth() - size, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            ((Graphics2D)g).rotate(-rotate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            g.translate(a / 2, size - getWheelWidth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            setFlag(FLAGS_CHANGED_ANGLE, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            return wheelImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        private void paintTriangle(Graphics g, int size, Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            float[] colors = Color.RGBtoHSB(color.getRed(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                                            color.getGreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                            color.getBlue(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            float hue = colors[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            double dSize = (double)size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            for (int y = 0; y < size; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                int maxX = (int)(y * Math.tan(Math.toRadians(30.0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                float factor = maxX * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                if (maxX > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    float value = (float)(y / dSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    for (int x = -maxX; x <= maxX; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        float saturation = (float)x / factor + .5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        g.setColor(Color.getHSBColor(hue, saturation, value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                        g.fillRect(x, y, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    g.fillRect(0, y, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         * Returns a color wheel image for the specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         * @param size Integer giving size of color wheel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         * @return Color wheel image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        private Image getWheelImage(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            int minSize = size - getWheelWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            int doubleSize = size * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            BufferedImage image = new BufferedImage(doubleSize, doubleSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                                              BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            for (int y = -size; y < size; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                int ySquared = y * y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                for (int x = -size; x < size; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    double rad = Math.sqrt(ySquared + x * x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    if (rad < size && rad > minSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                        int rgb = colorWheelLocationToRGB(x, y, rad) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                              0xFF000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                        image.setRGB(x + size, y + size, rgb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            wheelImage = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            return wheelImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
         * Adjusts the saturation and brightness. <code>x</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
         * <code>y</code> give the location to adjust to and are relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
         * to the origin of the wheel/triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
         * @param x X coordinate on the triangle to adjust to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
         * @param y Y coordinate on the triangle to adjust to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
         * @param checkLoc if true the location is checked to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
         *        it is contained in the triangle, if false the location is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
         *        constrained to fit in the triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         * @return true if the location is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        boolean adjustSB(int x, int y, boolean checkLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            int innerR = getWheelRadius() - getWheelWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            boolean resetXY = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            // Invert the axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            y = -y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            if (checkLoc && (x < -innerR || x > innerR || y < -innerR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                             y > innerR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            // Rotate to origin and and verify x is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            int triangleSize = (int)innerR * 3 / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            double x1 = Math.cos(angle) * x - Math.sin(angle) * y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            double y1 = Math.sin(angle) * x + Math.cos(angle) * y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            if (x1 < -(innerR / 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                if (checkLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                x1 = -innerR / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                resetXY = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            else if ((int)x1 > innerR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                if (checkLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                x1 = innerR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                resetXY = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            // Verify y location is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            int maxY = (int)((triangleSize - x1 - innerR / 2.0) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                             Math.tan(Math.toRadians(30.0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            if (y1 <= -maxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                if (checkLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                y1 = -maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                resetXY = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            else if (y1 > maxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                if (checkLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                y1 = maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                resetXY = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            // Rotate again to determine value and scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            double x2 = Math.cos(Math.toRadians(-30.0)) * x1 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                 Math.sin(Math.toRadians(-30.0)) * y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            double y2 = Math.sin(Math.toRadians(-30.0)) * x1 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                 Math.cos(Math.toRadians(-30.0)) * y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            float value = Math.min(1.0f, (float)((innerR - y2) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                                (double)triangleSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            float maxX = (float)(Math.tan(Math.toRadians(30)) * (innerR - y2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            float saturation = Math.min(1.0f, (float)(x2 / maxX / 2 + .5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            setFlag(FLAGS_SETTING_COLOR, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            if (resetXY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                setSaturationAndBrightness(saturation, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                setSaturationAndBrightness(saturation, value, x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                                      getWheelXOrigin(),getWheelYOrigin() - y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            GTKColorChooserPanel.this.setSaturationAndBrightness(saturation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                                                                 value, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            setFlag(FLAGS_SETTING_COLOR, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         * Sets the saturation and brightness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        private void setSaturationAndBrightness(float s, float b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            int innerR = getTriangleCircumscribedRadius();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            int triangleSize = (int)innerR * 3 / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            double x = b * triangleSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            double maxY = x * Math.tan(Math.toRadians(30.0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            double y = 2 * maxY * s - maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            x = x - innerR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            double x1 = Math.cos(Math.toRadians(-60.0) - angle) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                        x - Math.sin(Math.toRadians(-60.0) - angle) * y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            double y1 = Math.sin(Math.toRadians(-60.0) - angle) * x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                        Math.cos(Math.toRadians(-60.0) - angle) * y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            int newCircleX = (int)x1 + getWheelXOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            int newCircleY = getWheelYOrigin() - (int)y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            setSaturationAndBrightness(s, b, newCircleX, newCircleY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         * Sets the saturation and brightness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        private void setSaturationAndBrightness(float s, float b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                                             int newCircleX, int newCircleY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            newCircleX -= getIndicatorSize() / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            newCircleY -= getIndicatorSize() / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            int minX = Math.min(newCircleX, circleX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            int minY = Math.min(newCircleY, circleY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            repaint(minX, minY, Math.max(circleX, newCircleX) - minX +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                    getIndicatorSize() + 1, Math.max(circleY, newCircleY) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                    minY + getIndicatorSize() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            circleX = newCircleX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            circleY = newCircleY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
         * Adjusts the hue based on the passed in location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
         * @param x X location to adjust to, relative to the origin of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
         *        wheel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         * @param y Y location to adjust to, relative to the origin of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         *        wheel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         * @param check if true the location is checked to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         *        it is contained in the wheel, if false the location is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
         *        constrained to fit in the wheel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
         * @return true if the location is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        private boolean adjustHue(int x, int y, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            double rad = Math.sqrt(x * x + y * y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            int size = getWheelRadius();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            if (!check || (rad >= size - getWheelWidth() && rad < size)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                // Map the location to an angle and reset hue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                double angle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                if (x == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    if (y > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                        angle = Math.PI / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                        angle = Math.PI + Math.PI / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    angle = Math.atan((double)y / (double)x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    if (x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                        angle += Math.PI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    else if (angle < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        angle += 2 * Math.PI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                setFlag(FLAGS_SETTING_COLOR, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                setHue((float)(1.0 - angle / Math.PI / 2), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                setFlag(FLAGS_SETTING_COLOR, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                setHueAngle(angle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                setSaturationAndBrightness(getSaturation(), getBrightness());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         * Rotates the triangle to accomodate the passed in hue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        private void setAngleFromHue(float hue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            setHueAngle((1.0 - hue) * Math.PI * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
         * Sets the angle representing the hue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        private void setHueAngle(double angle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            double oldAngle = this.angle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            this.angle = angle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            if (angle != oldAngle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                setFlag(FLAGS_CHANGED_ANGLE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
         * Returns the size of the color indicator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        private int getIndicatorSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            return 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
         * Returns the circumscribed radius of the triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        private int getTriangleCircumscribedRadius() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            return 72;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
         * Returns the x origin of the wheel and triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        private int getWheelXOrigin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            return 85;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
         * Returns y origin of the wheel and triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        private int getWheelYOrigin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            return 85;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
         * Returns the width of the wheel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        private int getWheelWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            return 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
         * Sets the focus to one of: 0 no one, 1 the wheel or 2 the triangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        private void setFocusType(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            if (type == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                setFlag(FLAGS_FOCUSED_WHEEL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                setFlag(FLAGS_FOCUSED_TRIANGLE, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                int toSet = FLAGS_FOCUSED_WHEEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                int toUnset = FLAGS_FOCUSED_TRIANGLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                if (type == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    toSet = FLAGS_FOCUSED_TRIANGLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                    toUnset = FLAGS_FOCUSED_WHEEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                if (!isSet(toSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    setFlag(toSet, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                    repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                    setFlag(toUnset, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
         * Returns the radius of the wheel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        private int getWheelRadius() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            // As far as I can tell, GTK doesn't allow stretching this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            // widget
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            return 85;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
         * Updates the flags bitmask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        private void setFlag(int flag, boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            if (value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                flags |= flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                flags &= ~flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
         * Returns true if a particular flag has been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        private boolean isSet(int flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            return ((flags & flag) == flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
         * Returns the RGB color to use for the specified location. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
         * passed in point must be on the color wheel and be relative to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
         * origin of the color wheel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
         * @param x X location to get color for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
         * @param y Y location to get color for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
         * @param rad Radius from center of color wheel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
         * @param integer with red, green and blue components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        private int colorWheelLocationToRGB(int x, int y, double rad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            double angle = Math.acos((double)x / rad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            int rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            if (angle < PI_3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                if (y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    // FFFF00 - FF0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                    rgb = 0xFF0000 | (int)Math.min(255,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                                           (int)(255 * angle / PI_3)) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                    // FF0000 - FF00FF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    rgb = 0xFF0000 | (int)Math.min(255,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                                           (int)(255 * angle / PI_3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            else if (angle < 2 * PI_3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                angle -= PI_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                if (y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    // 00FF00 - FFFF00
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                    rgb = 0x00FF00 | (int)Math.max(0, 255 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                                           (int)(255 * angle / PI_3)) << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                    // FF00FF - 0000FF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                    rgb = 0x0000FF | (int)Math.max(0, 255 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                                           (int)(255 * angle / PI_3)) << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                angle -= 2 * PI_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                if (y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                    // 00FFFF - 00FF00
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                    rgb = 0x00FF00 | (int)Math.min(255,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                                           (int)(255 * angle / PI_3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    // 0000FF - 00FFFF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    rgb = 0x0000FF | (int)Math.min(255,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                                           (int)(255 * angle / PI_3)) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            return rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
         * Increments the hue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        void incrementHue(boolean positive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            float hue = triangle.getGTKColorChooserPanel().getHue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            if (positive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                hue += 1.0f / 360.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                hue -= 1.0f / 360.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            if (hue > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                hue -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            else if (hue < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                hue += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            getGTKColorChooserPanel().setHue(hue, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * Action class used for colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    private static class ColorAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        private int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        ColorAction(String name, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            ColorTriangle triangle = (ColorTriangle)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if (triangle.isWheelFocused()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                float hue = triangle.getGTKColorChooserPanel().getHue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                    triangle.incrementHue(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                    triangle.incrementHue(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                    triangle.focusTriangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    compositeRequestFocus(triangle, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                int xDelta = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                int yDelta = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                    // up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                    yDelta--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                    // down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                    yDelta++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    // left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    xDelta--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    // right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                    xDelta++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    compositeRequestFocus(triangle, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    triangle.focusWheel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                triangle.adjustSB(triangle.getColorX() + xDelta,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                                  triangle.getColorY() + yDelta, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    private class OpaqueLabel extends JLabel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        public boolean isOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
}