-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorItem.java
More file actions
59 lines (52 loc) · 1.68 KB
/
ColorItem.java
File metadata and controls
59 lines (52 loc) · 1.68 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
package mandeljava;
import java.awt.Color;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class ColorItem extends Panel implements ItemListener {
private final JRadioButton radio;
private final JPanel colorWindow;
private final int index;
private final ArrayList<ColorSelectedListener>listeners;
public static int preferredWidth = 40;
public static int preferredHeight = 20;
public ColorItem(int index, Color color){
this.index = index;
this.listeners = new ArrayList<ColorSelectedListener>();
this.setLayout(null);
this.radio = new JRadioButton();
this.radio.setBackground(Color.WHITE);
this.colorWindow = new JPanel();
this.colorWindow.setLayout(null);
this.colorWindow.setBackground(color);
this.colorWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.radio.setBounds(0, 0, 20, 20);
this.colorWindow.setBounds(20, 0, 20, 20);
this.add(this.radio);
this.add(this.colorWindow);
this.radio.addItemListener(this);
}
public JRadioButton getRadio(){
return this.radio;
}
public void addColorSelectedListener(ColorSelectedListener l){
listeners.add(l);
}
public void removeColorSelectedListener(ColorSelectedListener l){
if(listeners.contains(l)){
listeners.remove(l);
}
}
@Override
public void itemStateChanged(ItemEvent e) {
for (int i = 0; i < listeners.size(); i++) {
if(this.radio.isSelected()){
listeners.get(i).colorSelected(new ColorSelectedEvent(this.index));
}
}
}
}