我正在製作一個基於局域網命令的遊戲,我目前正在將結構化的英語解析爲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();
}
}
}
你怎麼跑的?與上面的例子「驗證」 –
永遠不會被調用。那麼,這是你的問題的答案? –
你有沒有試過ebugging的代碼?如果你是在事件調度線程上,那麼很大程度上取決於您是否需要使用 –