import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) {
        int rowCount = Config.getInstance().getGroundRowCount();
        int columnCount = Config.getInstance().getGroundColumnCount();
        int w = Config.getInstance().getCellWidth();
        int h = Config.getInstance().getCellHeight();

        // 把游戏面板的大小设置为Ground的大小.
        GamePanel gamePanel = new GamePanel();
        gamePanel.setPreferredSize(new Dimension(w * columnCount + 1, h * rowCount + 1));
        gamePanel.setBorder(null);
        
        Ground ground = new Ground(rowCount, columnCount);
        Controller controller = new Controller(gamePanel, ground);

        JFrame frame = new JFrame("Tetris");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(gamePanel);
        frame.pack();
        
        // 定位Frame，水平在屏幕中间，垂直为中上
        int x = (Toolkit.getDefaultToolkit().getScreenSize().width - frame.getSize().width) / 2;
        int y = (Toolkit.getDefaultToolkit().getScreenSize().height - frame.getSize().height) / 2 - 30;
        x = x > 0 ? x : 0;
        y = y > 0 ? y : 0;
        frame.setLocation(x, y);
        frame.setResizable(false);
        frame.setVisible(true);

        // 给Frame加上键盘事件
        frame.addKeyListener(controller);
        frame.addFocusListener(controller);
        controller.startGame();
    }
}
