- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
- 78
- 79
- 80
- 81
- 82
- 83
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JLayeredPane;
public class ContainerOperations extends JLayeredPane {
private static final long serialVersionUID = 1L;
private CardPositionInContainer cardPosition;
private List<Card> rem = new ArrayList<Card>();
public List<Card> getRem() {
return rem;
}
public ContainerOperations() {
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public void addFromListInToContainer(List<Card> list) {
if (getComponentCount() == 0)
addInToEmpty(list);
else
appendIfContains(list);
}
private void appendIfContains(List<Card> list) {
int fromIndex = highestLayer() + 1;
int toIndex = highestLayer() + 1 + list.size();
appendFromLayerToLayer(list, fromIndex, toIndex);
}
private void addInToEmpty(List<Card> list) {
int fromIndex = 0;
int toIndex = list.size();
cardPosition.setCardPosition(0);
appendFromLayerToLayer(list, fromIndex, toIndex);
}
private void appendFromLayerToLayer(List<Card> list, int fromIndex,
int toIndex) {
int listIndex = 0;
for (int layer = fromIndex; layer < toIndex; layer++) {
Card card = list.get(listIndex++);
cardPosition.setCardPosition(layer);
card.setLocation(cardPosition.getCardPosition());
add(card, new Integer(layer));
}
}
@Override
protected void processMouseEvent(MouseEvent e) {
rem.clear();
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
Component comp = getComponentAt(e.getPoint());
addInToRemListIfMousePressed(comp);
}
}
private void addInToRemListIfMousePressed(Component comp){
if (comp instanceof Card) {
Component mark = (Card) comp;
int markedLayer = getMarkedLayer(mark);
addInToRemList(markedLayer);
}
}
private Integer getMarkedLayer(Component marked) {
return getComponentCount() - getComponentZOrder(marked) - 1;
}
private void addInToRemList(int layerOfmark) {
for (int i = layerOfmark; i < highestLayer() + 1; i++) {
Component[] card = getComponentsInLayer(i);
rem.add((Card) card[0]);
}
}
}
Комментарии (0) RSS
Добавить комментарий