我正在设计一款简单的多人Java游戏。我想从JAR文件中的applet中运行java游戏。我的代码目前能够将坐标发送到服务器,但是当我添加代码来初始化applet时,我将运行游戏,它冻结并且没有到达代码来发送坐标。
我的代码中有什么问题吗?
Client.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();
}
}
}
}
另外我看到有人说用applet编码很糟糕,我应该使用SWING吗?