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

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

import javax.swing.JFrame;

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

@SuppressWarnings("serial")
public class View extends JFrame implements IView {
  private Table table;
  private PlayerPanel playerPanel;
  
  private final static float PLAYER_PANEL_RATIO = 0.125f;
  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 ITable getTable() {
    return table;
  }

  public IPlayerPanel getPlayerPanel() {
    return playerPanel;
  }

  
  public View() {
    super("JRummikub");
    setLayout(null);
    
    setSize(1000, 700);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    table = new Table();
    add(table);

    playerPanel = new PlayerPanel();
    playerPanel.setBorder(new CustomBorder(Color.BLACK, PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0));
    add(playerPanel);
    
    addComponentListener(new ComponentAdapter() {
      @Override
      public void componentResized(ComponentEvent e) {
        Insets insets = getInsets();
        int x = insets.left, y = insets.top, 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(x, y, width, tableHeight);
        table.validate();
        playerPanel.setBounds(x, y+tableHeight, width, playerPanelHeight);
      }
    });
    
    setVisible(true);
  }

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