コード例 #1
0
ファイル: BoardFrame.java プロジェクト: jjchee/mancala
  /**
   * Returns a JPanel that represents the mancala board using strategy pattern to insert style.
   *
   * @param strat concrete strategy
   * @return JPanel containing both users' pits as controllers
   */
  public JPanel boardContextDoWork(Strategy strat) {
    this.s = strat;
    Color boardColor = s.getBoardColor();
    Color fontColor = s.getFontColor();
    Font font = s.getFont();
    JPanel panCenter = new JPanel();
    JPanel panLeft = new JPanel();
    JPanel panRight = new JPanel();

    panCenter.setLayout(new GridLayout(2, 6, 10, 10));
    // B6 to B1 Controllers
    for (int i = 12; i > 6; i--) {
      final Pits temp = new Pits(i);
      final int pit = i;
      final JLabel tempLabel = new JLabel(temp);
      tempLabel.addMouseListener(
          new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
              if (Model.player == 1) {
                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame, "Player A's turn!");
              } else if (model.data[pit] == 0) {
                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame, "Pit is Empty try another one.");
              } else {
                if (temp.pitShape.contains(e.getPoint())) {
                  model.move(pit); // mutator
                  undoBtn.setText("Undo : " + model.getUndoCounter());
                  model.display();
                }
              }
            }
          });
      JPanel tempPanel = new JPanel(new BorderLayout());

      JTextPane textPane = new JTextPane();
      textPane.setEditable(false);
      textPane.setBackground(boardColor);
      textPane.setForeground(fontColor);
      textPane.setFont(font);
      textPane.setText("B" + (i - 6));
      StyledDocument doc = textPane.getStyledDocument();
      SimpleAttributeSet center = new SimpleAttributeSet();
      StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
      doc.setParagraphAttributes(0, doc.getLength(), center, false);
      tempPanel.add(textPane, BorderLayout.NORTH);
      tempPanel.add(tempLabel, BorderLayout.SOUTH);
      panCenter.add(tempPanel, BorderLayout.SOUTH);

      tempPanel.setBackground(boardColor);
    }
    // A1 to A6 Controllers
    for (int i = 0; i < 6; i++) {
      final Pits newPits = new Pits(i);
      JLabel label = new JLabel(newPits);
      final int pit = i;
      label.addMouseListener(
          new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
              if (Model.player == 2) {
                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame, "Player B's turn!");
              } else if (model.data[pit] == 0) {
                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame, "Pit is Empty try another one.");
              } else {
                if (newPits.pitShape.contains(e.getPoint())) {
                  model.move(pit); // mutator
                  undoBtn.setText("Undo : " + model.getUndoCounter());
                  model.display();
                }
              }
            }
          });
      JPanel tempPanel = new JPanel(new BorderLayout());

      tempPanel.add(label, BorderLayout.NORTH);
      JTextPane textPane = new JTextPane();
      textPane.setBackground(boardColor);
      textPane.setForeground(fontColor);
      textPane.setFont(font);
      textPane.setEditable(false);
      textPane.setText("A" + (i + 1));
      StyledDocument doc = textPane.getStyledDocument();
      SimpleAttributeSet center = new SimpleAttributeSet();
      StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
      doc.setParagraphAttributes(0, doc.getLength(), center, false);
      tempPanel.add(textPane, BorderLayout.SOUTH);
      tempPanel.setBackground(boardColor);
      panCenter.add(tempPanel, BorderLayout.SOUTH);
    }

    // left text pane
    JTextPane paneLeft = new JTextPane();
    paneLeft.setBackground(boardColor);
    paneLeft.setForeground(fontColor);
    paneLeft.setFont(font);
    paneLeft.setEditable(false);
    paneLeft.setText("M\nA\nN\nC\nA\nL\nA\n \nB");

    // right text pane
    JTextPane paneRight = new JTextPane();
    paneRight.setBackground(boardColor);
    paneRight.setForeground(fontColor);
    paneRight.setFont(font);
    paneRight.setEditable(false);
    paneRight.setText("M\nA\nN\nC\nA\nL\nA\n \nA");

    // Add text panes to left and right panels
    panLeft.setLayout(new BorderLayout());
    panRight.setLayout(new BorderLayout());
    panLeft.add(paneLeft, BorderLayout.WEST);
    panRight.add(paneRight, BorderLayout.EAST);
    panLeft.add(new JLabel(new Pits(13)), BorderLayout.EAST);
    panRight.add(new JLabel(new Pits(6)), BorderLayout.WEST);

    // add the 2 mancala panels and pit panel to larger displayPanel
    JPanel displayPanel = new JPanel();
    displayPanel.add(panLeft, BorderLayout.WEST);
    displayPanel.add(panCenter, BorderLayout.CENTER);
    displayPanel.add(panRight, BorderLayout.EAST);

    // set color
    panCenter.setBackground(boardColor);
    panLeft.setBackground(boardColor);
    panRight.setBackground(boardColor);
    displayPanel.setBackground(boardColor);

    // return display panel which contains the containers and elements created
    return displayPanel;
  }