I'm trying to write a program that takes color values for red,green, and blue from a slider for each value and then displays it as an rgb value. I'm not exactly sure how to pull the rgb value from the three sliders but I think I am close. I can't figure out how to send the data to the JLabel or if I'm using the correct approach to get the rgb.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.image.ColorModel;
import javax.swing.JSlider;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MyColorChooser extends JFrame {
public final JSlider colorRed;
public static JSlider colorBlue;
public static JSlider colorGreen;
public static JLabel rgbValue;
public static JLabel green;
public static JLabel blue;
public int r;
public int g;
public int b;
Color c;
public Color getC() {
return c;
}
public void setC(Color c) {
this.c = new Color(r, g, b);
}
public MyColorChooser() {
super("Slider Demo");
JPanel Panel1 = new JPanel(new BorderLayout());
JPanel Panel2 = new JPanel(new BorderLayout());
JPanel Panel3 = new JPanel(new BorderLayout());
JPanel Panel4 = new JPanel(new BorderLayout());
rgbValue = new JLabel("RGB Value");
colorRed
= new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
colorBlue
= new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
colorGreen
= new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
colorRed.setMajorTickSpacing(5); // create tick every 10
colorRed.setPaintTicks(true);
colorRed.setPaintLabels(true);// paint ticks on slider
colorBlue.setMajorTickSpacing(5); // create tick every 10
colorBlue.setPaintTicks(true);
colorBlue.setPaintLabels(true);//
colorGreen.setMajorTickSpacing(5); // create tick every 10
colorGreen.setPaintTicks(true);
colorGreen.setPaintLabels(true);//
colorRed.setLabelTable(colorRed.createStandardLabels(10));
colorGreen.setLabelTable(colorGreen.createStandardLabels(10));
colorBlue.setLabelTable(colorBlue.createStandardLabels(10));
Panel1.add(colorRed);
Panel2.add(colorGreen);
Panel3.add(colorBlue);
Panel4.add(rgbValue);
setLayout(new GridLayout(10, 1));
add(Panel1);
add(Panel2);
add(Panel3);
add(Panel4);
colorGreen.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider colorGreen = (JSlider) e.getSource();
g = colorGreen.getValue();; //To change body of generated methods, choose Tools | Templates.
}
});
colorRed.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider colorRed = (JSlider) e.getSource();
r = colorRed.getValue();
}
});
colorBlue.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider colorBlue = (JSlider) e.getSource();
b = colorBlue.getValue();//To change body of generated methods, choose Tools | Templates.
}
});
}
}
I can't figure out how to send the data to the JLabel or if I'm using the correct approach to get the rgb
Well you need to invoke the setText(....)
method on the label if you want the text to change.
So this means you will need to create a method like updateLabel()
that you can invoke from each of the 3 listeners. In that method you will then get the values of your r, g, b, variables and format the string the way you want the text to appear in the label.