This does nothing of use:
RectPanel rp = new RectPanel();
rp.setRed(redInt = redSlider.getValue());
as you're creating a new JPanel, changing its properties and displaying it nowhere. Instead you should change the state of your already displayed RectPanel JPanel, not create a new one. This means that the reference to the JPanel's who color is to be changed must be passed into your color chooser object so that its state (color) can be changed.
Also you should not be creating a RectPanel within the paintComponent method.
Your problem is equivalent to your owning a blue Ford Mustang, then you go the car shop, buy a new Ford Mustang, have it painted green, and then wonder why the original Mustang is still blue, and this problem stems from a fundamental confusion about what objects are and how they work. Every time you call a class's constructor using the new operator, understand that you are creating a completely new and unique instance of that type of object, and this instance is completely independent from other instances that may have been created before.
I created a working example, but am not going to post the entire thing here, since we don't do homework, but I will post part of it. Note that I created a single JPanel to be used with the 3 different colors, called ColorSliderPanel. I also called my drawing JPanel MyColorPanel.
Portions of the ColorSliderPanel look like this:
public class ColorSliderPanel extends JPanel {
private static final int MAX_COLOR = 255;
private Color color;
private JSlider slider = new JSlider(0, MAX_COLOR, 0);
private JTextField textField = new JTextField("0", 5);
private MyColorPanel myColorPanel;
// here is the key, my constructor takes MyColorPanel reference
public ColorSliderPanel(String text, Color color, MyColorPanel myColorPanel) {
this.color = color;
this.myColorPanel = myColorPanel; // and then sets the field of the class
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(new SliderListener());
// .....
setBorder(BorderFactory.createTitledBorder(text));
// .....
}
private class SliderListener implements ChangeListener {
@Override
public void stateChanged(ChangeEvent cEvt) {
int newValue = slider.getValue();
// call getColor() on the original MyColorPanel object
Color origClr = myColorPanel.getColor(); // get the original color
textField.setText("" + newValue);
// here we decide what the new color's r,g,b should be----
// ......
Color newColor = new Color(r, g, b);
// set the color of the original MyColorPanel object
myColorPanel.setColor(newColor);
}
}
}
Portions of the MyColorPanel. Note that I give it getColor()
and setColor(Color color)
methods, but you can use your individual red/blue/green getters/setters if you wish:
public class MyColorPanel extends JPanel {
private static final Color INIT_COLOR = Color.BLACK;
private static final int RECT_WIDTH = 200;
private Color color = INIT_COLOR;
// ...
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
int rectX = (getWidth() - RECT_WIDTH) / 2;
int rectY = (getHeight() - RECT_WIDTH) / 2;
g.fillRect(rectX, rectY, RECT_WIDTH, RECT_WIDTH);
}
}
The main GUI hooked everything together like so:
public class MyColorFoo extends JPanel {
private MyColorPanel myColorPanel = new MyColorPanel(1000, 400);
public MyColorFoo() {
JPanel slidersPanel = new JPanel(new GridLayout(0, 1, 3, 3));
slidersPanel.add(new ColorSliderPanel("Red", Color.RED, myColorPanel));
slidersPanel.add(new ColorSliderPanel("Green", Color.GREEN, myColorPanel));
slidersPanel.add(new ColorSliderPanel("Blue", Color.BLUE, myColorPanel));
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
setLayout(new BorderLayout(3, 3));
add(myColorPanel, BorderLayout.CENTER);
add(slidersPanel, BorderLayout.PAGE_END);
}
A newer more complete version:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial")
public class ColorExample extends JPanel {
private ColorSliderPanel[] sliders = {
new ColorSliderPanel(Color.RED, "Red"),
new ColorSliderPanel(Color.GREEN, "Green"),
new ColorSliderPanel(Color.BLUE, "Blue") };
private JPanel colorPanel = new JPanel();
public ColorExample() {
JPanel sliderPanel = new JPanel(new GridLayout(0, 1));
for (ColorSliderPanel colorSliderPanel : sliders) {
sliderPanel.add(colorSliderPanel);
colorSliderPanel.addListener(evt -> setColorPanelBackground());
}
colorPanel.setPreferredSize(new Dimension(600, 300));
setLayout(new GridLayout(0, 1));
add(colorPanel);
add(sliderPanel);
setColorPanelBackground();
}
private void setColorPanelBackground() {
int rgb = 0;
for (ColorSliderPanel colorSliderPanel : sliders) {
Color c = colorSliderPanel.getColor();
int value = colorSliderPanel.getValue();
// numeric magic here:
rgb |= value * (0x10101 & c.getRGB());
}
colorPanel.setBackground(new Color(rgb));
}
private static void createAndShowGui() {
ColorExample mainPanel = new ColorExample();
JFrame frame = new JFrame("Color Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
@SuppressWarnings("serial")
class ColorSliderPanel extends JPanel {
private final static int MAX_VALUE = 255;
private Color color;
private String text;
private JSlider slider = new JSlider(0, MAX_VALUE, MAX_VALUE / 2);
public ColorSliderPanel(Color color, String text) {
this.color = color;
this.text = text;
setBackground(color);
// lighter color
int colorInt = color.getRGB() | 0x7f7f7f;
slider.setBackground(new Color(colorInt));
slider.setMajorTickSpacing(50);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
setLayout(new BorderLayout());
add(slider);
setBorder(BorderFactory.createTitledBorder(text));
}
public Color getColor() {
return color;
}
public String getText() {
return text;
}
public int getValue() {
return slider.getValue();
}
public void addListener(ChangeListener listener) {
slider.addChangeListener(listener);
}
}