Wednesday, August 13, 2014

ColorMixer GUI Application Swing [Java]

The application has three JSliders that represent the color values (Red, Green, Blue). As soon as the
value is changed by any of the slider, the new mixed color gets displayed on a panel. A status bar shows
the current mixing of color values.


Code:

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

class ColorMixer extends JFrame {

    class DrawPanel extends JPanel {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            setBackground(new Color(redValue, greenValue, blueValue));
            repaint();
        }
    }

    JSlider red, green, blue;
    JPanel sliderPanel;
    DrawPanel display;
    JTextField status;
    int redValue = 0, greenValue = 0, blueValue = 0;

    {
        red = new JSlider(0, 255, 0);
        green = new JSlider(0, 255, 0);
        blue = new JSlider(0, 255, 0);

        sliderPanel = new JPanel();
        display = new DrawPanel();

        status = new JTextField();
        status.setEditable(false);
        status.setText("Red : " + redValue + " Green : " + greenValue + " Blue : " + blueValue);
    }

    ColorMixer() {
        setLayout(new GridLayout(2, 1));
        add(display);
        add(sliderPanel);

        sliderPanel.add(status);
        sliderPanel.add(red);
        sliderPanel.add(green);
        sliderPanel.add(blue);

        sliderPanel.setLayout(new GridLayout(4, 1));

        SliderListener changeListener = new SliderListener();
        red.addChangeListener(changeListener);
        green.addChangeListener(changeListener);
        blue.addChangeListener(changeListener);

        
        setTitle("Color Mixer");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    class SliderListener implements ChangeListener {

        @Override
        public void stateChanged(ChangeEvent e) {
     
            if (e.getSource() == red) {
                redValue = ((JSlider) e.getSource()).getValue();
            } else if (e.getSource() == green) {
                greenValue = ((JSlider) e.getSource()).getValue();
            } else if (e.getSource() == blue) {
                blueValue = ((JSlider) e.getSource()).getValue();
            }
            status.setText("Red : " + redValue + " Green : " + greenValue + " Blue : " + blueValue);
            display.repaint();
        }
    }
}

public class ColorMixerRunner {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        System.out.println(info.getClassName());
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e) {
                    // If Nimbus is not available, you can set the GUI to another look and feel.
                }
            }
        });
        ColorMixer cm = new ColorMixer();
    }
}

No comments:

Post a Comment