Hi all, I cant get my brain around the logic of making a check box add a specific color when it is checked. Also, when the other boxes are check, it will add that color to the selected color as well. here is my code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class ColorViewerFrame extends JFrame
{
public ColorViewerFrame()
{
colorPanel = new JPanel();
add(colorPanel, BorderLayout.CENTER);
createControlPanel();
//setSampleColor();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void createControlPanel()
{
class ColorListener implements ChangeListener
{
public void stateChanged(ChangeEvent event)
{
//setSampleColor();
}
}
ChangeListener listener = new ColorListener();
redCheckBox = new JCheckBox("Red");
redCheckBox.addChangeListener(listener);
greenCheckBox = new JCheckBox("Green");
greenCheckBox.addChangeListener(listener);
blueCheckBox = new JCheckBox("Blue");
blueCheckBox.addChangeListener(listener);
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(redCheckBox);
controlPanel.add(greenCheckBox);
controlPanel.add(blueCheckBox);
add(controlPanel, BorderLayout.SOUTH);
}
/**
Reads the check box values and sets the panel to
the selected color.
public void setSampleColor()
{
// Read CheckBox values
if(redCheckBox.isSelected())
colorPanel.setBackground(255, 0, 0);
if(redCheckBox.isSelected() ++ greenCheckBox.isSelected())
colorPanel.setBackground(255, 225, 0);
if(redCheckBox.isSelected() ++ blueCheckBox.isSelected())
colorPanel.setBackground(255, 0, 255);
if(greenCheckBox.isSelected())
colorPanel.setBackground(0, 255, 0);
if(blueCheckBox.isSelected())
colorPanel.setBackground(0, 0, 255);
// Set panel background to selected color
//colorPanel.setBackground(0, 0, 0);
colorPanel.repaint();
}
*/
private JPanel colorPanel;
private JCheckBox redCheckBox;
private JCheckBox greenCheckBox;
private JCheckBox blueCheckBox;
import javax.swing.JFrame;
// new class
public class ColorViewer
{
public static void main(String[] args)
{
ColorViewerFrame frame = new ColorViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
any help would be super!
Cheers!