1

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?

IslaStewart
  • 311
  • 2
  • 11
  • You should not write [JAVA] in the title. That's what the tags are for :-) – Eric J. Jun 21 '13 at 07:21
  • Ok, sorry I'll take it out – IslaStewart Jun 21 '13 at 07:29
  • 1
    You cannot display the applet by calling `init` , you need some container for the applet to display. You may add the applet to a `JFrame` and then call the applet's lifecycle methods sequentially. BTW use `JApplet`. `Applet` is old and outdated. – Extreme Coders Jun 21 '13 at 07:36
  • *"Also I have seen some people say that coding with applets are bad, should I be using SWING?"* It is Swing & there is `JApplet`, but use a `JFrame` instead of either. See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jun 21 '13 at 08:53

0 Answers0