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