2014-03-24 59 views
0

我正在製作一個基於局域網命令的遊戲,我目前正在將結構化的英語解析爲java。我解析了一切,我可以繪製到一個網格,但我只能在運行時硬編碼文本到網格,並且似乎無法使用新文本來刷新網格。JPanel刷新問題與遊戲

解析類:

public class ServerPlayerParsing {  
ServerGridGenerator serverGrid = new ServerGridGenerator (10, 10); 
public String validate(String command){ 
serverGrid.frameGen(); 
if (wordCount(command)== 3) { 
    String[] commandArray = command.split(" "); 
    commandParsing(commandArray); 
} else { 
    System.out.println("Error! Format incorrect!"); 
    System.out.println("Correct format = [COMMAND 1] [COMMAND 2] [COMMAND 3]"); 
} 
return ""; 
} 
public int wordCount(String command){ 
String[] commandCount = command.split("\\s"); 
return commandCount.length; 
} 
public String commandParsing(String[] commandArray) { 
switch (commandArray[0]) { 
case "move": 
secondCommand (commandArray); 
break; 
default: System.out.println("Error in first command!"); 
}   
return " "; 
} 
public String secondCommand (String commandArray[]) { 
switch (commandArray[1]) { 
case "forward": 
forwardMovement(commandArray); 
break; 
case "backward": 
backwardMovement (commandArray); 
break; 
case "left": 
leftMovement (commandArray); 
break; 
case "right": 
rightMovement (commandArray); 
break; 
default: System.out.println("Error in second command!"); 
} 
return " "; 
} 
public String forwardMovement (String commandArray[]) { 
switch (commandArray[2]) { 
    case "1": 
    serverGrid.serverPlayerMoveForward(1); 
    break; 
    case "2": 
    serverGrid.serverPlayerMoveForward(2); 
    break; 
    default: System.out.println("Error in third command!"); 
    } 
return " "; 
} 
public String backwardMovement (String commandArray[]) { 
switch (commandArray[2]) { 
    case "1": 
    serverGrid.serverPlayerMoveBackward(1); 
    break; 
    case "2": 
    serverGrid.serverPlayerMoveBackward(2); 
    break; 
    default: System.out.println("Error in third command!"); 
    } 
    return " "; 
} 
public String leftMovement (String commandArray[]) { 
switch (commandArray[2]) { 
    case "1": 
    serverGrid.serverPlayerMoveLeft(1); 
    break; 
    case "2": 
    serverGrid.serverPlayerMoveLeft(2); 
    break; 
    default: System.out.println("Error in third command!"); 
    } 
    return " "; 
} 
public String rightMovement (String commandArray[]) { 
switch (commandArray[2]) { 
    case "1": 
    serverGrid.serverPlayerMoveRight(1); 
    break; 
    case "2": 
    serverGrid.serverPlayerMoveRight(2); 
    break; 
    default: System.out.println("Error in third command!"); 
    } 
return " "; 
} 
} 

網格產生類:

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
public class ServerGridGenerator extends JFrame { 
public int serverPlayerXPos = 0; 
public int serverPlayerYPos = 0; 
public int clientPlayerXPos = 0; 
public int clientPlayerYPos = 9; 
public int endXPos = 9; 
public int endYPos = 5; 
int row = 10; 
int column = 10; 
int sizeGrid = 700; 
JButton[][] squareButtons = new JButton [row][column]; 

public void frameGen(){ 
ServerGridGenerator frame = new ServerGridGenerator(row, column); 
frame.setPreferredSize(new Dimension(sizeGrid, sizeGrid)); 
frame.setLocationRelativeTo(null); 
frame.pack(); 
frame.setVisible(true); 
} 

public ServerGridGenerator(int r, int c) { 
squareButtons = new JButton [r][c]; 
Container pane = getContentPane(); 
pane.setLayout(new GridLayout(r, c)); 
for(int y=0; y<c; y++){ 
    for (int x=0; x<r; x++) { 
     squareButtons[y][x] = new JButton(""); 
     squareButtons[y][x].setOpaque(true); 
     squareButtons[y][x].setBackground(Color.white); 
     squareButtons[y][x].setEnabled(false); 
     pane.add(squareButtons[y][x]); 
    }   
} 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
     squareButtons[clientPlayerYPos][clientPlayerXPos].setText(" P2"); 
     squareButtons[endYPos][endXPos].setText(" END"); 
} 

public void serverPlayerMoveRight (int moveBy){ 
    for (int i=0; i<moveBy; i++) { 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" "); 
     serverPlayerXPos = serverPlayerXPos + 1; 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
    } 
} 
public void serverPlayerMoveLeft (int moveBy){ 
    for (int i=0; i<moveBy; i++) { 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" "); 
     serverPlayerXPos = serverPlayerXPos - 1; 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
    } 
} 
public void serverPlayerMoveForward (int moveBy){ 
    for (int i=0; i<moveBy; i++) { 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" "); 
     serverPlayerYPos = serverPlayerYPos + 1; 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
    } 
} 
public void serverPlayerMoveBackward (int moveBy){ 
    for (int i=0; i<moveBy; i++) { 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" "); 
     serverPlayerYPos = serverPlayerYPos - 1; 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
     } 
} 

我的問題是,我可以得出第一個 「P1」, 「P2」, 「END」 但隨後分析器時調用向前,向後,向左,向右移動的方法,但它不會繪製在網格上,你能幫助解釋爲什麼發生這種情況,我能做些什麼來解決這個問題?

感謝您的時間

===================================== ================================================================

編輯:

我已經意識到我做錯了什麼,我需要調用gridGenerator /構造方法內的運動。我已經改變了代碼並且硬編碼了一個值並且它可以工作,但是現在我需要使解析器從構造函數調用繪圖方法,並且即時處理這個問題。你能幫我畫出下面例子的動作嗎?但是我不需要硬編碼,我需要從解析器中獲得這些值。

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
public class ServerGridGenerator extends JFrame { 
public int serverPlayerXPos = 0; 
public int serverPlayerYPos = 0; 
public int clientPlayerXPos = 0; 
public int clientPlayerYPos = 9; 
public int endXPos = 9; 
public int endYPos = 5; 
int row = 10; 
int column = 10; 
int sizeGrid = 700; 
JButton[][] squareButtons = new JButton [row][column]; 
//public ServerPlayerParsing serverpc = new ServerPlayerParsing(); 

public void frameGen(){ 
ServerGridGenerator frame = new ServerGridGenerator(row, column); 
frame.setPreferredSize(new Dimension(sizeGrid, sizeGrid)); 
frame.setLocationRelativeTo(null); 
frame.pack(); 
frame.setVisible(true); 
} 

public ServerGridGenerator(int r, int c) { 
squareButtons = new JButton [r][c]; 
Container pane = getContentPane(); 
pane.setLayout(new GridLayout(r, c)); 
for(int y=0; y<c; y++){ 
    for (int x=0; x<r; x++) { 
     squareButtons[y][x] = new JButton(""); 
     squareButtons[y][x].setOpaque(true); 
     squareButtons[y][x].setBackground(Color.white); 
     squareButtons[y][x].setEnabled(false); 
     pane.add(squareButtons[y][x]); 
    }   
} 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
     squareButtons[clientPlayerYPos][clientPlayerXPos].setText(" P2"); 
     squareButtons[endYPos][endXPos].setText(" END"); 
     serverPlayerMoveRight(6); // <============ Hard coded value 
} 

public void serverPlayerMoveRight (int moveBy){ 
    for (int i=0; i<(moveBy+1); i++) { 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" "); 
     serverPlayerXPos = serverPlayerXPos + 1; 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
     repaint(); 
     validate(); 
    } 
} 
public void serverPlayerMoveLeft (int moveBy){ 
    for (int i=0; i<(moveBy+1); i++) { 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" "); 
     serverPlayerXPos = serverPlayerXPos - 1; 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
    } 
} 
public void serverPlayerMoveForward (int moveBy){ 
    for (int i=0; i<(moveBy+1); i++) { 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" "); 
     serverPlayerYPos = serverPlayerYPos + 1; 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
    } 
} 
public void serverPlayerMoveBackward (int moveBy){ 
    for (int i=0; i<(moveBy+1); i++) { 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" "); 
     serverPlayerYPos = serverPlayerYPos - 1; 
     squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
     } 
} 
public void timeDelay(){ 
    try { 
     Thread.sleep(1000); 
    } catch(InterruptedException ex) { 
     Thread.currentThread().interrupt(); 
    } 
} 
} 
+0

你怎麼跑的?與上面的例子「驗證」 –

+0

永遠不會被調用。那麼,這是你的問題的答案? –

+0

你有沒有試過ebugging的代碼?如果你是在事件調度線程上,那麼很大程度上取決於您是否需要使用 –

回答

1

我認爲這個問題的末尾添加一個pane.repaint()是,你實際上是創建你的框架的多個實例,但只能看到其中的一個。內部驅動程序類,你有下面的代碼:

public class ServerPlayerParsing {  
    ServerGridGenerator serverGrid = new ServerGridGenerator (10, 10); 
    public String validate(String command){ 
     serverGrid.frameGen(); 
     if (wordCount(command)== 3) { 
      String[] commandArray = command.split(" "); 
      commandParsing(commandArray); 
     } else { 
      System.out.println("Error! Format incorrect!"); 
      System.out.println("Correct format = [COMMAND 1] [COMMAND 2] [COMMAND 3]"); 
     } 
     return ""; 
    } 

注意到你如何創建ServerGridGenerator作爲一個一流水平的對象,然後你調用serverGrid.frameGen()驗證命令裏面。 frameGen有這樣的代碼:

public void frameGen(){ 
    ServerGridGenerator frame = new ServerGridGenerator(row, column); 
    frame.setPreferredSize(new Dimension(sizeGrid, sizeGrid)); 
    frame.setLocationRelativeTo(null); 
    frame.pack(); 
    frame.setVisible(true); 
} 

所以每次你打電話的驗證方法創建的ServerGridGenerator框架的另一個實例,使該幀可見。如果您正在創建多個幀,請查看是否執行多個命令。

還注意到您創建的實例serverGrid永遠不會顯示,但那是正在接收移動命令的實例。

=====編輯迴應請求。例如=====

與您最初發布的ServerGridGenerator代碼開始,我做了以下修改僅構造函數中,然後靜置原樣:

public ServerGridGenerator(int r, int c) { 
    squareButtons = new JButton [r][c]; 
    Container pane = getContentPane(); 
    pane.setLayout(new GridLayout(r, c)); 
    for(int y=0; y<c; y++){ 
     for (int x=0; x<r; x++) { 
      squareButtons[y][x] = new JButton(""); 
      squareButtons[y][x].setOpaque(true); 
      squareButtons[y][x].setBackground(Color.white); 
      squareButtons[y][x].setEnabled(false); 
      pane.add(squareButtons[y][x]); 
     }   
    } 
    squareButtons[serverPlayerYPos][serverPlayerXPos].setText(" P1"); 
    squareButtons[clientPlayerYPos][clientPlayerXPos].setText(" P2"); 
    squareButtons[endYPos][endXPos].setText(" END"); 

    /*****  ADDED THESE LINES *******/ 
    setPreferredSize(new Dimension(sizeGrid, sizeGrid)); 
    setLocationRelativeTo(null); 
    pack(); 
    setVisible(true); 
} 

和我完全刪除的方法frameGen,因爲它是不必要的。然後在ServerPlayerParsing代碼中,在validate方法中,我刪除了對serverGrid.frameGen()的調用,因爲它不再存在。

測試代碼,我添加了一個主要以ServerPlayerParsing和輸入迴路接受命令,但另有留下的代碼不變:

import java.io.*; 

public class ServerPlayerParsing { 

    /*** NEED A MAIN TO TEST THE CODE ****/ 
    static public void main(String[] args) throws Exception { 
     ServerPlayerParsing td = new ServerPlayerParsing(); 
     td.go(); 
    } 

    /***** PROVIDES A SIMPLE USER PROMPT/INPUT LOOP FOR TESTING ****/ 
    public void go() throws Exception { 
     DataInputStream cin = new DataInputStream(System.in); 
     boolean running = true; 
     String line; 

     while(running) { 
     System.out.printf("> "); 
     line = cin.readLine(); 
     line = line.trim(); 
     if(line.length() == 0) continue; 
     if(line.equalsIgnoreCase("EXIT")) { 
      running = false; 
      continue; 
     } 
     System.out.println("Validate: " + validate(line.toLowerCase())); 
     } 
     System.exit(0); 
    } 

    /***** THE BELOW IS ALL YOUR ORIGINAL EXCEPT FOR THE frameGen CALL ***/ 
    ServerGridGenerator serverGrid = new ServerGridGenerator (10, 10); 
    public String validate(String command){ 
    /** NO LONGER NEEDED serverGrid.frameGen(); */ 
    if (wordCount(command)== 3) { 
     String[] commandArray = command.split(" "); 
     commandParsing(commandArray); 
    } else { 
     System.out.println("Error! Format incorrect!"); 
     System.out.println("Correct format = [COMMAND 1] [COMMAND 2] [COMMAND 3]"); 
    } 
    return ""; 
    } 
    public int wordCount(String command){ 
    String[] commandCount = command.split("\\s"); 
    return commandCount.length; 
    } 
    public String commandParsing(String[] commandArray) { 
    switch (commandArray[0]) { 
     case "move": 
     secondCommand (commandArray); 
     break; 
     default: System.out.println("Error in first command!"); 
    }   
    return " "; 
    } 
    public String secondCommand (String commandArray[]) { 
    switch (commandArray[1]) { 
     case "forward": 
      forwardMovement(commandArray); 
      break; 
     case "backward": 
      backwardMovement (commandArray); 
      break; 
     case "left": 
      leftMovement (commandArray); 
      break; 
     case "right": 
      rightMovement (commandArray); 
      break; 
     default: System.out.println("Error in second command!"); 
    } 
    return " "; 
    } 
    public String forwardMovement (String commandArray[]) { 
    switch (commandArray[2]) { 
     case "1": 
     serverGrid.serverPlayerMoveForward(1); 
     break; 
     case "2": 
     serverGrid.serverPlayerMoveForward(2); 
     break; 
     default: System.out.println("Error in third command!"); 
     } 
     return " "; 
    } 
    public String backwardMovement (String commandArray[]) { 
     switch (commandArray[2]) { 
     case "1": 
      serverGrid.serverPlayerMoveBackward(1); 
      break; 
     case "2": 
      serverGrid.serverPlayerMoveBackward(2); 
      break; 
     default: System.out.println("Error in third command!"); 
     } 
     return " "; 
    } 
    public String leftMovement (String commandArray[]) { 
     switch (commandArray[2]) { 
      case "1": 
      serverGrid.serverPlayerMoveLeft(1); 
      break; 
      case "2": 
      serverGrid.serverPlayerMoveLeft(2); 
       break; 
      default: System.out.println("Error in third command!"); 
     } 
     return " "; 
    } 
    public String rightMovement (String commandArray[]) { 
     switch (commandArray[2]) { 
      case "1": 
      serverGrid.serverPlayerMoveRight(1); 
      break; 
      case "2": 
       serverGrid.serverPlayerMoveRight(2); 
       break; 
       default: System.out.println("Error in third command!"); 
     } 
     return " "; 
    } 
    } 

此代碼本身將作爲您的預期,但我可以沒有看到你的代碼的其餘部分我不知道你的主要是正在與解析器類交互。

該代碼也可以清理很多。例如,不是將字符串傳遞給所有各種移動...方法並打開值,您可以使用Integer.parseInt()解析第3個命令數組元素。如果解析成功,則該值是一個整數,否則該命令是不好的。然後,你可以消除所有的移動功能和處理,在secondCommmand方法是這樣的:

public String secondCommand (String commandArray[]) { 
    int steps = -1; 
    try { 
    steps = Integer.parseInt(commandArray[2]) ; 
    } catch(Exception ex) { 
    steps = -1; 
    } 
    if(steps == -1 || steps > 2) { 
    System.out.println("Error in third command."); 
    } else { 
    switch (commandArray[1]) { 
     case "forward": 
     serverGrid.serverPlayerMoveForward(steps); 
     break; 
     case "backward": 
     serverGrid.serverPlayerMoveBackward(steps); 
     break; 
     case "left": 
     serverGrid.serverPlayerMoveLeft(steps); 
     break; 
     case "right": 
     serverGrid.serverPlayerMoveRight(steps); 
     break; 
     default: System.out.println("Error in second command!"); 
    } 
    } 
    return " "; 
} 
+0

我創建了框架和網格在一個構造方法中,它的工作原理,解析也可以,但即時通訊有其他問題。感謝您的幫助,儘管嘗試和解決它們 –

0

嘗試在每個serverPlayerMoveX方法

+0

我現在還沒有一個體面的Java編輯器,如果沒有人的回答,我今天再次檢查。 –