Write a java program with three horizontal sliders. Name the sliders, the first one as red, the second one as green and the third one as blue. You design the program such that on varying the individual slider position, you set the color of the panel setBackground(new Color(positionOfSlider1, positionOfSlider2, positionOfSlider3))
Here is a swing example that displays three sliders on the frame and show RGB colors.
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.plaf.basic.*; class Slider extends JSlider{ private String type; private RangeSlider ui; public Slider(int min, int max, String type){ super(min, max); this.type = type; } public void updateUI(){ ui = new RangeSlider(this); setUI(ui); updateLabelUIs(); } public RangeSlider getUI(){ return ui; } public String getType(){ return type; } } class RangeSlider extends BasicSliderUI{ public Color[] gradient = new Color[256]; public RangeSlider(JSlider B){ super(B); } public void updateGradient(int othercolor, int othercolor2){ if ("Red".equals(((Slider) slider).getType())){ for (int i = 0; i < gradient.length; i++){ gradient[i] = new Color(i, othercolor, othercolor2); } } if ("Green".equals(((Slider) slider).getType())){ for (int i = 0; i < gradient.length; i++){ gradient[i] = new Color(othercolor, i, othercolor2); } } if ("Blue".equals(((Slider) slider).getType())){ for (int i = 0; i < gradient.length; i++){ gradient[i] = new Color(othercolor, othercolor2, i); } } slider.repaint(); } } public class ColorSlider extends JFrame implements ChangeListener{ Slider redSlider = new Slider(0, 255, "Red"); Slider greenSlider = new Slider(0, 255, "Green"); Slider blueSlider = new Slider(0, 255, "Blue"); JPanel colourDisplay = new JPanel(); public ColorSlider(){ setVisible(true); setSize(300, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new GridLayout(4, 1)); redSlider.addChangeListener(this); greenSlider.addChangeListener(this); blueSlider.addChangeListener(this); add(redSlider); add(greenSlider); add(blueSlider); add(colourDisplay); } public void stateChanged(ChangeEvent e){ int red = redSlider.getValue(); int green = greenSlider.getValue(); int blue = blueSlider.getValue(); redSlider.getUI().updateGradient(green, blue); greenSlider.getUI().updateGradient(red, blue); blueSlider.getUI().updateGradient(red, green); this.colourDisplay.setBackground(new Color(red, green, blue)); } public static void main(String[] args){ new ColorSlider(); } }
Here is a swing example that displays three sliders on the frame and show RGB colors.
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.plaf.basic.*; class Slider extends JSlider{ private String type; private RangeSlider ui; public Slider(int min, int max, String type){ super(min, max); this.type = type; } public void updateUI(){ ui = new RangeSlider(this); setUI(ui); updateLabelUIs(); } public RangeSlider getUI(){ return ui; } public String getType(){ return type; } } class RangeSlider extends BasicSliderUI{ public Color[] gradient = new Color[256]; public RangeSlider(JSlider B){ super(B); } public void updateGradient(int othercolor, int othercolor2){ if ("Red".equals(((Slider) slider).getType())){ for (int i = 0; i < gradient.length; i++){ gradient[i] = new Color(i, othercolor, othercolor2); } } if ("Green".equals(((Slider) slider).getType())){ for (int i = 0; i < gradient.length; i++){ gradient[i] = new Color(othercolor, i, othercolor2); } } if ("Blue".equals(((Slider) slider).getType())){ for (int i = 0; i < gradient.length; i++){ gradient[i] = new Color(othercolor, othercolor2, i); } } slider.repaint(); } } public class ColorSlider extends JFrame implements ChangeListener{ Slider redSlider = new Slider(0, 255, "Red"); Slider greenSlider = new Slider(0, 255, "Green"); Slider blueSlider = new Slider(0, 255, "Blue"); JPanel colourDisplay = new JPanel(); public ColorSlider(){ setVisible(true); setSize(300, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new GridLayout(4, 1)); redSlider.addChangeListener(this); greenSlider.addChangeListener(this); blueSlider.addChangeListener(this); add(redSlider); add(greenSlider); add(blueSlider); add(colourDisplay); } public void stateChanged(ChangeEvent e){ int red = redSlider.getValue(); int green = greenSlider.getValue(); int blue = blueSlider.getValue(); redSlider.getUI().updateGradient(green, blue); greenSlider.getUI().updateGradient(red, blue); blueSlider.getUI().updateGradient(red, green); this.colourDisplay.setBackground(new Color(red, green, blue)); } public static void main(String[] args){ new ColorSlider(); } }
Ads