/** * ColorPicker.java 1.00 97/04/12 Merlin Hughes * * Copyright (c) 1997 Prominence Dot Com, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * for commercial and non-commercial purposes and without fee is * hereby granted provided that this copyright notice appears in * all copies. * * http://prominence.com/ merlin@prominence.com */ import java.awt.*; import java.awt.event.MouseEvent; public class ColorPicker extends Canvas { protected static final int LEVELS = 6; protected int r, g, b; public ColorPicker (Color color) { r = color.getRed (); g = color.getGreen (); b = color.getBlue (); enableEvents (AWTEvent.MOUSE_EVENT_MASK); } public ColorPicker () { this (Color.black); } public Dimension getPreferredSize () { return new Dimension (150, 60); } public void paint (Graphics g) { int h = getSize ().width / (LEVELS + 3 + LEVELS); int v = getSize ().height / (LEVELS); for (int red = 0; red < LEVELS; ++ red) { for (int green = 0; green < LEVELS; ++ green) { g.setColor (new Color (red * 255 / (LEVELS - 1), green * 255 / (LEVELS - 1), b)); g.fillRect (red * h, green * v, h, v); } } int x = LEVELS * h + h / 2; int y = v / 2 + v * (b * (LEVELS - 1) / 255); g.setColor (getForeground ()); g.drawLine (x, y, x + 2 * h - 1, y); for (int blue = 0; blue < LEVELS; ++ blue) { g.setColor (new Color (0, 0, blue * 255 / (LEVELS - 1))); g.fillRect ((LEVELS + 1) * h, blue * v, h, v); } g.setColor (new Color (r, this.g, b)); g.fillRect ((LEVELS + 3) * h, 0, h * LEVELS, v * LEVELS); } /* * This code allows us to catch mouse events without registering listeners. */ protected void processMouseEvent (MouseEvent e) { if (e.getID () == MouseEvent.MOUSE_PRESSED) { mousePressed (e); } super.processMouseEvent (e); } public void mousePressed (MouseEvent e) { int h = getSize ().width / (LEVELS + 3 + LEVELS); int v = getSize ().height / (LEVELS); if (e.getX () < LEVELS * h) { // in swatch area r = (e.getX () / h) * 255 / (LEVELS - 1); r = (r < 0) ? 0 : (r > 255) ? 255 : r; g = (e.getY () / v) * 255 / (LEVELS - 1); g = (g < 0) ? 0 : (g > 255) ? 255 : g; repaint (); } else if (e.getX () < (LEVELS + 3) * h) { // in blue bar b = (e.getY () / v) * 255 / (LEVELS - 1); b = (b < 0) ? 0 : (b > 255) ? 255 : b; repaint (); } else { // in select square postColorEvent (); } } /* * This code posts a new ColorEvent to the system event queue. */ protected void postColorEvent () { ColorEvent e = new ColorEvent (this, new Color (r, g, b)); Toolkit toolkit = getToolkit (); EventQueue queue = toolkit.getSystemEventQueue (); queue.postEvent (e); } // alternatively: // dispatchEvent (new ColorEvent (this, new Color (r, g, b))); /* * This code maintains a list of current ColorListeners. */ protected ColorListener colorListener; public synchronized void addColorListener (ColorListener l) { colorListener = ColorEventMulticaster.add (colorListener, l); } public synchronized void removeColorListener (ColorListener l) { colorListener = ColorEventMulticaster.remove (colorListener, l); } /* * This code processes the dispatch of ColorEvents to the ColorListeners. */ protected void processEvent (AWTEvent e) { if (e instanceof ColorEvent) { processColorEvent ((ColorEvent) e); } else { super.processEvent (e); } } protected synchronized void processColorEvent (ColorEvent e) { if (colorListener != null) colorListener.colorPicked (e); } } /* * Using a Vector to store the listeners * protected Vector colorListeners = new Vector (); public void addColorListener (ColorListener l) { colorListeners.addElement (l); } public void removeColorListener (ColorListener l) { colorListeners.removeElement (l); } protected void processColorEvent (ColorEvent e) { synchronized (colorListeners) { for (int i = 0; i < colorListeners.size (); ++ i) { ((ColorListener) colorListeners.elementAt (i)).colorPicked (e); } } } * */