?
Solved

Color Palet...

Posted on 2011-10-12
6
Medium Priority
?
194 Views
Last Modified: 2012-05-12
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
Comment
Question by:Mike Eghtebas
  • 3
  • 3
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36959536
Do you want it like that?
colorpalet.PNG
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36959583
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36959631
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36959663
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
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36959684
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
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 36959691
Thank you
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question