Example usage for java.awt Color equals

List of usage examples for java.awt Color equals

Introduction

In this page you can find the example usage for java.awt Color equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Determines whether another object is equal to this Color .

Usage

From source file:MainClass.java

public static void main(String[] a) {
    Color myBlack = new Color(0, 0, 0); // Color black
    Color myWhite = new Color(255, 255, 255); // Color white
    System.out.println(myBlack.equals(myWhite));
}

From source file:CreateColorSamplePopup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JColorChooser Create Popup Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JButton button = new JButton("Pick to Change Background");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Color initialBackground = button.getBackground();

            final JColorChooser colorChooser = new JColorChooser(initialBackground);
            //        colorChooser.setPreviewPanel(new JPanel());
            final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
            previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
            colorChooser.setPreviewPanel(previewLabel);
            // Bug workaround
            colorChooser.updateUI();//from w  w  w .j  a va  2s . c  o m

            // For okay button selection, change button background to
            // selected color
            ActionListener okActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    Color newColor = colorChooser.getColor();
                    if (newColor.equals(button.getForeground())) {
                        System.out.println("Color change rejected");
                    } else {
                        button.setBackground(colorChooser.getColor());
                    }
                }
            };

            // For cancel button selection, change button background to red
            ActionListener cancelActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    button.setBackground(Color.red);
                }
            };

            final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true,
                    colorChooser, okActionListener, cancelActionListener);

            // Wait until current event dispatching completes before showing
            // dialog
            Runnable showDialog = new Runnable() {
                public void run() {
                    dialog.show();
                }
            };
            SwingUtilities.invokeLater(showDialog);
        }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static Element toXML(Color c, Document document) {
    Element result = document.createElement("color");

    String name;/* w  w w  . ja va 2s .  c om*/
    if (c.equals(Color.WHITE)) {
        name = "white";
    } else if (c.equals(Color.CYAN)) {
        name = "cyan";
    } else if (c.equals(Color.YELLOW)) {
        name = "yellow";
    } else if (c.equals(Color.PINK)) {
        name = "pink";
    } else if (c.equals(Color.GREEN)) {
        name = "green";
    } else {
        name = "white";
    }

    result.setAttribute("name", name);

    return result;
}

From source file:Main.java

/**
 * Setups the given table for usage as row-header. This method setups the background color to
 * the same one than the column headers.
 *
 * {@note In a previous version, we were assigning to the row headers the same cell renderer than
 *        the one created by <cite>Swing</cite> for the column headers. But it produced strange
 *        effects when the L&F uses a vertical grandiant instead than a uniform color.}
 *
 * @param  table The table to setup as row headers.
 * @return The renderer which has been assigned to the table.
 *//*w  ww . j av  a  2 s  .  c  o m*/
public static TableCellRenderer setupAsRowHeader(final JTable table) {
    final JTableHeader header = table.getTableHeader();
    Color background = header.getBackground();
    Color foreground = header.getForeground();
    if (background == null || background.equals(table.getBackground())) {
        if (!SystemColor.control.equals(background)) {
            background = SystemColor.control;
            foreground = SystemColor.controlText;
        } else {
            final Locale locale = table.getLocale();
            background = UIManager.getColor("Label.background", locale);
            foreground = UIManager.getColor("Label.foreground", locale);
        }
    }
    final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setBackground(background);
    renderer.setForeground(foreground);
    renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
    final TableColumn column = table.getColumnModel().getColumn(0);
    column.setCellRenderer(renderer);
    column.setPreferredWidth(60);
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setCellSelectionEnabled(false);
    return renderer;
}

From source file:ColorUtils.java

private static boolean equals(Color expected, Color actual, boolean matchBySimilarity) {
    if (matchBySimilarity) {
        return computeHSBDistance(expected, actual) < 0.9;
    } else {//from   w  ww.j a va 2  s .c  om
        return expected.equals(actual);
    }
}

From source file:Main.java

/**
 * Sets the background color for the specified <code>ButtonGroup</code> and
 * all the JCheckBox, JComboBox, JButton, and JRadioButton components that 
 * it contains to the same color.//from   w ww. j  a v  a  2s .  c om
 * 
 * @param buttons the button group to set the background for.
 * @param bg the background color.
 */
public static void setBackground(ButtonGroup buttons, Color bg) {
    Enumeration<?> children = buttons.getElements();
    if (children == null) {
        return;
    }
    Component child;

    if (bg != null) {
        while (children.hasMoreElements()) {
            child = (Component) children.nextElement();
            if (!bg.equals(child.getBackground())
                    && ((child instanceof JCheckBox) || (child instanceof JComboBox)
                            || (child instanceof JButton) || (child instanceof JRadioButton))) {
                child.setBackground(bg);
            }
        }
    }
}

From source file:Main.java

/**
 * Sets the background color for the specified container and all the
 * JPanel, JLabel, JCheckBox, JComboBox, JTextField, JRadioButton, and
 * JScrollPane components that it contains to the same color.
 * //from  ww w.  j  av  a 2s  .co  m
 * @param c the container to set the background color of.
 * @param bg the background color.
 */
public static void setBackground(Container c, Color bg) {
    c.setBackground(bg);
    Component[] children = c.getComponents();
    if (children == null) {
        return;
    }
    Component child;
    int len = children.length;
    if (bg != null) {
        for (int i = 0; i < len; i++) {
            child = children[i];
            if (!bg.equals(child.getBackground())
                    && ((child instanceof JPanel) || (child instanceof JLabel) || (child instanceof JCheckBox)
                            || (child instanceof JComboBox) || (child instanceof JTextField)
                            || (child instanceof JRadioButton) || (child instanceof JScrollPane))) {
                child.setBackground(bg);
            }
        }
    } else {
        // Null background color case
        for (int i = 0; i < len; i++) {
            child = children[i];
            if ((child.getBackground() != null) && ((child instanceof JPanel) || (child instanceof JLabel)
                    || (child instanceof JCheckBox))) {
                child.setBackground(null);
            } //end if
        } // end for
    } //end if else
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

@Nullable
public static Color extractCommonColorForColorChooserButton(@Nonnull final String colorAttribute,
        @Nonnull @MustNotContainNull final Topic[] topics) {
    Color result = null;
    for (final Topic t : topics) {
        final Color color = html2color(t.getAttribute(colorAttribute), false);
        if (result == null) {
            result = color;/*from  w  w  w . j a va 2 s. c om*/
        } else if (!result.equals(color)) {
            return ColorChooserButton.DIFF_COLORS;
        }
    }
    return result;
}

From source file:Main.java

/**
 * Converts a color into a string. If the color is equal to one of the defined
 * constant colors, that name is returned instead. Otherwise the color is
 * returned as hex-string.//from   w w  w .ja v a 2  s.c  o  m
 * 
 * @param c
 *          the color.
 * @return the string for this color.
 */
public static String colorToString(final Color c) {
    try {
        final Field[] fields = Color.class.getFields();
        for (int i = 0; i < fields.length; i++) {
            final Field f = fields[i];
            if (Modifier.isPublic(f.getModifiers()) && Modifier.isFinal(f.getModifiers())
                    && Modifier.isStatic(f.getModifiers())) {
                final String name = f.getName();
                final Object oColor = f.get(null);
                if (oColor instanceof Color) {
                    if (c.equals(oColor)) {
                        return name;
                    }
                }
            }
        }
    } catch (Exception e) {
        //
    }

    // no defined constant color, so this must be a user defined color
    final String color = Integer.toHexString(c.getRGB() & 0x00ffffff);
    final StringBuffer retval = new StringBuffer(7);
    retval.append("#");

    final int fillUp = 6 - color.length();
    for (int i = 0; i < fillUp; i++) {
        retval.append("0");
    }

    retval.append(color);
    return retval.toString();
}

From source file:com.igormaznitsa.ideamindmap.utils.IdeaUtils.java

public static Color extractCommonColorForColorChooserButton(final String colorAttribute, final Topic[] topics) {
    Color result = null;
    for (final Topic t : topics) {
        final Color color = html2color(t.getAttribute(colorAttribute), false);
        if (result == null) {
            result = color;//from  ww w. ja v a  2s. c  o  m
        } else {
            if (!result.equals(color)) {
                return ColorChooserButton.DIFF_COLORS;
            }
        }
    }
    return result;
}