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

import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Collection;

import javax.swing.JFrame;
import javax.swing.border.MatteBorder;

import jrummikub.model.Stone;
import jrummikub.view.IPlayerPanel;
import jrummikub.view.ITablePanel;
import jrummikub.view.IView;

/**
 * Implementation of the top-level view interface
 */
@SuppressWarnings("serial")
public class View extends JFrame implements IView {
  private TablePanel table;
  private PlayerPanel playerPanel;

  private final static float PLAYER_PANEL_RATIO = 0.14f;
  private final static int PLAYER_PANEL_BORDER_WIDTH = 1;
  private final static int PLAYER_PANEL_MAX_HEIGHT = 180 + PLAYER_PANEL_BORDER_WIDTH;

  private static int even(double d) {
    return 2 * (int) (d / 2);
  }

  public ITablePanel getTablePanel() {
    return table;
  }

  public IPlayerPanel getPlayerPanel() {
    return playerPanel;
  }

  /**
   * Create a new instance of the view
   */
  public View() {
    super("JRummikub");
    setLayout(null);

    setSize(800, 600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    table = new TablePanel();
    add(table);

    playerPanel = new PlayerPanel();
    playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0,
        Color.BLACK));
    add(playerPanel);

    addComponentListener(new ComponentAdapter() {
      @Override
      public void componentResized(ComponentEvent e) {
        Insets insets = getInsets();
        int width = getWidth() - insets.left
            - insets.right, height = getHeight() - insets.top - insets.bottom;

        int playerPanelHeight = even(Math.pow((double) width * width * height,
            1 / 3.0) * PLAYER_PANEL_RATIO)
            + PLAYER_PANEL_BORDER_WIDTH;
        if (playerPanelHeight > PLAYER_PANEL_MAX_HEIGHT)
          playerPanelHeight = PLAYER_PANEL_MAX_HEIGHT;

        int tableHeight = height - playerPanelHeight;

        table.setBounds(0, 0, width, tableHeight);
        table.validate();
        playerPanel.setBounds(0, tableHeight, width, playerPanelHeight);
      }
    });

    setVisible(true);
  }

  @Override
  public void setSelectedStones(Collection<Stone> stones) {
    table.setSelectedStones(stones);
    playerPanel.getHandPanel().setSelectedStones(stones);
  }
}