summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/Board.java
blob: 2d51cb2bb83a294ff6865c1ed71903c37126a3ae (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
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.image.BufferedImage;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import javax.swing.ImageIcon;

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

@SuppressWarnings("serial")
public class Board extends StonePanel implements IBoard {
  private final static BufferedImage BACKGROUND;
  static {
    ImageIcon image = new ImageIcon(
        Board.class.getResource("/jrummikub/resource/wood.png"));
    BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
        BufferedImage.TYPE_INT_RGB);
    
    image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
  }

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

  Board() {
    setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1));
  }
  
  private BufferedImage getScaledBackground(int size) {
    BufferedImage scaled = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = scaled.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    
    g.drawImage(BACKGROUND, 0, 0, size, size, null);
    
    return scaled;
  }
  
  @Override
  protected 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);
    int size = height/2;
    BufferedImage scaledBackground = getScaledBackground(size);
    
    for(int xpos = 0; xpos < width; xpos += size) {
      g.drawImage(scaledBackground, xpos, 0, null);
    }
    for(int xpos = -size/3; xpos < width; xpos += size) {
      g.drawImage(scaledBackground, xpos, size, null);
    }
    
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    
    getStonePainter().setScale(size*StonePainter.PIXEL_SCALE);
    
    for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
      getStonePainter().paintStone(g, entry.getKey(), entry.getValue(),
          selectedStones.contains(entry.getKey()));
    }
  }

  @Override
  public void setStones(Map<Stone, Position> stones) {
    this.stones = stones;
    repaint();
  }
  
  public void setSelectedStones(Collection<Stone> stones) {
    selectedStones = stones;
    repaint();
  }
}