summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/StoneCollectionPanel.java
blob: 872917019127762be91e799b2c3f9dd7952da522 (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
package jrummikub.view.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.swing.border.EmptyBorder;

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

/**
 * Implementation of the stone collection (selection)
 */
@SuppressWarnings("serial")
class StoneCollectionPanel extends AbstractStonePanel implements
    IStoneCollectionPanel {
  private final static int INSET = 7;
  private final static float STONE_SCALE = 1.1f;

  private Collection<Stone> selectedStones = Collections.emptyList();

  /**
   * Creates a new StoneCollection instance
   */
  StoneCollectionPanel() {
    super(STONE_SCALE);

    setOpaque(false);
    setVisible(false);
    setBorder(new EmptyBorder(INSET, INSET, INSET, INSET));
  }

  /**
   * Sets the stones to be shown in the collection
   * 
   * @param selectedStones
   *          the selected stones
   */
  void setSelectedStones(Collection<Stone> selectedStones) {
    this.selectedStones = selectedStones;

    Map<Stone, Position> stones = new HashMap<Stone, Position>();
    float x = 0;

    for (Stone stone : selectedStones) {
      stones.put(stone, new Position(x, 0));
      x += stone.getWidth();
    }

    setStones(stones);

    if (selectedStones.isEmpty()) {
      setVisible(false);
    } else {
      setSize(getStonePainter().getStoneWidth() * selectedStones.size() + 2
          * INSET, getStonePainter().getStoneHeight() + 2 * INSET);
      setVisible(true);

      repaint();
    }
  }

  @Override
  public void paintComponent(Graphics g1) {
    Insets insets = getInsets();
    int x = insets.left, y = insets.top, width = getWidth() - insets.left
        - insets.right, height = getHeight() - insets.top - insets.bottom;
    Graphics2D g = (Graphics2D) g1.create(x, y, width, height);

    if (!selectedStones.isEmpty()) {
      g1.setColor(new Color(0, 0, 0, 0.25f));
      g1.fillRoundRect(0, 0, getWidth(), getHeight(), INSET, INSET);

      float xpos = 0;

      for (Stone stone : selectedStones) {
        getStonePainter().paintStone(g, stone, new Position(xpos, 0), false);
        xpos++;
      }
    }
  }
}