1 /* 2 * Copyright (c) 2008, 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 final class DiagramComponent extends JComponent implements MouseListener, MouseMotionListener { 38 39 private final ColorPanel panel; 40 private final boolean diagram; 41 42 private final Insets insets = new Insets(0, 0, 0, 0); 43 44 private int width; 45 private int height; 46 47 private int[] array; 48 private BufferedImage image; 49 50 DiagramComponent(ColorPanel panel, boolean diagram) { 51 this.panel = panel; 52 this.diagram = diagram; 53 addMouseListener(this); 54 addMouseMotionListener(this); 55 } 56 57 @Override 58 protected void paintComponent(Graphics g) { 59 getInsets(this.insets); 60 this.width = getWidth() - this.insets.left - this.insets.right; 61 this.height = getHeight() - this.insets.top - this.insets.bottom; 62 63 boolean update = (this.image == null) 64 || (this.width != this.image.getWidth()) 65 || (this.height != this.image.getHeight()); 66 if (update) { 67 int size = this.width * this.height; 68 if ((this.array == null) || (this.array.length < size)) { 69 this.array = new int[size]; 70 } 71 this.image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB); 72 } 73 { 74 float dx = 1.0f / (float) (this.width - 1); 75 float dy = 1.0f / (float) (this.height - 1); 76 77 int offset = 0; 78 float y = 0.0f; 79 for (int h = 0; h < this.height; h++, y += dy) { 80 if (this.diagram) { 81 float x = 0.0f; 82 for (int w = 0; w < this.width; w++, x += dx, offset++) { 83 this.array[offset] = this.panel.getColor(x, y); 84 } 85 } 86 else { 87 int color = this.panel.getColor(y); 88 for (int w = 0; w < this.width; w++, offset++) { 89 this.array[offset] = color; 90 } 91 } 92 } 93 } 94 this.image.setRGB(0, 0, this.width, this.height, this.array, 0, this.width); 95 g.drawImage(this.image, this.insets.left, this.insets.top, this.width, this.height, this); 96 if (isEnabled()) { 97 this.width--; 98 this.height--; 99 g.setXORMode(Color.WHITE); 100 g.setColor(Color.BLACK); 101 if (this.diagram) { 102 int x = getValue(this.panel.getValueX(), this.insets.left, this.width); 103 int y = getValue(this.panel.getValueY(), this.insets.top, this.height); 104 g.drawLine(x - 8, y, x + 8, y); 105 g.drawLine(x, y - 8, x, y + 8); 106 } 107 else { 108 int z = getValue(this.panel.getValueZ(), this.insets.top, this.height); 109 g.drawLine(this.insets.left, z, this.insets.left + this.width, z); 110 } 111 g.setPaintMode(); 112 } 113 } 114 115 public void mousePressed(MouseEvent event) { 116 mouseDragged(event); 117 } 118 119 public void mouseReleased(MouseEvent event) { 120 } 121 122 public void mouseClicked(MouseEvent event) { 123 } 124 125 public void mouseEntered(MouseEvent event) { 126 } 127 128 public void mouseExited(MouseEvent event) { 129 } 130 131 public void mouseMoved(MouseEvent event) { 132 } 133 134 public void mouseDragged(MouseEvent event) { 135 if (isEnabled()) { 136 float y = getValue(event.getY(), this.insets.top, this.height); 137 if (this.diagram) { 138 float x = getValue(event.getX(), this.insets.left, this.width); 139 this.panel.setValue(x, y); 140 } 141 else { 142 this.panel.setValue(y); 143 } 144 } 145 } 146 147 private static int getValue(float value, int min, int max) { 148 return min + (int) (value * (float) (max)); 149 } 150 151 private static float getValue(int value, int min, int max) { 152 if (min < value) { 153 value -= min; 154 return (value < max) 155 ? (float) value / (float) max 156 : 1.0f; 157 } 158 return 0.0f; 159 } 160 }