Hey guys i am pretty new to java swing and need some help. I am developing a simple color chooser program in swing. I have a color panel that is connected to three sliders. red green and blue. but for some reason i cant get it to work..i will post my code and if anyone has any insight to why my changelistener wont work correctly I would really appreciate it. Thanks.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class SwingTemplate extends JFrame {
private JPanel mainPanel,secondPanel,sliderPanel;
private JLabel colorSquare,colorID;
private Color [] colorArray;
private JLabel [] gridCell;
private JLabel rLabel, gLabel, bLabel;
private JSlider RedS,BlueS,GreenS;
Box horizontalBox;
Box horizontalBox2;
Border thisborder;
public SwingTemplate() {
colorArray = new Color[32];
gridCell = new JLabel[32];
for (int i=0; i<32; i++){
gridCell[i] =new JLabel();
gridCell[i].setOpaque(true);
gridCell[i].addMouseListener(new MyGridCellHandler());
}
setWindowAttributes();
setLookAndFeel();
addMainPanel();
addComponents();
setVisible(true);
}
//////////////////////////////////////////////////////////////////////////////
/// MAIN METHODS ///
//////////////////////////////////////////////////////////////////////////////
private void setWindowAttributes() {
setTitle("Simple Color Chooser");
setSize(700,800);
setLocation(50,50);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
//UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
System.out.println("Sorry, LookAndFeel not found. Available LAFs are\n");
UIManager.LookAndFeelInfo [] x = UIManager.getInstalledLookAndFeels();
for(int i=0; i < x.length; i++)
System.out.println(x[i]);
}
}
private void addMainPanel() {
mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());
mainPanel.setBackground(new Color(240,240,240));
add(mainPanel);
}
private void addComponents() {
///////////////////////////////////////////////////////
//TITLE BAR
JRootPane root = getRootPane();
mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
JMenuBar bar = new JMenuBar();
JMenuItem mi,exitItem;
JMenu menu = new JMenu("File");
bar.add(menu);
menu.add("Clear Contents");
menu.add("Exit");
JMenu menu1 = new JMenu("Help");
bar.add(menu1);
menu1.add("About ColorChooser");
root.setJMenuBar(bar);
mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//COLOR BUTTONS
JPanel panel = new JPanel();
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
horizontalBox = Box.createHorizontalBox();
horizontalBox2 = Box.createHorizontalBox();
horizontalBox.add(Box.createGlue());
horizontalBox.add(new JButton("Yellow"));
horizontalBox.add(new JButton("Orange"));
//horizontalBox.add(new JButton("Red"));
horizontalBox.add(new JButton("Pink"));
horizontalBox.add(new JButton("Teal"));
horizontalBox.add(new JButton("Aqua"));
horizontalBox.add(new JButton("Lime"));
horizontalBox.add(new JButton("Green"));
panel = new JPanel(new BorderLayout());
horizontalBox.add(Box.createGlue());
panel.add(horizontalBox);
panel.setBorder(BorderFactory.createTitledBorder("Light Colors"));
mainPanel.add(panel);
horizontalBox.add(Box.createGlue());
mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
///////////////////////////////////////////////////////////
JPanel p2 = new JPanel();
horizontalBox2=Box.createHorizontalBox();
horizontalBox2.add(new JButton("Black"));
horizontalBox2.add(new JButton("Grey"));
horizontalBox2.add(new JButton("Brown"));
horizontalBox2.add(new JButton("Blue"));
horizontalBox2.add(new JButton("Purple"));
//horizontalBox2.add(new JButton("Green"));
horizontalBox2.add(new JButton("Red"));
p2 = new JPanel(new BorderLayout());
p2.add(horizontalBox2);
p2.setBorder(BorderFactory.createTitledBorder("Dark Colors"));
mainPanel.add(p2);
mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
////////////////////////////////////////////////////////////////
//Color Swatch Panel
JLabel colorSquare = new JLabel();
colorSquare.setOpaque(true);
colorSquare.setPreferredSize(new Dimension(200,200));
colorSquare.setMaximumSize(new Dimension(200,200));
colorSquare.setBackground(Color.white);
mainPanel.add(colorSquare);
colorSquare.setBorder(BorderFactory.createTitledBorder("ColorSwatch"));
mainPanel.add(Box.createRigidArea(new Dimension(5,0)));
///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//color grid
JPanel colorPanel= new JPanel();
colorPanel.setLayout(new GridLayout(4,8));
colorPanel.setPreferredSize(new Dimension (200,200));
//colorPanel.setSize(new Dimension(200,200));
colorPanel.setMaximumSize(new Dimension(200,200));
colorPanel.setBorder(new LineBorder(Color.black,2));
int idx=0;
for (int i =0; i<4; i++)
for (int j=0; j<8;j++)
colorPanel.add(gridCell[idx++],i,j);
fillColorArray();
setGridColors();
//horizontalBox= Box.createHorizontalBox();
//horizontalBox.add(colorPanel);
mainPanel.add(colorPanel);
colorPanel.setBorder(BorderFactory.createTitledBorder("Color Grid"));
mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
//////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//Sliders
JPanel SliderControls = new JPanel();
SliderControls.setLayout(new GridLayout(0,3));
JSlider BlueS = new JSlider(JSlider.HORIZONTAL,0,255,0);
JSlider RedS = new JSlider(JSlider.HORIZONTAL,0,255,0);
JSlider GreenS = new JSlider(JSlider.HORIZONTAL,0,255,0);
BlueS.setBorder(BorderFactory.createTitledBorder("Red"));
BlueS.setMajorTickSpacing(50);
BlueS.setMinorTickSpacing(10);
BlueS.setPaintTicks(true);
BlueS.setPaintLabels(true);
RedS.setBorder(BorderFactory.createTitledBorder("Green"));
RedS.setMajorTickSpacing(50);
RedS.setMinorTickSpacing(10);
RedS.setPaintTicks(true);
RedS.setPaintLabels(true);
GreenS.setBorder(BorderFactory.createTitledBorder("Blue"));
GreenS.setMajorTickSpacing(50);
GreenS.setMinorTickSpacing(10);
GreenS.setPaintTicks(true);
GreenS.setPaintLabels(true);
mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
SliderControls.add(RedS);
SliderControls.add(GreenS);
SliderControls.add(BlueS);
SliderListener listener = new SliderListener();
RedS.addChangeListener(listener);
GreenS.addChangeListener(listener);
BlueS.addChangeListener(listener);
mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
mainPanel.add(SliderControls, BorderLayout.PAGE_END);
mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
////////////////////////////////////////////////////////
//color ID and Random button
////////
mainPanel.add(Box.createRigidArea(new Dimension(40,40)));
colorID = new JLabel("Color Selected: #000000");
colorID.setFont(new Font("Arial",Font.BOLD,24));
colorID.setAlignmentX(Component.CENTER_ALIGNMENT);
mainPanel.add(colorID);
mainPanel.add(Box.createRigidArea(new Dimension(40,40)));
JButton button = new JButton("Click me for new colors");
button.setToolTipText("Clicking on this button generates 32 new random colors");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fillColorArray();
setGridColors();
}
});
mainPanel.add(button);
////////////////////////////////////////////////////////////
}
//////////////////////////////////////////////////////////////////////////////
/// GUI BUILD AND PLACE
//////////////////////////////////////////////////////////////////////////////
private void fillColorArray(){
int red=0,green=0,blue=0;
for(int i=0; i<32; i++) {
red = (int) (256*Math.random());
green = (int) (256*Math.random());
blue = (int) (256*Math.random());
colorArray[i] = new Color(red,green,blue);
//colorArray[i] = new Color(red,green);
}
}
private void setGridColors() {
for (int i=0; i<32; i++) {
gridCell[i].setBackground(colorArray[i]);
gridCell[i].repaint();
}
}
//////////////////////////////////////////////////////////////////////////////
/// HELPER METHODS ///
//////////////////////////////////////////////////////////////////////////////
public class SliderListener implements ChangeListener {
int red, green, blue;
public void stateChanged(ChangeEvent event) {
red = RedS.getValue();
green = GreenS.getValue();
blue = BlueS.getValue();
rLabel.setText("Red: " + red + " Hex: " +
Integer.toHexString(red).toUpperCase());
gLabel.setText("Green: " + green + " Hex: " +
Integer.toHexString(green).toUpperCase());
bLabel.setText("Blue: " + blue + " Hex: " +
Integer.toHexString(blue).toUpperCase());
colorSquare.setBackground(new Color(red, green, blue));
}
}
//////////////////////////////////////////////////////////////////////////////
/// MOUSE HANDLER CLASS ///
class MyGridCellHandler extends MouseAdapter{
public void mouseClicked(MouseEvent e) {
JLabel tmp= (JLabel) e.getSource();
Color c =tmp.getBackground();
String red = Integer.toHexString(c.getRed()).toUpperCase();
String blue=Integer.toHexString(c.getBlue()).toUpperCase();
String green= Integer.toHexString(c.getGreen()).toUpperCase();
if(red.length() !=2) red = "0" + red;
if (green.length() !=2) green = "0" + green;
if (blue.length() !=2) blue = "0" + blue;
colorID.setText("Color Selected: #" +red+green+blue);
colorSquare.setBackground(c);
}
}
//////////////////////////////////////////////////////////////////////////////
public static void main(String [] args) {
new SwingTemplate();
}
}
Comment