Java slider loop

I'm trying to create a program that changes RGB to CMYK with sliders, but I ran into a problem: when changing RGB, the CMYK sliders should change accordingly, but then the CMYK change happens, so I go back and forth the action. How to fix it?

Complete code:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;

public class MainWND {
    int i_R, i_G, i_B;
    float i_C, i_M, i_Y, i_K;
    boolean changedRGB, changedCMYK = false;

    private JFrame frame;
    private JSlider sliderR;
    private JSlider sliderG;
    private JSlider sliderB;
    private JSlider sliderC;
    private JSlider sliderM;
    private JSlider sliderY;
    private JSlider sliderK;
    private JLabel lblNewLabel;
    private JLabel lblNewLabel_1;
    private JLabel lbR;
    private JLabel lbG;
    private JLabel lbB;
    private JLabel lbC;
    private JLabel lbM;
    private JLabel lbY;
    private JLabel lbK;
    private JLabel lblNewLabel_9;
    private JLabel lblNewLabel_10;
    private JLabel lblNewLabel_11;
    private JLabel lblNewLabel_12;
    private JLabel lblNewLabel_13;
    private JLabel lblNewLabel_14;
    private JLabel lblNewLabel_15;
    private JPanel panel;

    public void rgb_to_cmyk() {

        i_K = Math.min(Math.min(255 - i_R, 255 - i_G), 255 - i_B);
        // System.out.println("! " + i_K + " !");
        // System.out.println("! " + (int)i_K + " !");
        if (i_K != 255) {
            i_C = (255 - i_R - i_K) / (255 - i_K) * 255;
            i_M = (255 - i_G - i_K) / (255 - i_K) * 255;
            i_Y = (255 - i_B - i_K) / (255 - i_K) * 255;
        } else {
            i_C = 255 - i_R;
            i_M = 255 - i_G;
            i_Y = 255 - i_B;
        }
        // System.out.println(i_C+" "+i_M+" "+i_Y+" "+i_K);

    }

    public void cmyk_to_rgb() {
        if (i_K != 255) {
            i_R = (int) ((255 - i_C) * (255 - i_K)) / 255;
            i_G = (int) ((255 - i_M) * (255 - i_K)) / 255;
            i_B = (int) ((255 - i_Y) * (255 - i_K)) / 255;
        } else {
            i_R = (int) (255 - i_C);
            i_G = (int) (255 - i_M);
            i_B = (int) (255 - i_Y);
        }
    }

    public void update() {
        sliderR.setValue(i_R);
        sliderG.setValue(i_G);
        sliderB.setValue(i_B);
        sliderC.setValue((int) i_C);
        sliderM.setValue((int) (i_M));
        sliderY.setValue((int) (i_Y));
        sliderK.setValue((int) (i_K));
        lbR.setText(Integer.toString(i_R));
        panel.setBackground(new Color(i_R, i_G, i_B));

        System.out.println("RGB " + changedRGB + " CMYK " + changedCMYK);

    }

    public class rgbListener implements ChangeListener {

        @Override
        public void stateChanged(ChangeEvent arg0) {

            i_R = sliderR.getValue();
            i_G = sliderG.getValue();
            i_B = sliderB.getValue();

            rgb_to_cmyk();
            update();

        }

    }

    public class cmykListener implements ChangeListener {

        @Override
        public void stateChanged(ChangeEvent arg0) {

            i_C = sliderC.getValue();
            i_M = sliderM.getValue();
            i_Y = sliderY.getValue();
            i_K = sliderK.getValue();
            cmyk_to_rgb();
            update();

        }

    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainWND window = new MainWND();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MainWND() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 620, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[] { 0, 150, 0, -48, 197, 33, 0 };
        gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 83, 0, 0 };
        gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0, 0.0,
                0.0, Double.MIN_VALUE };
        gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
                0.0, Double.MIN_VALUE };
        frame.getContentPane().setLayout(gridBagLayout);

        lblNewLabel = new JLabel("RGB");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel.gridx = 1;
        gbc_lblNewLabel.gridy = 0;
        frame.getContentPane().add(lblNewLabel, gbc_lblNewLabel);

        lblNewLabel_1 = new JLabel("CMYK");
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_1.gridx = 4;
        gbc_lblNewLabel_1.gridy = 0;
        frame.getContentPane().add(lblNewLabel_1, gbc_lblNewLabel_1);

        lblNewLabel_9 = new JLabel("R");
        GridBagConstraints gbc_lblNewLabel_9 = new GridBagConstraints();
        gbc_lblNewLabel_9.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_9.gridx = 0;
        gbc_lblNewLabel_9.gridy = 1;
        frame.getContentPane().add(lblNewLabel_9, gbc_lblNewLabel_9);

        sliderR = new JSlider();
        sliderR.setValue(0);
        sliderR.setMaximum(255);
        GridBagConstraints gbc_sliderR = new GridBagConstraints();
        gbc_sliderR.insets = new Insets(0, 0, 5, 5);
        gbc_sliderR.gridx = 1;
        gbc_sliderR.gridy = 1;
        sliderR.addChangeListener(new rgbListener());
        frame.getContentPane().add(sliderR, gbc_sliderR);

        lbR = new JLabel("New label");
        GridBagConstraints gbc_lbR = new GridBagConstraints();
        gbc_lbR.insets = new Insets(0, 0, 5, 5);
        gbc_lbR.gridx = 2;
        gbc_lbR.gridy = 1;
        frame.getContentPane().add(lbR, gbc_lbR);

        lblNewLabel_12 = new JLabel("C");
        GridBagConstraints gbc_lblNewLabel_12 = new GridBagConstraints();
        gbc_lblNewLabel_12.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_12.gridx = 3;
        gbc_lblNewLabel_12.gridy = 1;
        frame.getContentPane().add(lblNewLabel_12, gbc_lblNewLabel_12);

        sliderC = new JSlider();
        sliderC.setValue(0);
        sliderC.setMaximum(255);
        GridBagConstraints gbc_sliderC = new GridBagConstraints();
        gbc_sliderC.insets = new Insets(0, 0, 5, 5);
        gbc_sliderC.gridx = 4;
        gbc_sliderC.gridy = 1;
        sliderC.addChangeListener(new cmykListener());
        frame.getContentPane().add(sliderC, gbc_sliderC);

        lbC = new JLabel("New label");
        GridBagConstraints gbc_lbC = new GridBagConstraints();
        gbc_lbC.insets = new Insets(0, 0, 5, 0);
        gbc_lbC.gridx = 5;
        gbc_lbC.gridy = 1;
        frame.getContentPane().add(lbC, gbc_lbC);

        lblNewLabel_10 = new JLabel("G");
        GridBagConstraints gbc_lblNewLabel_10 = new GridBagConstraints();
        gbc_lblNewLabel_10.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_10.gridx = 0;
        gbc_lblNewLabel_10.gridy = 2;
        frame.getContentPane().add(lblNewLabel_10, gbc_lblNewLabel_10);

        sliderG = new JSlider();
        sliderG.setValue(0);
        sliderG.setMaximum(255);
        GridBagConstraints gbc_sliderG = new GridBagConstraints();
        gbc_sliderG.insets = new Insets(0, 0, 5, 5);
        gbc_sliderG.gridx = 1;
        gbc_sliderG.gridy = 2;
        sliderG.addChangeListener(new rgbListener());
        frame.getContentPane().add(sliderG, gbc_sliderG);

        lbG = new JLabel("New label");
        GridBagConstraints gbc_lbG = new GridBagConstraints();
        gbc_lbG.insets = new Insets(0, 0, 5, 5);
        gbc_lbG.gridx = 2;
        gbc_lbG.gridy = 2;
        frame.getContentPane().add(lbG, gbc_lbG);

        lblNewLabel_13 = new JLabel("M");
        GridBagConstraints gbc_lblNewLabel_13 = new GridBagConstraints();
        gbc_lblNewLabel_13.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_13.gridx = 3;
        gbc_lblNewLabel_13.gridy = 2;
        frame.getContentPane().add(lblNewLabel_13, gbc_lblNewLabel_13);

        sliderM = new JSlider();
        sliderM.setValue(0);
        sliderM.setMaximum(255);
        GridBagConstraints gbc_sliderM = new GridBagConstraints();
        gbc_sliderM.insets = new Insets(0, 0, 5, 5);
        gbc_sliderM.gridx = 4;
        gbc_sliderM.gridy = 2;
        sliderM.addChangeListener(new cmykListener());
        frame.getContentPane().add(sliderM, gbc_sliderM);

        lbM = new JLabel("New label");
        GridBagConstraints gbc_lbM = new GridBagConstraints();
        gbc_lbM.insets = new Insets(0, 0, 5, 0);
        gbc_lbM.gridx = 5;
        gbc_lbM.gridy = 2;
        frame.getContentPane().add(lbM, gbc_lbM);

        lblNewLabel_11 = new JLabel("B");
        GridBagConstraints gbc_lblNewLabel_11 = new GridBagConstraints();
        gbc_lblNewLabel_11.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_11.gridx = 0;
        gbc_lblNewLabel_11.gridy = 3;
        frame.getContentPane().add(lblNewLabel_11, gbc_lblNewLabel_11);

        sliderB = new JSlider();
        sliderB.setValue(0);
        sliderB.setMaximum(255);
        GridBagConstraints gbc_sliderB = new GridBagConstraints();
        gbc_sliderB.insets = new Insets(0, 0, 5, 5);
        gbc_sliderB.gridx = 1;
        gbc_sliderB.gridy = 3;
        sliderB.addChangeListener(new rgbListener());
        frame.getContentPane().add(sliderB, gbc_sliderB);

        lbB = new JLabel("New label");
        GridBagConstraints gbc_lbB = new GridBagConstraints();
        gbc_lbB.insets = new Insets(0, 0, 5, 5);
        gbc_lbB.gridx = 2;
        gbc_lbB.gridy = 3;
        frame.getContentPane().add(lbB, gbc_lbB);

        lblNewLabel_14 = new JLabel("Y");
        GridBagConstraints gbc_lblNewLabel_14 = new GridBagConstraints();
        gbc_lblNewLabel_14.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_14.gridx = 3;
        gbc_lblNewLabel_14.gridy = 3;
        frame.getContentPane().add(lblNewLabel_14, gbc_lblNewLabel_14);

        sliderY = new JSlider();
        sliderY.setValue(0);
        sliderY.setMaximum(255);
        GridBagConstraints gbc_sliderY = new GridBagConstraints();
        gbc_sliderY.insets = new Insets(0, 0, 5, 5);
        gbc_sliderY.gridx = 4;
        gbc_sliderY.gridy = 3;
        sliderY.addChangeListener(new cmykListener());
        frame.getContentPane().add(sliderY, gbc_sliderY);

        lbY = new JLabel("New label");
        GridBagConstraints gbc_lbY = new GridBagConstraints();
        gbc_lbY.insets = new Insets(0, 0, 5, 0);
        gbc_lbY.gridx = 5;
        gbc_lbY.gridy = 3;
        frame.getContentPane().add(lbY, gbc_lbY);

        lblNewLabel_15 = new JLabel("K");
        GridBagConstraints gbc_lblNewLabel_15 = new GridBagConstraints();
        gbc_lblNewLabel_15.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_15.gridx = 3;
        gbc_lblNewLabel_15.gridy = 4;
        frame.getContentPane().add(lblNewLabel_15, gbc_lblNewLabel_15);

        sliderK = new JSlider();
        sliderK.setValue(255);
        sliderK.setMaximum(255);
        GridBagConstraints gbc_sliderK = new GridBagConstraints();
        gbc_sliderK.insets = new Insets(0, 0, 5, 5);
        gbc_sliderK.gridx = 4;
        gbc_sliderK.gridy = 4;
        sliderK.addChangeListener(new cmykListener());
        frame.getContentPane().add(sliderK, gbc_sliderK);

        lbK = new JLabel("New label");
        GridBagConstraints gbc_lbK = new GridBagConstraints();
        gbc_lbK.insets = new Insets(0, 0, 5, 0);
        gbc_lbK.gridx = 5;
        gbc_lbK.gridy = 4;
        frame.getContentPane().add(lbK, gbc_lbK);

        panel = new JPanel();

        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.gridheight = 2;
        gbc_panel.gridwidth = 6;
        gbc_panel.insets = new Insets(0, 0, 0, 5);
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 5;
        frame.getContentPane().add(panel, gbc_panel);

    }

}

      

+3


source to share


1 answer


Inactivate each inverse ChangeListener inside another ChangeListener before setting its model value, and then reactivate the other listener.



+2


source







All Articles