重置JFrame

时间:2015-12-10 00:28:48

标签: java swing

这个游戏连接四个,在游戏结束时我给玩家一个选项 再玩一次。因此,如果他们想再玩一次,我会尝试清理画布并重置电路板。我基本上都不知道做java图形,所以我就是这样 很确定这个错误源于我不理解的东西 图形而不是游戏逻辑。 如果您在我收到用户输入后立即查看更新的gameState方法 是我试图重置一切。当我说重置时,我的意思是从窗口删除所有图像等。而不是一切被删除它只是锁定。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Color;

public class MainFrame extends JFrame
{
   public static final String GAME_NAME        = "Connect Four!";
   private static final int   _PLAYER_ONE_TURN = 1;

   private static String      _title;
   private Board              _gameBoard;
   private ToolBar            _buttons;
   private boolean            _humanVsHuman;
   private int                _gameIteration;


   public MainFrame()
   {
      super();

      final String INITIAL_GAME_MESSAGE = "Human Vs. Human? Default Computer "
            + "Vs. Human.";
      final String MENU_TITLE = "Game Setup...";
      final String[] MENU_OPTIONS = { "Yes!", "No!" };

      _gameBoard = new Board(Game.BOARD_WIDTH, Game.BOARD_HEIGHT);
      _buttons = new ToolBar(Game.BOARD_WIDTH);
      _humanVsHuman = 0 == JOptionPane.showOptionDialog(this,
            INITIAL_GAME_MESSAGE, MENU_TITLE, JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, MENU_OPTIONS, MENU_OPTIONS[1]);

      updateTitle();
      setLayout(new BorderLayout());
      setSize(Game.SCREEN_WIDTH, Game.SCREEN_HEIGHT);
      setResizable(false);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      add(_buttons, BorderLayout.SOUTH);
      setVisible(true);
   }


   public void paint(Graphics g)
   {
      GamePiece[][] board = _gameBoard.getBoard();
      _buttons.repaint();

      for (int x = 0; x < Game.BOARD_WIDTH; x++)
      {
         for (int y = 0; y < Game.BOARD_HEIGHT; y++)
         {
            if (board[x][y] != null)
            {

               g.setColor(
                     board[x][y].toString().equals(GamePiece.GAME_PIECE_BLUE)
                           ? Color.BLUE : Color.RED);
               g.fillOval((int) (x * (GamePiece.SIZE[0] * .92) + ((Game.SCREEN_WIDTH - (GamePiece.SIZE[0] * Game.BOARD_WIDTH)) / Game.BOARD_WIDTH) * x + 30), (int) (y * (GamePiece.SIZE[1] * .9) + ((Game.SCREEN_HEIGHT - (GamePiece.SIZE[1] * Game.BOARD_HEIGHT)) / Game.BOARD_HEIGHT) * y + 30),
                     GamePiece.SIZE[0], GamePiece.SIZE[1]);
            }
         }
      }
   }


   public void updateTitle()
   {
      final String HUMAN = "HUMAN";
      final String COMPUTER = "COMPUTER";
      final String PLAYER = "PLAYER";
      final String TURN = "TURN";

      _title = GAME_NAME + " " + Game.VERSION + ": " + PLAYER + " "
            + (getPlayerTurn(_gameIteration) == _PLAYER_ONE_TURN
                  ? _humanVsHuman ? "1" : HUMAN
                  : _humanVsHuman ? "2" : COMPUTER)
            + " " + TURN;

      setTitle(_title);
   }


   public void updateGameState(boolean aWinner)
   {
      final String AGAIN = "Play Again?";
      final String ROUND_OVER = "ROUND OVER";
      final String[] OPTIONS = { "Yes", "No" };
      boolean hasWinner = aWinner;

      if (hasWinner || _gameBoard.isFull())
      {

         if (hasWinner)
         {
            JOptionPane.showMessageDialog(this,
                  getPlayerTurn(_gameIteration) == _PLAYER_ONE_TURN
                        ? "Blue won!" : "Red won!");
         }

         if (0 != JOptionPane.showOptionDialog(this, AGAIN, ROUND_OVER,
               JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
               OPTIONS, OPTIONS[1]))
         {
            setVisible(false);
            dispose();
         }
         else
         {
            _gameBoard.clearBoard();
            removeAll();//or remove(JComponent)
            invalidate();
            _buttons = new ToolBar(Game.BOARD_WIDTH);
            updateMainFrame();            
         }
      }

      _gameIteration++;

   }


   public void updateMainFrame()
   {
      revalidate();
      repaint();
      updateTitle();

      if (_gameIteration + 1 == Integer.MAX_VALUE)
      {
         if (getPlayerTurn(_gameIteration) == _PLAYER_ONE_TURN)
         {
            _gameIteration = 0;
         }
         else
         {
            _gameIteration = 1;
         }
      }

   }


   private int getPlayerTurn(int iteration)
   {
      return (iteration % 2) + 1;
   }

   private class ToolBar extends JPanel
   {

      public ToolBar(int numberOfButtons)
      {

         setLayout(new FlowLayout(FlowLayout.CENTER,
               (int) (Game.SCREEN_WIDTH / numberOfButtons) / 2, 0));

         for (int i = 0; i < numberOfButtons; i++)
         {
            JButton button = new JButton("" + (i + 1));

            button.addActionListener(new ActionListener()
            {

               @Override
               public void actionPerformed(ActionEvent e)
               {
                  boolean hasWinner;

                  if (!_gameBoard
                        .isRowFull(Integer.parseInt(button.getText()) - 1))
                  {
                     hasWinner = _gameBoard.insert(
                           getPlayerTurn(_gameIteration) == _PLAYER_ONE_TURN
                           ? new BluePiece() : new RedPiece(),
                     Integer.parseInt(button.getText()) - 1);
                     updateMainFrame();
                     updateGameState(hasWinner);

                  }
               }

            });
            add(button);

         }
      }
   }
}

0 个答案:

没有答案