4 /**A window for choosing a colour, either from a pre-set palette
7 class CbColorWindow extends FixedFrame implements CbButtonCallback
9 CbColorWindowCallback callback;
12 static Vector defpal = new Vector();
13 Image palimg[] = new Image[12];
14 CbButton palbut[] = new CbButton[12];
17 CbColorWindowCube ccube;
21 defpal.addElement(Color.black);
22 defpal.addElement(Color.blue);
23 defpal.addElement(Color.cyan);
24 defpal.addElement(Color.gray);
25 defpal.addElement(Color.green);
26 defpal.addElement(Color.darkGray);
27 defpal.addElement(Color.magenta);
28 defpal.addElement(Color.orange);
29 defpal.addElement(Color.pink);
30 defpal.addElement(Color.red);
31 defpal.addElement(Color.white);
32 defpal.addElement(Color.yellow);
35 CbColorWindow(Color c, CbColorWindowCallback cb)
41 pal = callback.palette(this);
44 else if (pal.size() == 0)
45 for(int i=0; i<12; i++)
46 pal.addElement(defpal.elementAt(i));
48 // Create palette images
49 for(int i=0; i<12; i++) {
50 palimg[i] = Util.createImage(16, 16);
55 setLayout(new BorderLayout());
56 Panel bot = new GrayPanel();
57 bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
58 bot.add(ok = new CbButton("Ok", this));
59 bot.add(cancel = new CbButton("Cancel", this));
61 Panel mid = new BorderPanel(1);
62 mid.setLayout(new BorderLayout());
63 Panel midbot = new GrayPanel();
64 midbot.setLayout(new GridLayout(2, 6, 4, 4));
65 CbButtonGroup g = new CbButtonGroup();
66 for(int i=0; i<12; i++) {
67 midbot.add(palbut[i] = new CbButton(palimg[i], this));
68 palbut[i].setGroup(g);
70 for(int i=0; i<12; i++)
71 if (c.equals(pal.elementAt(i))) {
76 mid.add("South", midbot);
77 mid.add("North", ccube = new CbColorWindowCube(this));
82 setTitle("Choose Color...");
87 Graphics g = palimg[i].getGraphics();
88 g.setColor((Color)pal.elementAt(i));
89 g.fillRect(0, 0, 16, 16);
90 if (palbut[i] != null) palbut[i].repaint();
93 public void click(CbButton b)
96 callback.chosen(this, col);
102 for(int i=0; i<12; i++)
103 if (b == palbut[i]) {
105 col = (Color)pal.elementAt(i);
106 ccube.red.setPosition(col.getRed());
107 ccube.blue.setPosition(col.getBlue());
108 ccube.green.setPosition(col.getGreen());
109 ccube.swatch.setColor(col);
114 public void dispose()
117 callback.chosen(this, null);
120 public boolean isResizable() { return false; }
123 /**Displays 3 sliders, for red green and blue plus a block to show the
126 class CbColorWindowCube extends BorderPanel implements CbSliderCallback
128 CbColorWindow parent;
129 CbSlider red, green, blue;
130 CbColorWindowSwatch swatch;
132 CbColorWindowCube(CbColorWindow p)
134 super(1, Util.body, Util.body);
136 setLayout(new BorderLayout());
137 Panel sl = new GrayPanel();
138 sl.setLayout(new GridLayout(3, 1));
139 sl.add(red = new CbSlider(0, 0, 255, p.col.getRed(), this));
140 sl.add(green = new CbSlider(0, 0, 255, p.col.getBlue(), this));
141 sl.add(blue = new CbSlider(0, 0, 255, p.col.getGreen(), this));
143 add("East", swatch = new CbColorWindowSwatch(p.col));
146 public void moved(CbSlider s, int p)
151 public void moving(CbSlider s, int p)
153 parent.col = new Color(red.getPosition(), green.getPosition(),
155 swatch.setColor(parent.col);
156 if (parent.curpal != -1) {
157 parent.pal.setElementAt(parent.col, parent.curpal);
158 parent.updatePal(parent.curpal);
164 interface CbColorWindowCallback
166 /**This method will be called when the user chooses a colour. If
167 * the user cancels the dialog, then this method will also be chosen
168 * but with null for the color.
170 public void chosen(CbColorWindow w, Color c);
172 /**The chooser keeps a palette of colors that the user can modify,
173 * stored in a vector. The callback class should provide this vector
174 * so as to maintain the palette between color window calls.
175 * If an empty vector is returned, it will be filled with the default
176 * color table (which can be then modified).
177 * If null is returned, the chooser will use it's own internal
180 public Vector palette(CbColorWindow w);
184 class CbColorWindowSwatch extends BorderPanel
186 Color col = Color.black;
189 CbColorWindowSwatch(Color c)
195 void setColor(Color c)
198 txt = col.getRed()+","+col.getGreen()+","+col.getBlue();
202 public void paint(Graphics g)
206 g.fillRect(1, 1, size().width-2, size().height-2);
207 g.setColor(Color.white);
208 g.setXORMode(Color.black);
210 g.drawString(txt, 3, Util.fnm.getHeight()+1);
214 public void upate(Graphics g) { paint(g); }
216 public Dimension preferredSize()
218 return new Dimension(60, 60);
221 public Dimension minimumSize()
223 return preferredSize();