【发布时间】: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