ColorLabel.java 2.37 KB
import java.awt.Color;
import java.awt.Label;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

/**
 * ColorLabel is an extension of a label with an id field and coordinates. It 
 * has a listener on it to set the Mouse Left and Right click with the corresponding
 * background color.
 **/
public class ColorLabel extends Label implements MouseListener
{ 
	private int x, y, idX, idY;
	private Color background;
	
	/** Creates a default ColorLabel. **/
	public ColorLabel(){}
	
	/** 
	 * Creates a ColorLabel which takes in a color, coordinates and a special id.
	 * 
	 * Example:
	 * new ColorLabel(Color.Blue,5,5,5,5);
	 **/
	public ColorLabel(Color background, int x, int y, int idX, int idY)
	{
		// sets a listener, sets x and y coordinates and the background and
		// sets the fields with the corresponding values
		addMouseListener(this);
		setBounds(x,y,15,15);
		setBackground(background);
		this.background = background;
		this.x = x;
		this.y = y;
		this.idX = idX;
		this.idY = idY;
	}
	
	/** 
	 * Overwrites the setBackground so it sets the background and stores it in 
	 * a field of its own.
	 **/
	public void setBackground(Color background)
	{
		// sets the background and sets the field to the value passed through
		// the parameter
		this.background = background;
		super.setBackground(background);
	}
	
	/**
	 * Invoked when the mouse button has been clicked (pressed and released) on a component.
	 * Sets the corresponding color to the Mouse Left and Mouse Right fields and 
	 * save the ID of the last color picked.
	 **/
	public void mouseClicked(MouseEvent e)
	{
		if(e.getButton() == MouseEvent.BUTTON1)
			ColorBox.foreGround.setBackground(background);
			
		if(e.getButton() == MouseEvent.BUTTON3)
			ColorBox.backGround.setBackground(background);
		
		// lets the program know which is the current swatch selected
		ColorBox.selectedSwatchX = idX;
		ColorBox.selectedSwatchY = idY;
	}
	
	/** Invoked when a mouse button has been released on a component.**/
	public void mouseReleased(MouseEvent e){}
	/** Invoked when a mouse button has been pressed on a component. **/
	public void mousePressed(MouseEvent e){}
	/** Invoked when the mouse enters a component.**/
	public void mouseEntered(MouseEvent e){}
	/** Invoked when the mouse exits a component. **/
	public void mouseExited(MouseEvent e){}
}