【问题标题】:How to represent a Board Panel in Java for a game?如何用 Java 表示游戏的棋盘面板?
【发布时间】:2012-08-17 23:32:37
【问题描述】:

我想为游戏修复一个 2D 棋盘。我已经为 Gui 修复了其他面板,一切顺利。但是板的面板不能打印在窗口上。我对此有点困惑,因为我认为我遵循了与我需要的其他面板相同的想法。

这是我所做的:

编辑: 我要做的是根据它的尺寸为游戏修复一个棋盘面板,将每个正方形保持在一个数组中,以便在需要的任何地方使用它。我用 draw 方法绘制了它的每个小方块,然后将其放回面板。因此,板上的每个方块都是一个面板。这就是想法。但正如你所看到的。上面有问题/错误。

编辑:代码已更新。 刚刚发现问题的一部分。我首先认为我已将背景设置为平方,但我没有。有了这个,它在面板上出现了一个宽的黑色“柱子”。不幸的是,仍然没有正方形。 :(

另一个编辑: 另外,我意识到 draw 方法永远不会被调用。当我将 draw 方法放在以下方法中时,我可以看到方块,但它们仍然很小。我用 setSize 重新定义了它们,但仍然没有改变。

如何使用paint方法正确编辑面板????像现在这样,它不能。即使它不能返回一个对象(例如面板),因为它是多态的 void!

/**
    *Method used to construct the square in the area of the
    *gui's grid. In this stage a GUISquare array is being constructed, 
    * used in the whole game as
    *a mean of changing a square graphical state.
    *@param squares is the squares array from whom the gui grid will be
    *constructed.
    *@see getSquare about the correspondance beetween a squareModel and
    * a GUISquare.
    */
    private void initBoardPanel(SquareModel[][] squares){

         BoardPanel.setLayout(new GridLayout(height ,width )); //set layout

         SquareRenderer[][] Squares;
         JPanel[][] grid;
         Squares=new GUISquare[height][width()];
         grid=new JPanel[height()][width()];

         for (int i=0; i<height(); i++){
                for (int j=0; j<width() ; j++){
                    SquareRenderer kou=new SquareRenderer(i,j);
                kou.setSquare(myGame.getSquares()[i][j]);
                      //NOTE: THE FOLLOWING DRAW METHOD CANT BE CALLED!!!?
                if (myGame.getSquares()[i][j] instanceof SimpleSq  ){
                        kou .paintPanel(i,j,"");}
                else if (myGame.getSquares()[i][j] instanceof ActionSq  )
                {       kou .paintPanel(i,j);
                }
                   //JUST BECAUSE DRAW CANT BE CALLED I PUT ITS CODE HERE: 
                   //JUST TO CHECK:
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                JLabel label1 = new JLabel("Move To "+myGame.getSquares()[i][j].getGoTo());
                JLabel label2 = new JLabel(""+myGame.getSquares()[i][j].getSquare());

                panel.setBackground(Color.ORANGE);
                panel.add(label2, BorderLayout.NORTH);
                panel.add(label1, BorderLayout.CENTER);
                panel.setSize(250,250);

                    /////////  <--until here ---paint method<---

                kou.add(panel);
                kou.setVisible(true);
                kou.setBackground(Color.BLACK);

                Squares[i][j]= kou;

                BoardPanel.add(kou);
                BoardPanel.setVisible(true);
                BoardPanel.setBackground(Color.WHITE); 
                }
        }
        this.add(BoardPanel,BorderLayout.WEST);
     //   this.pack(); //sets appropriate size for frame
        this.setVisible(true); //makes frame visible
}

由 Squarerenderer 实现:

/**
 * Transformer for Snake/Ladder
 * <br>This method is used to display a square on the screen.
 */
public void paintPanel(int i,int j) {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    JLabel label1 = new JLabel("Move To"+myGame.getSquares()[i][j].getGoTo());
    JLabel label2 = new JLabel(""+myGame.getSquares()[i][j].getSquare());
    JSeparator CellSeparator = new JSeparator(orientation);
    panel.add(CellSeparator);
    panel.setForeground(Color.ORANGE);
    panel.add(label2, BorderLayout.NORTH);
    panel.add(label1, BorderLayout.CENTER);
}

【问题讨论】:

  • 建议:不要检查正方形的类型,而是让每个类实现一个drawSquare 方法,该方法接受GUISquare。它使您的板子初始化方法更清晰,并更好地封装您的代码。
  • 如果你是对的,那就更好了。我只是把代码的“清理”留到后面。现在我只想以图形部分结束:/ thnx 提示 anw
  • 根据您的问题和代码,我不确定您当前的问题是什么。您可能希望向我们提供更多详细信息。此外,unholysampler 的建议不仅仅是“清理”您的代码。如果你的代码像他建议的那样简单,那么你和我们调试起来会更容易。
  • 我应该提供整个代码吗?这很难。你可以问我什么。无论如何,我会尝试发布更多详细信息...
  • 刚刚发现了部分问题。再次更新。

标签: java user-interface


【解决方案1】:

我发现了一些问题:

  1. 使用布局管理器时,应避免调用 setSize。大多数布局管理器会简单地覆盖它。您应该改用 setPreferredSize、setMinimumSize 和 setMaximumSize,它们会为布局管理器提供提示。
  2. 看起来 SquareRenderer 的 draw 方法从未将您创建的面板添加到任何东西。您应该在绘制的最后一行将面板添加到 SquareRenderer,或者(更好)将子组件直接添加到 SquareRenderer。

【讨论】:

  • 首先感谢您。我不知道。至于第二个,我注意到->查看更新。问题是 draw 方法从未被调用过。搜索原因。
  • 您不需要重写任何 awt 或 swing 油漆组件来执行您在此处尝试执行的操作(有关在 swing 中绘画的入门知识,请参阅 java.sun.com/products/jfc/tsc/articles/painting)。您只需要确保正确初始化组件(即确保将所有内容添加到组件层次结构中)并正确设置大小提示。上面的例子你好像还在用setSize,试试我上面提到的方法来指定组件大小
  • 你能举个例子吗?我不想使用图形()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多