import java.awt.*; public class ColorSelector extends Panel { private Color color = Color.black; private int width = 15, height = 17, nColors = 0, selected = 0; private String dir = "horizontal"; private Color availColors[] = {new Color(0,0,0), new Color(255,255,255), new Color(255,255,0), new Color(192,192,192), new Color(0,255,255), new Color(0,255,0), new Color(128,128,128), new Color(128,128,0), new Color(255,0,255), new Color(0,128,128), new Color(255,0,0), new Color(0,128,0), new Color(128,0,128), new Color(128,0,0), new Color(0,0,255), new Color(0,0,128) }; public ColorSelector() { super(); nColors = availColors.length; } public ColorSelector(String _dir) { super(); nColors = availColors.length; if (_dir.equalsIgnoreCase("vertical")) dir = "vertical"; } public ColorSelector(int _width, int _height, String _dir) { super(); width = _width; height = _height; nColors = availColors.length; if (_dir.equalsIgnoreCase("vertical")) dir = "vertical"; } public Dimension minimumSize() { return preferredSize(); } public Dimension preferredSize() { if (dir.equals("horizontal")) return new Dimension((width+3)*nColors + 2,height+6); else return new Dimension(width+6, (height+3) * nColors + 2); } public Color getColor() { return color; } public void paint(Graphics g) { for (int i = 0; i < nColors; i++) { g.setColor(availColors[i]); if (dir.equals("horizontal")) g.fill3DRect(i*(width+3) + 2,2, width,height,true); else g.fill3DRect(2, i*(height+3) + 2, width, height,true); } g.setColor(Color.black); if (dir.equals("horizontal")) g.drawRect(selected * (width+3), 0, width+3, height+3); else g.drawRect(0, selected * (height+3), width+3, height+3); } public boolean mouseDown(Event e, int x, int y) { boolean validColor = false; if ((dir.equals("horizontal")) && (y <= height + 3) && ((x / (width + 3)) < nColors)) { validColor = true; selected = x / (width + 3); } if ((dir.equals("vertical")) && (x <= width + 3) && ((y / (height + 3)) < nColors)) { validColor = true; selected = y / (height + 3); } if (validColor) { color = availColors[selected]; repaint(); postEvent(new Event(this,Event.ACTION_EVENT, e.arg)); return true; } else return super.mouseDown(e,x,y); } }