• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 3
  • 3
1 Solution
 
for_yanCommented:
Do you want it like that?
colorpalet.PNG
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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.
0
 
for_yanCommented:
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
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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                               |
---------------------------------------------------
0
 
for_yanCommented:
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(1,2));
        s1Panel.add(Red);
         s1Panel.add(sRed);
                 s2Panel.setLayout(new GridLayout(1,2));
        s2Panel.add(Green);
         s2Panel.add(sGreen);
                      s3Panel.setLayout(new GridLayout(1,2));
        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.CENTER);
        c.add(colorLablesPanel,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 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


colorpalet2.PNG
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now