JPanel刷新游戏问题

时间:2014-03-24 12:01:05

标签: jpanel

我正在制作基于LAN命令的游戏,我目前正在将结构化英语解析为java。我已经解析了所有内容,我可以绘制到网格,但我只能在运行时硬编码文本到网格,并且似乎无法使用新文本刷新网格。

解析类:

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 " ";
}
} 

Grid Generator类:

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");
      }
 }

我的问题是我可以画第一个&#34; P1&#34;,&#34; P2&#34;,&#34; END&#34;但是当解析器调用向前,向后,向左,向右移动的方法但它没有在网格上绘制时,你能帮助解释为什么会发生这种情况以及我能做些什么来解决这个问题?

感谢您的时间

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

编辑:

我已经意识到我做错了什么,我需要调用gridGenerator / constructor方法中的动作。我已经更改了代码并对一个值进行了硬编码并且它可以工作,但现在我需要让解析器从构造函数中调用绘图方法,并且我遇到了麻烦。你可以帮我打电话画下面的例子,但不是硬编码我需要从解析器获取的值。

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();
    }
 }
}

2 个答案:

答案 0 :(得分:1)

我认为问题是你实际上是在创建框架的多个实例,但只看到其中一个。在您的驱动程序类中,您有以下代码:

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()的validate命令中。 frameGen 有以下代码:

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

因此,每次调用 validate 方法时,都会创建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代码中,在验证方法内,我删除了对 serverGrid.frameGen()的调用,因为它已不存在。

为了测试代码,我向ServerPlayerParsing添加了一个main,并接受了一个输入循环来接受命令,但是原则上保留了代码:

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 " ";
    }
  } 

此代码本身可以按预期工作,但由于我无法看到其余代码,因此我不知道 main 如何与解析器类进行交互。

代码也可以清理一下。例如,不是将字符串传递给所有各种 move ... 方法并打开值,而是​​使用 Integer.parseInt()。如果解析成功,则该值为整数,否则命令为坏。然后你可以消除所有的移动函数并在 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 " ";
}

答案 1 :(得分:0)

尝试在每个serverPlayerMoveX方法的末尾添加pane.repaint()