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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
package jrummikub.view.impl;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.border.EmptyBorder;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.util.Pair;
import jrummikub.view.IStoneCollectionPanel;
/**
* Implementation of the stone collection (selection)
*/
@SuppressWarnings("serial")
class StoneCollectionPanel extends AbstractStonePanel implements
IStoneCollectionPanel {
private final static ImageIcon BACKGROUND = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/felt.png"));
private final static ImageIcon DARK_BACKGROUND = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/dark_felt.png"));
/**
* The width of the border of the collection panel
*/
public final static int INSET = 7;
private Collection<Stone> selectedStones = Collections.emptyList();
/**
* Creates a new StoneCollection instance
*/
StoneCollectionPanel() {
setBorder(new EmptyBorder(INSET, INSET, INSET, INSET));
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rescale();
}
});
}
private void rescale() {
Insets insets = getInsets();
int height = getHeight() - insets.top - insets.bottom;
getStonePainter().setScale(height * StonePainter.HEIGHT_SCALE);
repaint();
}
/**
* Sets the stones to be shown in the collection
*
* @param selectedStones
* the selected stones
*/
void setSelectedStones(Collection<Stone> selectedStones) {
this.selectedStones = selectedStones;
List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>();
float x = 0;
for (Stone stone : selectedStones) {
stones.add(new Pair<Stone, Position>(stone, new Position(x, 0)));
x++;
}
setStones(stones);
repaint();
}
@Override
protected Pair<Integer, Integer> getTranslation() {
int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2
* INSET;
int x = (getWidth() - width) / 2;
return new Pair<Integer, Integer>(x + INSET, INSET);
}
@Override
public void paintComponent(Graphics g1) {
for (int xpos = 0; xpos < getWidth(); xpos += BACKGROUND.getIconWidth()) {
for (int ypos = 0; ypos < getHeight(); ypos += BACKGROUND.getIconHeight()) {
BACKGROUND.paintIcon(this, g1, xpos, ypos);
}
}
int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2
* INSET, height = getHeight();
int x = (getWidth() - width) / 2;
Graphics2D g = (Graphics2D) g1.create(x, 0, width, height);
if (!selectedStones.isEmpty()) {
for (int xpos = 0; xpos < width; xpos += DARK_BACKGROUND.getIconWidth()) {
for (int ypos = 0; ypos < height; ypos += DARK_BACKGROUND
.getIconHeight()) {
DARK_BACKGROUND.paintIcon(this, g, xpos, ypos);
}
}
g.translate(INSET, INSET);
float xpos = 0;
for (Stone stone : selectedStones) {
getStonePainter().paintStone(g, stone, new Position(xpos, 0), false);
xpos++;
}
}
}
}
|