I am designing a simple multiplayer java game. I want to run the java game in an applet from a JAR file. I my code currently is able to send coordinates to the server but when I add code to initialize the applet I will run the game in, it freezes and doesn't reach the code to send the coordinates.
Am I doing anything wrong in my code?
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);
}
}
}
My Client_Applet Code:
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();
}
}
}
}
Also I have seen some people say that coding with applets are bad, should I be using SWING?