Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Color Palet...

Using an existing code in attempt to add labels to the slider and to put the slider at the bottom of the panel, I have done enough damage so that it is no longer working.

Q1. How to stack color labels on top of one another (making sure that similar to the labels, the sliders are also in the order of red, green, and blue from top to down).

Q2. How make it function again.

Q3. How to force color region to expand and cover all of the upperPanel in the code.

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

public class ColorPalet extends JFrame implements ChangeListener {

    JSlider sRed;
    JSlider sGreen;
    JSlider sBlue;
    JPanel colorPanel;
    
    public ColorPalet(){
        
        JPanel upperPanel = new JPanel();
        JPanel sliderPanel = new JPanel();
        JPanel lowerPanel = new JPanel();
        JPanel colorLablesPanel = new JPanel();
        JPanel s1Panel = new JPanel();
        JPanel s2Panel = new JPanel();
        JPanel s3Panel = new JPanel();
        JLabel red = new JLabel("Red");
        JLabel Green = new JLabel("Green");
        JLabel Blue = new JLabel("Blue");
        
        sRed = new JSlider (0,255);
        sGreen = new JSlider (0,255);
        sBlue = new JSlider (0,255);
        sRed.setValue(0);
        sGreen.setValue(0);
        sBlue.setValue(0);

        sRed.addChangeListener(this);
        sGreen.addChangeListener(this);
        sBlue.addChangeListener(this);
      
        colorLablesPanel.setLayout(new GridLayout(1,1));
        colorLablesPanel.add(red, BorderLayout.NORTH);
        colorLablesPanel.add(Green, BorderLayout.CENTER);
        colorLablesPanel.add(Blue, BorderLayout.SOUTH);
        
        sliderPanel.setLayout(new GridLayout(3,1));
        sliderPanel.add(sRed);
        sliderPanel.add(sGreen);
        sliderPanel.add(sBlue);

        // 1st column to hold the labels and the (colorLablesPanel)
        // second column to hold the sliders (sliderPanel)
        lowerPanel.setLayout(new GridLayout(1,2));
        lowerPanel.add(colorLablesPanel);
        lowerPanel.add(sliderPanel);
        
        
        upperPanel= new JPanel();
        upperPanel.setBackground(new Color(0,0,0));

        Container c = this.getContentPane();
        c.setLayout(new BorderLayout());
        c.add(upperPanel, BorderLayout.NORTH);
        c.add(lowerPanel, BorderLayout.SOUTH);

        this.setSize(300,300);
        this.setTitle("Color Palet");
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }

    public void setColor(int i, int j, int k){
        colorPanel.setBackground(new Color(i,j,k));

    }


    public void stateChanged(ChangeEvent ce){

        setColor(sRed.getValue(), sGreen.getValue(), sBlue.getValue());


    }

    public static void main(String[] args) {

        new Exercise17_15();
    }
    
    class UpperPanel{
        public UpperPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,80);
        } 
    }
    
    class LowerPanel{
        public LowerPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,20);
        } 
    }    

}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

Do you want it like that?
colorpalet.PNG
Avatar of Mike Eghtebas

ASKER

no, the upper panel is only for the resultant color based on selections made from the three sliders.

The labels Red, Green, and Blue need to be next to their respective sliders.
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class ColorPalet extends JFrame implements ChangeListener {

    JSlider sRed;
    JSlider sGreen;
    JSlider sBlue;
    JPanel colorPanel;

    public ColorPalet(){

        JPanel upperPanel = new JPanel();
        JPanel sliderPanel = new JPanel();
        JPanel lowerPanel = new JPanel();
        JPanel colorLablesPanel = new JPanel();
        JPanel s1Panel = new JPanel();
        JPanel s2Panel = new JPanel();
        JPanel s3Panel = new JPanel();
        JLabel red = new JLabel("Red");
        JLabel Green = new JLabel("Green");
        JLabel Blue = new JLabel("Blue");

        sRed = new JSlider (0,255);
        sGreen = new JSlider (0,255);
        sBlue = new JSlider (0,255);
        sRed.setValue(0);
        sGreen.setValue(0);
        sBlue.setValue(0);

        sRed.addChangeListener(this);
        sGreen.addChangeListener(this);
        sBlue.addChangeListener(this);

       // colorLablesPanel.setLayout(new GridLayout(3,1));
       // colorLablesPanel.add(red);
       // colorLablesPanel.add(Green);
       // colorLablesPanel.add(Blue);

     //   sliderPanel.setLayout(new GridLayout(3,1));
     //   sliderPanel.add(sRed);
      //  sliderPanel.add(sGreen);
       // sliderPanel.add(sBlue);
           s1Panel.setLayout(new GridLayout(2,1));
        s1Panel.add(red);
         s1Panel.add(sRed);
                 s2Panel.setLayout(new GridLayout(2,1));
        s2Panel.add(Green);
         s2Panel.add(sGreen);
                      s3Panel.setLayout(new GridLayout(2,1));
        s3Panel.add(Blue);
         s3Panel.add(sBlue);
        colorLablesPanel.setLayout(new GridLayout(3,1));
             colorLablesPanel.add(s1Panel);
             colorLablesPanel.add(s2Panel);
             colorLablesPanel.add(s3Panel);




        // 1st column to hold the labels and the (colorLablesPanel)
        // second column to hold the sliders (sliderPanel)
       // lowerPanel.setLayout(new GridLayout(2,1));
       // lowerPanel.add(colorLablesPanel);
        //lowerPanel.add(sliderPanel);


        colorPanel= new JPanel();
        colorPanel.setBackground(new Color(0,0,0));

        Container c = this.getContentPane();
        c.setLayout(new BorderLayout());
        c.add(colorPanel, BorderLayout.NORTH);
        c.add(colorLablesPanel);


        this.setSize(300,300);
        this.setTitle("Color Palet");
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }

    public void setColor(int i, int j, int k){
        colorPanel.setBackground(new Color(i,j,k));

    }


    public void stateChanged(ChangeEvent ce){

        setColor(sRed.getValue(), sGreen.getValue(), sBlue.getValue());


    }

    public static void main(String[] args) {

        new ColorPalet();
    }

    class UpperPanel{
        public UpperPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,80);
        }
    }

    class LowerPanel{
        public LowerPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,20);
        }
    }

}

Open in new window

colorpalet1.PNG
Sorry for the confusion. Here is what I want to build:

---------------------------------------------------
|                                                                     |
|                                                                     |
|                     Color selected to show here         |
|                                                                     |
|                                                                     |
|                                                                     |
---------------------------------------------------
| Red                      slider 1                              |
---------------------------------------------------
| Green                     slider 2                             |
---------------------------------------------------
|  Blue                    Slider 3                               |
---------------------------------------------------
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you