Java 当我运行两个类时,我的老鼠没有移动
我有一节迷宫课和一节老鼠课。它们都使用9个字符的字符串协议和套接字进行通信,老鼠告诉迷宫它想如何移动。迷宫移动它。为了显示老鼠正在我的迷宫中移动,我将迷宫路径的背景色设置为红色 每次老鼠在特定的路径上移动。但是,出于某种原因,这并没有发生。我完全不知道为什么。请帮忙Java 当我运行两个类时,我的老鼠没有移动,java,swing,user-interface,maze,Java,Swing,User Interface,Maze,我有一节迷宫课和一节老鼠课。它们都使用9个字符的字符串协议和套接字进行通信,老鼠告诉迷宫它想如何移动。迷宫移动它。为了显示老鼠正在我的迷宫中移动,我将迷宫路径的背景色设置为红色 每次老鼠在特定的路径上移动。但是,出于某种原因,这并没有发生。我完全不知道为什么。请帮忙 package TheMaze; import java.util.*; import java.awt.Color; import java.awt.GridLayout; import javax.swing.JButton;
package TheMaze;
import java.util.*;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.net.*;
import java.awt.*;
import java.io.*;
public class TheMaze {
static JButton[][] jb;
//static String r1, r2, r3, r4, r5, r6, r7, r8, r9;
static String m1, m2, m3, m4, m5, m6, m7, m8, m9;
static int row = 0;
static int column = 3;
static char[][] mProtocol;
static JFrame jf = new JFrame("Mazifying Maze");
Container contain = new Container();
public TheMaze(int length, int width) {
jb = new JButton[length][width];
mProtocol = new char[length][width];
contain = new Container();
for (int x = 0; x < width; x++) {
for (int y = 0; y < length; y++) {
jb[y][x] = new JButton();
jf.add(jb[y][x]);
}
}
jb[2][0].setBackground(Color.black);
jb[4][0].setBackground(Color.black);
jb[2][97].setBackground(Color.black);
jb[2][96].setBackground(Color.black);
jb[2][95].setBackground(Color.black);
jb[2][94].setBackground(Color.black);
jb[2][93].setBackground(Color.black);
jb[2][92].setBackground(Color.black);
jb[2][91].setBackground(Color.black);
.
.
.
Just goes on and on.
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (jb[i][j].getBackground() == Color.black) {
mProtocol[i][j] = 'w';
} else {
mProtocol[i][j] = 'p';
}
}
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new GridLayout(length, width));
jf.setSize(1400, 700);
jf.setVisible(true);
}
}
public static void ratMessage(String ratPos) {
System.out.println(ratPos);
String ratMessage = ratPos;
String r1 = ratMessage.substring(0, 1);
String r2 = ratMessage.substring(1, 2);
String r3 = ratMessage.substring(2, 3);
String r4 = ratMessage.substring(3, 4);
String r5 = ratMessage.substring(4, 5);
String r6 = ratMessage.substring(5, 6);
String r7 = ratMessage.substring(6, 7);
String r8 = ratMessage.substring(7, 8);
String r9 = ratMessage.substring(8, 9);
if (r2.equals('r')) {
jb[row - 1][column].setBackground(Color.red);
row = row - 1;
}
if (r4.equals('r')) {
jb[row][column - 1].setBackground(Color.red);
column = column - 1;
}
if (r6.equals('r')) {
jb[row][column + 1].setBackground(Color.red);
column = column + 1;
}
if (r8.equals('r')) {
jb[row + 1][column].setBackground(Color.red);
row = row + 1;
}
}
public static String mazeMessage() {
if (row - 1 < 0 && column - 1 < 0) {
m1 = "o";
m2 = "o";
m3 = "o";
m4 = "o";
m5 = "r";
m6 = mProtocol[row][column + 1] + "";
m7 = "o";
m8 = mProtocol[row + 1][column] + "";
m9 = mProtocol[row + 1][column + 1] + "";
} else if (row + 1 > 99 && column + 1 > 99) {
m1 = mProtocol[row - 1][column - 1] + "";
m2 = mProtocol[row - 1][column] + "";
m3 = "o";
m4 = mProtocol[row][column - 1] + "";
m5 = "r";
m6 = "o";
m7 = "o";
m8 = "o";
m9 = "o";
} else if (row - 1 < 0 && column + 1 > 99) {
m1 = "o";
m2 = "o";
m3 = "o";
m4 = mProtocol[row][column - 1] + "";
m5 = "r";
m6 = "o";
m7 = mProtocol[row + 1][column - 1] + "";
m8 = mProtocol[row + 1][column] + "";
m9 = "o";
} else if (row + 1 > 99 & column - 1 < 0) {
m1 = "o";
m2 = mProtocol[row - 1][column] + "";
m3 = mProtocol[row - 1][column + 1] + "";
m4 = "o";
m5 = "r";
m6 = mProtocol[row][column + 1] + "";
m7 = "o";
m8 = "o";
m9 = "o";
} else if (row - 1 < 0) {
m1 = "o";
m2 = "o";
m3 = "o";
m4 = "" + mProtocol[row][column - 1];
m5 = "r";
m6 = "" + mProtocol[row][column + 1];
m7 = mProtocol[row + 1][column - 1] + "";
m8 = mProtocol[row + 1][column] + "";
m9 = mProtocol[row + 1][column + 1] + "";
} else if (row + 1 > 99) {
m1 = mProtocol[row - 1][column - 1] + "";
m2 = mProtocol[row - 1][column] + "";
m3 = mProtocol[row - 1][column + 1] + "";
m4 = mProtocol[row][column - 1] + "";
m5 = "r";
m6 = mProtocol[row][column + 1] + "";
m7 = "o";
m8 = "o";
m9 = "o";
} else if (column - 1 < 0) {
m1 = "o";
m2 = mProtocol[row - 1][column] + "";
m3 = mProtocol[row - 1][column + 1] + "";
m4 = "o";
m5 = "r";
m6 = mProtocol[row][column + 1] + "";
m7 = "o";
m8 = mProtocol[row + 1][column] + "";
m9 = mProtocol[row + 1][column + 1] + "";
} else if (column + 1 > 99) {
m1 = mProtocol[row - 1][column - 1] + "";
m2 = mProtocol[row - 1][column] + "";
m3 = "o";
m4 = mProtocol[row][column - 1] + "";
m5 = "r";
m6 = "o";
m7 = mProtocol[row + 1][column - 1] + "";
m8 = mProtocol[row + 1][column] + "";
m9 = "o";
} else {
m1 = mProtocol[row - 1][column - 1] + "";
m2 = mProtocol[row - 1][column] + "";
m3 = mProtocol[row - 1][column + 1] + "";
m4 = mProtocol[row][column - 1] + "";
m5 = "r";
m6 = mProtocol[row][column + 1] + "";
m7 = mProtocol[row + 1][column - 1] + "";
m8 = mProtocol[row + 1][column] + "";
m9 = mProtocol[row + 1][column + 1] + "";
}
return m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9;
}
public static void main(String[] args) {//throws IOException{
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
TheMaze mz = new TheMaze(100, 100);
try {
ServerSocket svs = new ServerSocket(13000);
System.out.println("Has not Connected");
Socket socks = svs.accept();
System.out.println("Has Connected");
DataOutputStream p = new DataOutputStream(socks.getOutputStream());
InputStreamReader isr = new InputStreamReader(socks.getInputStream());
DataInputStream d = new DataInputStream(socks.getInputStream());
String theMazeMessage = "ooowrwwpw";
System.out.println("first message" + theMazeMessage);
p.writeUTF(theMazeMessage);
while (row != 99 && column != 87) {
System.out.println("in loop");// still going, not yet reached end of maze.
String recieving = d.readUTF();
System.out.println(recieving);
ratMessage(recieving);
p.writeUTF(theMazeMessage);
System.out.println(theMazeMessage);
}
socks.close();
p.close();
isr.close();
d.close();
} catch (IOException e) {
e.printStackTrace();
String theMazeMessage = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9;
}
}
}
我看到的主要问题是在ratMessage方法中,您正在比较字符串值和字符 这是行不通的,因为字符串和字符永远不可能相等,它们是错误的类型。如果相反,你把它们改成更像
if (r2.equals("r"))
你应该有更多的成功
还有很多其他的陷阱会引起一些悲伤。静态不是解决方案。过度使用静电会回来狠狠地咬你。在可能的情况下,您应该依赖于传递需要与之交互的对象的有效引用
就我个人而言,我会设计一系列描述预期合同的接口,例如ratMessage。您的UI将实现这个接口,它的引用应该传递给主comms类,该类从rat读取信息
您也违反了Swing的单线程规则。Swing要求对任何UI组件的所有交互和更新必须在事件调度线程的上下文中完成。因为您正在从主线程运行主循环,并调用ratMessage,然后更新按钮,所以您违反了此规则
请查看和以了解更多详细信息
该解决方案可能涉及一个SwingWorker以实现简单性或SwingUtilities.invokeLater以实现快速更改…您首先违反了ratMessage方法中Swing的单线程规则
if (r2.equals('r'))
if (r2.equals("r"))