summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/Table.java
blob: 4a5589b96105d1d4d941edc581ef2be76edb9114 (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
package jrummikub.view.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

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

import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
import jrummikub.view.ITable;

@SuppressWarnings("serial")
public class Table extends StonePanel implements ITable {
  private final static ImageIcon background = new ImageIcon(
      Board.class.getResource("/jrummikub/resource/felt.png"));
  private final static float DEFAULT_SCALE = 1;
  
  private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel;
  private JPanel innerPanel;
  
  private StonePainter selectedStonePainter = new StonePainter(1.2f);

  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) {
    this.stoneSets = stoneSets;
    repaint();
  }
  
  public void setSelectedStones(Collection<Stone> stones) {
    selectedStones = stones;
    repaint();
  }

  Table() {
    super(DEFAULT_SCALE);
    
    setLayout(new BorderLayout());

    leftPlayerLabel = new JLabel();
    leftPlayerLabel.setForeground(Color.WHITE);
    add(leftPlayerLabel, BorderLayout.WEST);

    topPlayerLabel = new JLabel();
    topPlayerLabel.setHorizontalAlignment(JLabel.CENTER);
    topPlayerLabel.setHorizontalTextPosition(JLabel.CENTER);
    topPlayerLabel.setForeground(Color.WHITE);
    add(topPlayerLabel, BorderLayout.NORTH);

    rightPlayerLabel = new JLabel();
    rightPlayerLabel.setForeground(Color.WHITE);
    add(rightPlayerLabel, BorderLayout.EAST);

    innerPanel = new JPanel();
    innerPanel.setOpaque(false);
    add(innerPanel, BorderLayout.CENTER);
  }

  public void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) {
    float x = pos.getX();

    for (Stone stone : stoneSet) {
      getStonePainter().paintStone(g, stone, new Position(x, pos.getY()),
          selectedStones.contains(stone));
      x++;
    }
  }
  
  @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());
    }
    
    int selectedStonesWidth = getWidth()*3/5-14;
    int selectedStonesHeight = selectedStonePainter.getStoneHeight();
    int selectedStonesX = getWidth() / 2 - selectedStonesWidth / 2;
    int selectedStonesY = getHeight() - selectedStonesHeight - 12;

    if (!selectedStones.isEmpty()) {
      g.setColor(new Color(0, 0, 0, 0.3f));
      g.fillRect(selectedStonesX-7, selectedStonesY-7, selectedStonesWidth + 14,
          selectedStonesHeight + 14);

      Graphics2D translatedG = (Graphics2D) g.create(selectedStonesX,
          selectedStonesY, selectedStonesWidth, selectedStonesHeight);
      
      float x = 0;
      
      for (Stone stone : selectedStones) {
        selectedStonePainter.paintStone(translatedG, stone, new Position(x, 0), false);
        x++;
      }
    }
  }
}