我正在用 mvc 设计中的 gui 编写一个国际象棋游戏。
第 1 步:弹出一个主菜单,然后您选择一个游戏模组。
第 2 步:一旦您选择,主菜单关闭并打开一个新窗口,其中包含棋盘和棋子,然后您可以使用鼠标进行游戏。
对于第 1 步;我使用 actionEvent 并检查您单击的按钮的字符串。例如,您有按钮标准游戏,然后模型设置数据板并通知观察者(=视图)。
对于第 2 步;我使用 mouseEvent 并检查相对坐标 x/y,模型执行它的工作并决定您是否可以移动该块。
我想查看两种更新方法,一种用于step 1
,另一种用于step 2
. 目前它总是进行第一次更新。
// this is in the model, initializing once you chose a game mod,
// this is for the first step.
public void initializeGame(String givenString){
abstractGame = factoryGame.createGame(givenString);
abstractGame.startPlaying(boardTest);
setChanged();
notifyObservers(5);
}
// this is in the model, doing stuff, this is for the second step.
public boolean checkGivenCoordinates(int sourceRow, int sourceColumn, int destinationRow, int destinationColumn) throws IncorrectCoordinatesException, IncorrectColorException, InvalidMoveException
{
if(abstractGame.checkCorrectCoordinates(sourceRow, sourceColumn, destinationRow, destinationColumn) == true)
{
abstractGame.checkMove(sourceRow, sourceColumn, destinationRow, destinationColumn);
int [] changeView = {sourceRow, sourceColumn, destinationRow, destinationColumn};
System.out.println("Model : in the move ");
setChanged();
notifyObservers(changeView);
return true;
}
else
throw new IncorrectCoordinatesException();
}
// Called from the Model
public void update(Observable obs, Object obj) { // this is the one it always goes to now.
//who called us and what did they send?
System.out.println ("First update View : Observable is " + obs.getClass() + ", object passed is " + obj.getClass());
} //update()
// Called from the Model
/* this is for step 2, but is not invoked.
The array I send holds 4 ints, source row/column and destination row/column.
this is what I do in model just prior to notifying,
supposed to go to step 2's update method,
but as stated, it doesnt.
*/
public void update(Observable obs, int[] obj) {
//who called us and what did they send?
System.out.println ("Second update View : Observable is " + obs.getClass() + ", object passed is " + obj.getClass());
graphBoardView.setMove(obj[0], obj[1], obj[2], obj[3]);
} //update()
所以,
1)我可以在一个班级中有多个更新吗?
2)如果是这样,我怎样才能直接通知正确的更新方法?
3)如果 q1 不可能,我该如何绕过这个?
4)可能是 Object obj 导致它总是转到第一个吗?
在此先感谢,爱丽儿。