ColorLabel.java
2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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){}
}