我正在设计一个简单的多人 java 游戏。我想从 JAR 文件的小程序中运行 java 游戏。我的代码目前能够将坐标发送到服务器,但是当我添加代码来初始化小程序时,我将在其中运行游戏,它会冻结并且无法到达发送坐标的代码。
我在我的代码中做错了什么吗?
客户端.java
package Client_Test;
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class Client {
public static void main(String [] args) throws UnknownHostException, IOException {
Client_Applet Client_AppletObj = new Client_Applet();
String playerX = "3001", playerY = "300";
String serverName = "127.0.0.1";
int port = 6789;
int port1 = 6788;
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);
Socket client1 = new Socket(serverName, port1);
System.out.println("Connected");
Scanner test = new Scanner(System.in);
OutputStream outsetup = client.getOutputStream();
OutputStream out1setup = client1.getOutputStream();
DataOutputStream out = new DataOutputStream(outsetup);
DataOutputStream out1 = new DataOutputStream(out1setup);
//freezes here
Client_AppletObj.init();
Client_AppletObj.start();
Client_AppletObj.run();
while(true) {
playerX = test.nextLine();
playerY = test.nextLine();
if (playerX == "stop") {
System.exit(1);
}
out.writeUTF(playerX);
out1.writeUTF(playerY);
}
}
}
我的 Client_Applet 代码:
package Client_Test;
import java.applet.Applet;
public class Client_Applet extends Applet implements Runnable{
@Override
public void init() {
this.setSize(500, 800);
}
@Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void run() {
while(true) {
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
我也看到有人说用小程序编码不好,我应该使用 SWING 吗?