为什么创建连接四时我的主板不出现?

时间:2014-01-19 22:26:49

标签: java

我正在尝试制作连接四的GUI Java代码,但我的游戏棋盘圈都没有出现。我把它们画到了黑板上然而它们只是没有出现。我不明白我做错了什么。请帮忙看看修改后的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
//how do I get the board to actually appear?


public class JenniferConnectFourJan19 {

  static int [][] boardArray = new int [6][7];
  static String [][] spotOnBoard = new String [6][7];
  static int maxRow=6;
  static int maxCol=7;
  static boolean endOfGame = false;
  static boolean gameStart;

  static JPanel boardPanel;

  static JButton firstOption = new JButton ("Drop");
  static JButton secondOption = new JButton ("Drop");
  static JButton thirdOption = new JButton ("Drop");
  static JButton fourthOption = new JButton ("Drop");
  static JButton fifthOption = new JButton ("Drop");
  static JButton sixthOption = new JButton ("Drop");
  static JButton seventhOption = new JButton ("Drop");

  static JButton playAgain = new JButton ("New");
  static JButton reset = new JButton ("Reset");
  static JButton exit = new JButton ("Exit");

  static int blank =0;
  static int red=1;
  static int yellow=2;
  static int firstColour= red;

  public static void board() {
    boardArray = new int [maxRow][maxCol];
    for (int row=0; row < maxRow; row++){
      for (int col=0; col< maxCol; col++){
        boardArray[row][col]= blank;
      }
    }
  }


  public static class drawBoard extends JPanel {
    public void paintComponent(Graphics g){
      super.paintComponent(g);
 //     drawBoard(g, drawBoard); 
  }//end of paintComponent.

  void drawConnectFourBoard(Graphics g){    

    g.setColor(Color.BLUE);
    g.fillRect(110, 50, 100+100*maxCol, 100+100*maxRow);
    for (int row=0; row<maxRow; row++)
      for (int col=0; col<maxCol; col++) {
      if (boardArray[row][col]==blank) g.setColor(Color.white);
      if (boardArray[row][col]==red) g.setColor(Color.red);
      if (boardArray[row][col]==yellow) g.setColor(Color.yellow);
      g.fillOval(160+100*col, 100+100*row, 100, 100);
    }
  }
  public Dimension getPreferredSize() {
    return new Dimension(300,300);
  }
  }

  public static boolean winCheck(String box) { 
    for(int j = 0; j < 6; j++) { 
      for (int k = 0; k < 7; k++) { 
        if(spotOnBoard[j][k].equals(box)) {           
          if(((j -3) >= 0) && spotOnBoard[j -1][k].equals(box) && spotOnBoard[j -2][k].equals(box) && spotOnBoard[j -3][k].equals(box)) {  
            return true;
          }          
          else if(((k -3) >= 0) && spotOnBoard[j][k -1].equals(box) && spotOnBoard[j][k -2].equals(box) && spotOnBoard[j][k -3].equals(box)) {
            return true; 
          }          
          else if(((j+3)<= 5) && spotOnBoard[j+1][k].equals(box) && spotOnBoard[j+2][k].equals(box) && spotOnBoard[j+3][k].equals(box)){ 
            return true;
          }          
          else if(((k +3) <= 6) && spotOnBoard[j][k +1].equals(box) && spotOnBoard[j][k +2].equals(box) && spotOnBoard[j][k +3].equals(box)) {          
            return true; 
          }          
          else if(((j -3)>= 0) && ((k +3)<=6) && spotOnBoard[j-1][k+1].equals(box) && spotOnBoard[j-2][k+2].equals(box) && spotOnBoard[j-3][k+3].equals(box)){ 
            return true; 
          }          
          else if(((j +3) <=5) && ((k -3) >=0) && spotOnBoard[j +1][k -1].equals(box) && spotOnBoard[j +2][k -2].equals(box) && spotOnBoard[j +3][k -3].equals(box)){ 
            return true;
          }          
          else if(((j -3)>=0) && ((k -3)>= 0) && spotOnBoard[j -1][k -1].equals(box) && spotOnBoard[j -2][k -2].equals(box) && spotOnBoard[j -3][k -3].equals(box)){ 
            return true;
          }          
          else if(((j +3) <=5) && ((k +3) <=6) && spotOnBoard[j +1][k +1].equals(box) && spotOnBoard[j +2][k +2].equals(box) && spotOnBoard[j +3][k +3].equals(box)){
            return true; 
          }
          else 
            return false; 
        }
      } 
    }
    return false; 
  }

  public static void displayWinner(Graphics g, int n) {
    g.setColor(Color.BLACK);
    g.setFont(new Font("Courier", Font.BOLD, 100));
    if (n==red)
      g.drawString("Red wins!", 100, 400);
    else
      g.drawString("Yellow wins!", 100, 400);
    endOfGame=true;
  }

  public static void main (String [] args) {
    drawBoard drawConnectFourBoard = new drawBoard();
    board();

    ButtonHandler listen = new ButtonHandler();

    firstOption.addActionListener(listen);
    secondOption.addActionListener(listen);
    thirdOption.addActionListener(listen);
    fourthOption.addActionListener(listen);
    fifthOption.addActionListener(listen);
    sixthOption.addActionListener(listen);
    seventhOption.addActionListener(listen);
    playAgain.addActionListener(listen);
    reset.addActionListener(listen);
    exit.addActionListener(listen);

   // JPanel boardPanel = new JPanel();


    JPanel topPanel = new JPanel();
    topPanel.setLayout(new GridLayout(1,7));   
    topPanel.setBackground(new Color(0,0,0));
    topPanel.add(firstOption);
    topPanel.add(secondOption);
    topPanel.add(thirdOption);
    topPanel.add(fourthOption);
    topPanel.add(fifthOption);
    topPanel.add(sixthOption);
    topPanel.add(seventhOption);


    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new GridLayout(1,4));
    bottomPanel.add(playAgain);
    bottomPanel.add(reset);
    bottomPanel.add(exit);


    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.setBackground(new Color(0,214,154));
    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(drawConnectFourBoard);
    board();
    mainPanel.add(bottomPanel, BorderLayout.SOUTH);

    JFrame window = new JFrame ("Connect4");
    window.setContentPane(mainPanel);
    window.setSize(500,500);
    window.setLocation(500,100);
    window.setResizable(true);
    window.setVisible(true);
  }

   private static class ButtonHandler implements ActionListener { 
    public void actionPerformed(ActionEvent a) {
      if (a.getSource()== firstOption)
        dropCircle(1);
      else if (a.getSource()== secondOption)
        dropCircle(2);
      else if (a.getSource()== thirdOption)
        dropCircle(3);
      else if (a.getSource()== fourthOption)
        dropCircle(4);
      else if (a.getSource()== fifthOption)
        dropCircle(5);
      else if (a.getSource()== sixthOption)
        dropCircle(6);
      else if (a.getSource()==seventhOption)
        dropCircle(7);

    if (a.getSource() == playAgain) {
      gameStart=true;
    }
    if (a.getSource() == exit) {
      System.exit(0);
    }
    }   
    public void dropCircle(int n) {
      if (endOfGame) return;
      gameStart=true;
      int row;
      n--;
      for (row=0; row<maxRow; row++)
        if (boardArray[row][n]>0) break;
      if (row>0) {
        boardArray[--row][n]=firstColour;
        if (firstColour==red)
          firstColour=yellow;
        else
          firstColour=red;

      }
    }

   }
}

1 个答案:

答案 0 :(得分:2)

你有一系列问题,这些问题不容易解决。

首先,static的过度使用对您不利,事实上,这是设计糟糕的表现。

您可以通过创建自定义组件来解决此问题,从JPanel扩展并将其作为应用程序其余部分的基础,根据需要创建更多自定义组件,或者只使用简单容器,如JPanel,用于生成更复杂的UI

其次,它“似乎”就像你有资源问题一样。一般来说,您有两种类型的应用程序可以使用的资源,外部和内部(AKA嵌入式)。

ImageIcon(String)正在寻找磁盘上的外部资源(即File)。基于您的神秘评论,我认为图像实际上嵌入在您的应用程序的上下文中,使ImageIcon(String)无法找到任何内容。

相反,您应该尝试使用ImageIcon(URL),例如new ImageIcon(getClass().getResource("/path/to/image/image.png")

ImageIcon的问题是,当找不到或加载图片时,它不会抛出Exception,有时只是不绘画。

相反,您应该使用ImageIO.read。请查看Reading/Loading images了解更多详情

其他

  • paintComponent中没有任何内容可以实际绘制任何内容
  • 您的ActionListener无法通知任何人您的游戏模式已更新

分离游戏逻辑和UI逻辑,使得“模型”完全负责维护游戏的虚拟状态并通知感兴趣的各方模型已经改变并且UI部分简单地负责渲染视图模型。

然后允许控件更改模型的状态。