Viewing file: comp249/assignment2/SwatchPanel.java | Back to directory listing
Author: Loren Segal | Last modified: February 21 2006 12:00 am | Download

/**
 * SwatchPanel controls the individual colour selection groups for
 * the hat/glasses/face colour in the colour selection panel
 *
 * @author  Loren Segal
 * @version 1.0
 */
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;
 
public class SwatchPanel extends JPanel implements ItemListener
{
	JPanel colorBox = new JPanel();
	JComboBox colorOptions = new JComboBox(ColorEnum.values());
	private String name;
	
	public SwatchPanel(String title, ColorEnum defaultColor) 
	{
		super();
		setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 1, true), title));
		colorBox.setBorder(new LineBorder(new Color(0,0,0), 2, true));
		name = title;
 
		// Load default settings        
		colorBox.setBackground(defaultColor.getValue());
		colorOptions.setSelectedIndex(defaultColor.ordinal());
	
		// Setup the listeners
		colorOptions.addItemListener(this);
	
		// add the components
		add(colorOptions);
		add(colorBox);
	}   
	
	public void itemStateChanged(ItemEvent e)  
	{
		JComboBox combo = (JComboBox)e.getSource();
		for (ColorEnum c : ColorEnum.values())
		{
			if (c.ordinal() == combo.getSelectedIndex()) 
			{
				colorBox.setBackground(c.getValue());
				
				// Update main options
				if (name.startsWith("Hat"))
				{
					DigitalPicasso.setHatColor(c);
				}
				else if (name.startsWith("Shades"))
				{
					DigitalPicasso.setShadesColor(c);
				}
				else if (name.startsWith("Face"))
				{
					DigitalPicasso.setFaceColor(c);
				}
			}
		}
	}   
}