Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 骑士';图形用户界面在图形处理中的应用_Java_User Interface_Applet_Processing_Backtracking - Fatal编程技术网

Java 骑士';图形用户界面在图形处理中的应用

Java 骑士';图形用户界面在图形处理中的应用,java,user-interface,applet,processing,backtracking,Java,User Interface,Applet,Processing,Backtracking,我正在用基本gui处理骑士之旅的问题,我想在两个文本字段中输入用户的输入,这两个文本字段组成了用户的(x,y),然后在一个文本框中打印解决方案是否可行,在另一个文本框中我写骑士采用的路径。我的算法工作正常,gui中有问题。我给了(x,y)一些默认值因此,我得到了正确的输出。但是当我更改文本字段中(x,y)的值时,不会发生任何更改。这是主文件,下面是另一个事件处理程序文件。非常感谢您的帮助。我正在处理2.2.1。这就是输出屏幕的外观 主文件/project.pde // Need G4P libr

我正在用基本gui处理骑士之旅的问题,我想在两个文本字段中输入用户的输入,这两个文本字段组成了用户的(x,y),然后在一个文本框中打印解决方案是否可行,在另一个文本框中我写骑士采用的路径。我的算法工作正常,gui中有问题。我给了(x,y)一些默认值因此,我得到了正确的输出。但是当我更改文本字段中(x,y)的值时,不会发生任何更改。这是主文件,下面是另一个事件处理程序文件。非常感谢您的帮助。我正在处理2.2.1。这就是输出屏幕的外观

主文件/project.pde

// Need G4P library
import g4p_controls.*;
Maxim maxim;
AudioPlayer player;
int x=-1;
int y=-1;
String solution="";
PImage img;
int count=0;

public void setup(){
  size(480, 320, JAVA2D);
  maxim=new Maxim(this);
  player=maxim.loadFile("song.wav");
  player.setLooping(true);

  img=loadImage("chess.jpg");

  createGUI();
  customGUI();

  // Place your setup code here

}
int t[]=new int[25];
boolean visited[][]=new boolean[5][5];
int check[][]=new int[5][5];
boolean b;
int counter=-1;
boolean move(int x,int y , int m){
 boolean result=false; 
   if (x<0 || x>=5 || y<0 || y>=5 || visited[x][y]==true)
   {
        return false;
   }

    visited[x][y]=true;

    if (m==24)
    {

        visited[x][y]=true;

        return true;
    }
    else
    {
      String xstring=String.valueOf(x);
      String ystring=String.valueOf(y);
      solution=solution+xstring+","+ystring+"  ";
        print (x);
        print(",");
        print(y);
        check[x][y]=counter+1;
        if (move(x+2,y+1,m+1) || move(x+2,y-1,m+1)
            || move(x-2,y+1,m+1) || move(x-2,y-1,m+1)
            || move(x+1,y+1,m+1) || move(x+1,y-1,m+1)
            || move(x-1,y+1,m+1) || move(x-1,y-1,m+1)){
            print (x);
            print(",");
            print(y);
            //check[x][y]=1;
            return true;

            }


    return false;


}
}


public void draw(){
 counter=counter+1; 
   background(0,128,128);
   image(img,0,0,480,320);
   player.play();
   textarea2.setText(solution);


   String txt1 = textfield1.getText();
   x = Integer.parseInt(txt1);

   String txt2 = textfield2.getText();
   y= Integer.parseInt(txt2);
   print(solution);
   if(x>=0 && y>=0)
   {
     b=move(x,y,0);


     if(b==false)
     {
       textarea1.setText("Solution is not possible,enter other coordinates");
     }
     if(b==true)
     {
       textarea1.setText("Congratulations solution is possible");
     }
   }
   if(count%8==0)
   {
     delay(1000);
     println(counter);

   }

}
void keyPressed()
{
  if (key==13)
  {
    solution="";
    print(solution);
    textarea2.setText(solution);
    String txt1 = textfield1.getText();
   x = Integer.parseInt(txt1);

   String txt2 = textfield2.getText();
   y= Integer.parseInt(txt2);
  }
    if(x>=0 && y>=0)
   {
     b=move(x,y,0);


     if(b==false)
     {
       textarea1.setText("Solution is not possible,enter other coordinates");
     }
     if(b==true)
     {
       textarea1.setText("Congratulations solution is possible");
     }
   }
}

// Use this method to add additional statements
// to customise the GUI controls
public void customGUI(){

}

上述结构存在一些问题。 在
draw()方法中执行递归
move()
方法。但在处理过程中,动画线程每秒多次调用
draw()

此类情况的常见设计为:

  • 您应该具有保存应用程序状态的变量(逻辑状态)
  • draw()
    方法绘制的内容仅取决于状态
  • 状态可以通过动画线程或任何其他线程进行修改
我建议稍微修改一下代码:

第一状态变量:

// 1 - display status, wait to enter values
// 2 - check if x and y are correct
// 3 - solve problem
int state=1;
boolean solutionExists=false;

public void setup() {
...
}
下一个draw()方法:

最后是文本字段处理程序:

public void tf1(GTextField source, GEvent event) {
  if (event.getType().equals("LOST_FOCUS")) {
    state=2;
  }
}

public void tf2(GTextField source, GEvent event) {
  if (event.getType().equals("LOST_FOCUS")) {
    state=2;
  } 
}
如你所见:

  • 如果
    状态==1
    -draw()仅更新消息
  • 如果
    状态==2
    -draw()检查x和y是否有效,如果有效->将状态更改为3
  • 如果
    状态==3
    -draw()执行递归算法,则更新solutionExists变量,将状态更改为1
当textfield1或textfield2失去焦点时,它会将状态更改为2

  • draw()仅由应用程序状态驱动
  • 应用程序状态由其他事件修改
为了获得最佳结果,递归算法应该在另一个线程中执行,而不是在动画线程中执行


一个很好的注意事项:当您编辑文本字段时,它可能包含字符串,如
(空)或
“3”
(前导空格)或
“3”
等-此类文本不能用Integer.parseInt解析-您需要修剪此类文本,并确保不会引发NumberFormatException-请参阅
readXY()
方法。

您可能希望编辑您的帖子,使其仅包含一个技术问题。关于你的截止日期和恳求人们阅读你的问题的那一长段只会让阅读你的问题变得更加困难。凯文,我已经做了更新,OK you@przemek hertel,你的修改救了我的命,还有一件事,解决方案字符串没有更新,因为在解决方案中,只显示第一组坐标可能会有帮助,如果你能帮我的话,否则谢谢!!
void draw() {

   counter=counter+1; 
   background(0,128,128);

   String coords = "(" + x + "," + y + ")";

  if (state == 1) {
     if(solutionExists) {
       textarea1.setText("Congratulations solution is possible  " + coords);       
     } else {
       textarea1.setText("Solution is not possible,enter other coordinates  " +coords);
     }

     return; 
  }

  if (state == 2) {
    readXY();
    return;
  }

  if (state == 3) {
    println("find solution for: " + coords);
    solutionExists = move(x,y,0);
    state = 1;
    return;
  }

}

public void readXY() {
  try {
   x = Integer.parseInt(textfield1.getText().trim());
   y = Integer.parseInt(textfield2.getText().trim());
   state = 3;
  } catch(Exception e) {
    state = 1;
  }

}
public void tf1(GTextField source, GEvent event) {
  if (event.getType().equals("LOST_FOCUS")) {
    state=2;
  }
}

public void tf2(GTextField source, GEvent event) {
  if (event.getType().equals("LOST_FOCUS")) {
    state=2;
  } 
}