Example #1
0
  public GamePanel() {
    setBackground(new Color(0, 0, 0));
    setFocusable(true);
    //        setIgnoreRepaint(true);
    addKeyListener(KeyManager.getDefault());
    // 因为游戏图画根据窗体大小变化
    // 当PANEL大小变化时需要重画.否则由小变大不会调用paint
    addComponentListener(
        new ComponentAdapter() {
          public void componentResized(ComponentEvent e) {
            if (m_game != null) m_game.refreshScreen();
          }
        });

    // 当失去焦点,要清空键盘,否则可能导致只有按键无抬键
    addFocusListener(
        new FocusAdapter() {
          public void focusLost(FocusEvent e) {
            //                System.out.println("lost Focus");
            KeyManager.clearKeyState();
            m_prompt = "PAUSED";
            repaint();
            if (m_game != null) m_game.pause();
          }

          public void focusGained(FocusEvent e) {
            //                System.out.println("lost Focus");
            if (m_game != null) m_game.resume();
            m_prompt = "LOADING...";
          }
        });
  }