Viewing file: comp249/assignment2/FacePanel.java | Back to directory listing
Author: Loren Segal | Last modified: February 20 2006 07:00 pm | Download

/**
 * FacePanel displays the hat/shades style selection options as well as
 * the face width/height for drawing.
 *
 * @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 FacePanel extends JPanel implements ItemListener, ChangeListener 
{
    private JRadioButton[] hatRadio     = new JRadioButton[HatEnum.values().length];
    private JRadioButton[] shadesRadio  = new JRadioButton[ShadesEnum.values().length];
    private ButtonGroup    hatStyles    = new ButtonGroup();
    private ButtonGroup    shadesStyles = new ButtonGroup();
	
    private JSlider sliderWidth  = new JSlider(50, 400, DigitalPicasso.getFaceWidth());
    private JSlider sliderHeight = new JSlider(50, 400, DigitalPicasso.getFaceHeight()); 
	
    public FacePanel() 
    {
	super(new FlowLayout(FlowLayout.CENTER, 10, 10));
 
	JPanel hatPanel = new JPanel(new GridLayout(3, 1));
	JPanel shadesPanel = new JPanel(new GridLayout(3, 1));
	JPanel centerPanel = new JPanel(new GridLayout(2, 1,0,15));
	JPanel topCenterPanel = new JPanel(new GridLayout(1, 2, 15, 0));
	JPanel topPanel = new JPanel(new BorderLayout());
	JPanel sizePanel = new JPanel(new GridLayout(2,1));
	JPanel widthPanel = new JPanel();
	JPanel heightPanel = new JPanel();
 
	// Hat Styles
	hatPanel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 1, true), "Hat"));
	for (HatEnum hat: HatEnum.values())
	{
		int i = hat.ordinal();
		hatRadio[i] = new JRadioButton(hat.getValue());
		hatPanel.add(hatRadio[i]);
		hatStyles.add(hatRadio[i]);
		hatRadio[i].addItemListener(this);
		if (hat == DigitalPicasso.getHatStyle()) hatRadio[i].setSelected(true);
	}
	
	// Shades Styles
	shadesPanel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 1, true), "Shades"));
	for (ShadesEnum shades: ShadesEnum.values())
	{
		int i = shades.ordinal();
		shadesRadio[i] = new JRadioButton(shades.getValue());
		shadesPanel.add(shadesRadio[i]);
		shadesStyles.add(shadesRadio[i]);
		shadesRadio[i].addItemListener(this);
		if (shades == DigitalPicasso.getShadesStyle()) shadesRadio[i].setSelected(true);
	}
	
	// Bottom- width/height sliders
	sliderWidth.setPaintTicks(true);
	sliderWidth.setPaintLabels(true);
	sliderWidth.setMajorTickSpacing(50);
	sliderWidth.setMinorTickSpacing(25);
	sliderWidth.addChangeListener(this);
	sliderHeight.setPaintTicks(true);
	sliderHeight.setPaintLabels(true);
	sliderHeight.setMajorTickSpacing(50);
	sliderHeight.setMinorTickSpacing(25);
	sliderHeight.addChangeListener(this);
 
	widthPanel.add(new JLabel("Width:"));
	widthPanel.add(sliderWidth);
	heightPanel.add(new JLabel("Height:"));
	heightPanel.add(sliderHeight);
	sizePanel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 1, true), "Face"));
	sizePanel.add(widthPanel);
	sizePanel.add(heightPanel);
	
	// Add all of the panels to the main panel
	topCenterPanel.add(hatPanel);
	topCenterPanel.add(shadesPanel);
	topPanel.add(topCenterPanel, BorderLayout.CENTER);
	centerPanel.add(topPanel);
	centerPanel.add(sizePanel);
 
	add(centerPanel);
    }
 
	public void itemStateChanged(ItemEvent e)  
	{
		for (HatEnum i: HatEnum.values())
		{
			if (hatRadio[i.ordinal()] == e.getSource())
			{
				// A set method is _really_ not required here.
				DigitalPicasso.setHatStyle(i);  
				return;
			}
		}
		for (ShadesEnum i : ShadesEnum.values())
		{
			if (shadesRadio[i.ordinal()] == e.getSource())
			{
				// A set method is _really_ not required here.
				DigitalPicasso.setShadesStyle(i);
				return;
			}
		}
	
    }
 
	public void stateChanged(ChangeEvent e)
	{
		JSlider slide = (JSlider)e.getSource();
		if (slide == sliderWidth)
		{
			DigitalPicasso.setFaceWidth(slide.getValue());
		}
		if (slide == sliderHeight)
		{
			DigitalPicasso.setFaceHeight(slide.getValue());
		}
		
	}
 
}