javaswingjcolorchooser

JColorChoose, similar colors exception


I have to write a program in which JColorChooser is opened twice which will allow the user to select background and foreground colors. However, if the user selects the colors which are too similar the program should throw out an error message and open the JColorChoosers again...

I know that I have to use the VetoableChangeListener but I have no idea how to implement it into my code...

Below the code which I have up to this point.

Help would be greatly appreciated

   package test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class jcolortest{

   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public jcolortest(){
      prepareGUI();
   }

   public static void main(String[] args){
      jcolortest  swingControlDemo = new jcolortest();      
      swingControlDemo.showColorChooserDemo();
   }

   private void prepareGUI(){
      mainFrame = new JFrame("JColorChooser");
      mainFrame.setSize(400,200);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 



      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    

      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }




   private void showColorChooserDemo(){
      headerLabel.setText("Test foreground text");
      headerLabel.setForeground(Color.RED);

            Color backgroundColor = JColorChooser.showDialog(mainFrame,
               "Background Color", Color.white);
            if(backgroundColor != null){
               controlPanel.setBackground(backgroundColor);
               mainFrame.getContentPane().setBackground(backgroundColor);
            }

            Color foregroundColor = JColorChooser.showDialog(mainFrame,
               "Text Color", Color.white);
            if(foregroundColor != Color.RED){
               controlPanel.setForeground(foregroundColor);
               headerLabel.setForeground(foregroundColor);


         }

      mainFrame.setVisible(true);  
   }


}

Solution

  • In such case I would propably use something like that:

    Color foregroundColor;
    do{                           //loop as long as colors are similar
        foregroundColor = JColorChooser.showDialog(mainFrame,
        "Text Color", Color.white);
    }while (isSimilar(backgroundColor, foregroundColor));
    

    and add mathod:

    public boolean isSimilar(Color background, Color foreground){
        int r1 = background.getRed();
        int g1 = background.getGreen();
        int b1 = background.getBlue();
    
        int r2 = foreground.getRed();
        int g2 = foreground.getGreen();
        int b2 = foreground.getBlue();
    
        if((r2 >= r1+150 || r2 <= r1-150) || (g2 >= g1+150 || g2 <= g1-150) || (b2 >= b1+150 || b2 <= b1-150)){
            return false;             // 150 controls the "sensitivity"
    
        }
        JOptionPane.showMessageDialog(mainFrame,"Too similar color","Error",JOptionPane.INFORMATION_MESSAGE);
        return true;
    }
    

    nothing fancy, and it is rather primitive, but it works in some degree. The problem is to set "sensitivity" on a satisfying level.