jdk/src/share/classes/javax/swing/colorchooser/DiagramComponent.java
author darcy
Sun, 23 Mar 2014 13:49:48 -0700
changeset 23697 e556a715949f
parent 5506 202f599c92aa
permissions -rw-r--r--
8034169: Fix serial lint warnings in javax.swing Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     4
 *
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    10
 *
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    15
 * accompanied this code).
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    16
 *
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
    23
 * questions.
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    24
 */
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    25
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    26
package javax.swing.colorchooser;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    27
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    28
import java.awt.Color;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    29
import java.awt.Graphics;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    30
import java.awt.Insets;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    31
import java.awt.event.MouseEvent;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    32
import java.awt.event.MouseListener;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    33
import java.awt.event.MouseMotionListener;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    34
import java.awt.image.BufferedImage;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    35
import javax.swing.JComponent;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    36
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
    37
@SuppressWarnings("serial") // Superclass is not serializable across versions
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    38
final class DiagramComponent extends JComponent implements MouseListener, MouseMotionListener {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    39
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    40
    private final ColorPanel panel;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    41
    private final boolean diagram;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    42
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    43
    private final Insets insets = new Insets(0, 0, 0, 0);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    44
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    45
    private int width;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    46
    private int height;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    47
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    48
    private int[] array;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    49
    private BufferedImage image;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    50
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    51
    DiagramComponent(ColorPanel panel, boolean diagram) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    52
        this.panel = panel;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    53
        this.diagram = diagram;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    54
        addMouseListener(this);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    55
        addMouseMotionListener(this);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    56
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    57
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    58
    @Override
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    59
    protected void paintComponent(Graphics g) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    60
        getInsets(this.insets);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    61
        this.width = getWidth() - this.insets.left - this.insets.right;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    62
        this.height = getHeight() - this.insets.top - this.insets.bottom;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    63
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    64
        boolean update = (this.image == null)
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    65
                || (this.width != this.image.getWidth())
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    66
                || (this.height != this.image.getHeight());
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    67
        if (update) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    68
            int size = this.width * this.height;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    69
            if ((this.array == null) || (this.array.length < size)) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    70
                this.array = new int[size];
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    71
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    72
            this.image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    73
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    74
        {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    75
            float dx = 1.0f / (float) (this.width - 1);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    76
            float dy = 1.0f / (float) (this.height - 1);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    77
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    78
            int offset = 0;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    79
            float y = 0.0f;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    80
            for (int h = 0; h < this.height; h++, y += dy) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    81
                if (this.diagram) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    82
                    float x = 0.0f;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    83
                    for (int w = 0; w < this.width; w++, x += dx, offset++) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    84
                        this.array[offset] = this.panel.getColor(x, y);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    85
                    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    86
                }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    87
                else {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    88
                    int color = this.panel.getColor(y);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    89
                    for (int w = 0; w < this.width; w++, offset++) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    90
                        this.array[offset] = color;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    91
                    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    92
                }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    93
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    94
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    95
        this.image.setRGB(0, 0, this.width, this.height, this.array, 0, this.width);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    96
        g.drawImage(this.image, this.insets.left, this.insets.top, this.width, this.height, this);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    97
        if (isEnabled()) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    98
            this.width--;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    99
            this.height--;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   100
            g.setXORMode(Color.WHITE);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   101
            g.setColor(Color.BLACK);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   102
            if (this.diagram) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   103
                int x = getValue(this.panel.getValueX(), this.insets.left, this.width);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   104
                int y = getValue(this.panel.getValueY(), this.insets.top, this.height);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   105
                g.drawLine(x - 8, y, x + 8, y);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   106
                g.drawLine(x, y - 8, x, y + 8);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   107
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   108
            else {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   109
                int z = getValue(this.panel.getValueZ(), this.insets.top, this.height);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   110
                g.drawLine(this.insets.left, z, this.insets.left + this.width, z);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   111
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   112
            g.setPaintMode();
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   113
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   114
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   115
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   116
    public void mousePressed(MouseEvent event) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   117
        mouseDragged(event);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   118
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   119
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   120
    public void mouseReleased(MouseEvent event) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   121
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   122
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   123
    public void mouseClicked(MouseEvent event) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   124
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   125
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   126
    public void mouseEntered(MouseEvent event) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   127
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   128
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   129
    public void mouseExited(MouseEvent event) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   130
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   131
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   132
    public void mouseMoved(MouseEvent event) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   133
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   134
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   135
    public void mouseDragged(MouseEvent event) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   136
        if (isEnabled()) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   137
            float y = getValue(event.getY(), this.insets.top, this.height);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   138
            if (this.diagram) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   139
                float x = getValue(event.getX(), this.insets.left, this.width);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   140
                this.panel.setValue(x, y);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   141
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   142
            else {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   143
                this.panel.setValue(y);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   144
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   145
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   146
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   147
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   148
    private static int getValue(float value, int min, int max) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   149
        return min + (int) (value * (float) (max));
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   150
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   151
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   152
    private static float getValue(int value, int min, int max) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   153
        if (min < value) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   154
            value -= min;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   155
            return (value < max)
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   156
                    ? (float) value / (float) max
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   157
                    : 1.0f;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   158
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   159
        return 0.0f;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   160
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   161
}