public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof TextField) { TextField tf = (TextField) e.getSource(); String text = tf.getText(); String fields[] = FILE.split(text, " ,"); int r = 0; int g = 0; int b = 0; if (fields.length == 3) { // assumed decimal rgb values r = FILE.readInteger(fields[0]); g = FILE.readInteger(fields[1]); b = FILE.readInteger(fields[2]); } else if (fields.length == 1) { // assume 6 digit hex code int crgb = Color32.getColorFromName(fields[0]); r = (crgb & 0xff0000) >> 16; g = (crgb & 0xff00) >> 8; b = (crgb & 0xff); } else { Log.error("illegal colour format"); Log.error("should be 0xabcdef or 255 0 255 or colorname"); return; } Color c = new Color(r, g, b); updateColor(c, e.getSource()); } }
public ColorChooser(Dialog d) { dialog = d; JCContainer spinboxContainer = new JCContainer(); cs = new ColorSpace(); cs.addMouseListener(this); cs.addMouseMotionListener(this); Layout.fill(this, cs, 0, 0, GridBagConstraints.BOTH); JCLabel redLabel = new JCLabel("Red"); red = new JCSpinBox(3); red.setAutoArrowDisable(false); red.setMinimum(0); red.setMaximum(255); JCLabel greenLabel = new JCLabel("Green"); green = new JCSpinBox(3); green.setMinimum(0); green.setMaximum(255); JCLabel blueLabel = new JCLabel("Blue"); blue = new JCSpinBox(3); blue.setMinimum(0); blue.setMaximum(255); Layout.fill(spinboxContainer, redLabel, 0, 0, GridBagConstraints.NONE); Layout.fill(spinboxContainer, red, 1, 0, GridBagConstraints.NONE); Layout.fill(spinboxContainer, greenLabel, 0, 1, GridBagConstraints.NONE); Layout.fill(spinboxContainer, green, 1, 1, GridBagConstraints.NONE); Layout.fill(spinboxContainer, blueLabel, 0, 2, GridBagConstraints.NONE); Layout.fill(spinboxContainer, blue, 1, 2, GridBagConstraints.NONE); JCLabel thisColor = new JCLabel("New"); colorSample = new Panel(); // common colours JCContainer commonContainer = new JCContainer(); String commonColors[] = { "red", "orange", "brown", "yellow", "green", "blue", "cyan", "magenta", "purple", "white", "lightgrey", "grey", "darkgrey", "black" }; for (int i = 0; i < commonColors.length; i++) { JCButton b = new JCButton(); b.setPreferredSize(2, 10); b.setBackground(new Color(Color32.getColorFromName(commonColors[i]))); b.addActionListener(this); Layout.fill(commonContainer, b, i, 0, GridBagConstraints.NONE); } Layout.fill(this, commonContainer, 0, 1, GridBagConstraints.HORIZONTAL); JCLabel hexLabel = new JCLabel("Hex"); hexText = new TextField(""); hexText.setColumns(6); hexText.setFont(new Font("courier", Font.PLAIN, 9)); hexText.addActionListener(this); Layout.fill(spinboxContainer, hexLabel, 0, 4, GridBagConstraints.HORIZONTAL); Layout.fill(spinboxContainer, hexText, 1, 4, GridBagConstraints.HORIZONTAL); Layout.fill(spinboxContainer, thisColor, 0, 5, GridBagConstraints.HORIZONTAL); Layout.fill(spinboxContainer, colorSample, 1, 5, GridBagConstraints.HORIZONTAL); Layout.fill(this, spinboxContainer, 1, 0, GridBagConstraints.NONE); JCContainer buttonContainer = new JCContainer(); ok = new JCButton("OK"); ok.addActionListener(this); cancel = new JCButton("Cancel"); cancel.addActionListener(this); Layout.fill(buttonContainer, ok, 0, 0, GridBagConstraints.NONE); Layout.fill(buttonContainer, cancel, 1, 0, GridBagConstraints.NONE); Layout.fill(this, buttonContainer, 0, 2, GridBagConstraints.NONE); doLayout(); }