summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/TablePanel.java
blob: 3eb33d8aec2f16053bf20913b27b7d8ff63ecee3 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package jrummikub.view.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
import jrummikub.view.IStoneCollectionPanel;
import jrummikub.view.ITablePanel;

/**
 * The implementation of the table
 */
@SuppressWarnings("serial")
class TablePanel extends AbstractStonePanel implements ITablePanel {
  private final static ImageIcon background = new ImageIcon(
      HandPanel.class.getResource("/jrummikub/resource/felt.png"));

  private final static float DEFAULT_SCALE = 1;
  private final int COLLECTION_GAP = 5;

  private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel;
  private StoneCollectionPanel stoneCollection;

  private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
  private Collection<Stone> selectedStones = Collections.emptyList();

  @Override
  public void setLeftPlayerName(String playerName) {
    leftPlayerLabel.setText(playerName);
  }

  @Override
  public void setTopPlayerName(String playerName) {
    topPlayerLabel.setText(playerName);
  }

  @Override
  public void setRightPlayerName(String playerName) {
    rightPlayerLabel.setText(playerName);
  }

  @Override
  public void setStoneSets(Map<StoneSet, Position> stoneSets) {
    Map<Stone, Position> stones = new HashMap<Stone, Position>();
    
    for (Map.Entry<StoneSet, Position> entry : stoneSets.entrySet()) {
      float x = entry.getValue().getX(), y = entry.getValue().getY();
      
      for (Stone stone : entry.getKey()) {
        stones.put(stone, new Position(x, y));
        x += stone.getWidth();
      }
    }
    
    setStones(stones);
    this.stoneSets = stoneSets;
    
    repaint();
  }

  @Override
  public IStoneCollectionPanel getStoneCollectionPanel() {
    return stoneCollection;
  }

  /**
   * Sets the currently selected stones
   * 
   * @param stones
   *          the selected stones
   */
  void setSelectedStones(Collection<Stone> stones) {
    selectedStones = stones;
    stoneCollection.setSelectedStones(stones);
    repaint();
  }

  private void rescale() {
    Insets insets = getInsets();
    int x = insets.left, y = insets.top, width = getWidth() - insets.left
        - insets.right, height = getHeight() - insets.top - insets.bottom;

    leftPlayerLabel.setBounds(x, y, width, height);
    topPlayerLabel.setBounds(x, y, width, height);
    rightPlayerLabel.setBounds(x, y, width, height);

    stoneCollection.setLocation(x + width / 2 - stoneCollection.getWidth() / 2,
        y + height - stoneCollection.getHeight() - COLLECTION_GAP);
  }

  /**
   * Creates a new Table instance
   */
  TablePanel() {
    super(DEFAULT_SCALE);

    setLayout(null);

    leftPlayerLabel = new JLabel();
    leftPlayerLabel.setForeground(Color.WHITE);
    leftPlayerLabel.setHorizontalAlignment(JLabel.LEFT);
    leftPlayerLabel.setHorizontalTextPosition(JLabel.LEFT);
    add(leftPlayerLabel);

    topPlayerLabel = new JLabel();
    topPlayerLabel.setHorizontalAlignment(JLabel.CENTER);
    topPlayerLabel.setHorizontalTextPosition(JLabel.CENTER);
    topPlayerLabel.setVerticalAlignment(JLabel.TOP);
    topPlayerLabel.setVerticalTextPosition(JLabel.TOP);
    topPlayerLabel.setForeground(Color.WHITE);
    add(topPlayerLabel);

    rightPlayerLabel = new JLabel();
    rightPlayerLabel.setForeground(Color.WHITE);
    rightPlayerLabel.setHorizontalAlignment(JLabel.RIGHT);
    rightPlayerLabel.setHorizontalTextPosition(JLabel.RIGHT);
    add(rightPlayerLabel);

    stoneCollection = new StoneCollectionPanel();
    add(stoneCollection);

    ComponentListener rescaleListener = new ComponentAdapter() {
      @Override
      public void componentResized(ComponentEvent e) {
        rescale();
      }
    };

    addComponentListener(rescaleListener);
    stoneCollection.addComponentListener(rescaleListener);
  }

  private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) {
    float x = pos.getX();
    int width = getStonePainter().getStoneWidth(), height = getStonePainter()
        .getStoneHeight();

    g.setColor(new Color(0, 0, 0, 0.25f));
    g.fillRect((int) (x * width) - width / 4, (int) (pos.getY() * height),
        width / 4, height);

    for (Stone stone : stoneSet) {
      getStonePainter().paintStone(g, stone, new Position(x, pos.getY()),
          selectedStones.contains(stone));
      x++;
    }

    g.setColor(new Color(0, 0, 0, 0.25f));
    g.fillRect((int) (x * width), (int) (pos.getY() * height), width / 4,
        height);
  }

  @Override
  protected void paintComponent(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;

    for (int x = 0; x < getWidth(); x += background.getIconWidth()) {
      for (int y = 0; y < getHeight(); y += background.getIconHeight()) {
        background.paintIcon(this, g, x, y);
      }
    }

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

    for (Map.Entry<StoneSet, Position> stoneSet : stoneSets.entrySet()) {
      paintStoneSet(g, stoneSet.getKey(), stoneSet.getValue());
    }
  }
}