title: Java applet: Where to call repaint();?
body: <p>Thank you for taking the time to read this. I am attempting to write some code to connect to a Lantronix UDS-10 Serial/Network interface. My applet runs, but it won't repaint. If you could give me any pointers, I'd appreciate it. Believe it or not, I actually went to school for this (Bachelor's Degree in Computer Science). However, that was two years ago, and I pretty much haven't touched it since. I'm pretty good when it comes to the basics (classes, methods, variables, etc.), but I'm afraid I'm a bit lacking on the graphics and threading / multi-threading. A lot of the code came from the Lantronix manual (available here: <a href="http://www.endurance-rc.com/media/Creating_Custom_Web_Pages.pdf" rel="nofollow">http://www.endurance-rc.com/media/Creating_Custom_Web_Pages.pdf</a>).</p>
  
  <p>The code requires a string of 5 'chunks' of characters, each separated by a space, sent by a ServerSocket over port 10001.</p>
  
  <p>EDIT: The server updates the string regularly. If a window is moved over my applet, the part of the applet underneath the window will repaint. However, I can't seem to find the way to trigger it whenever the characters change.
  /EDIT</p>
  
  <p>Any direction you can give me would be much appreciated.</p>
  
  <p>VerifyGas.java</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.applet.Applet;
  import java.net.*;
  import java.io.*;
  import java.lang.*;
  import java.text.*;
  import java.util.*;
  
  import javax.swing.JApplet;
  
  public class VerifyGas extends JApplet {
  
  static final long serialVersionUID = 1;
  tcpip gtp = null;
  InetAddress reader_ip = null;
  int port = 10001;
  TankData tankData;
  Text_io text_io;
  
  public void init() {
      gtp = null;
      reader_ip = null;
      port = 10001;
  }
  
  public void start() {
  
      /*  Get the IP address from the HTTP server
      */
  
      try{
          reader_ip = InetAddress.getByName(getCodeBase().getHost());
      }
      catch (UnknownHostException e){
  
      }
  
      /* Open a socket to the Device Server's serial port
      */
  
      if (reader_ip != null) {
          if (gtp == null) {
              gtp = new tcpip(reader_ip, port);
              if (gtp.s == null) {
                  System.out.println("Connection failed!");
                  gtp = null;
              }
          }
      }
      if (gtp == null) {
          System.out.println("Not connected.");
          return;
      }
      System.out.println("Connected!");
  
      /* You may now perform IO with the Device Server via
      * gtp.send(byte[] data_out);
      * byte[] data_in = gtp.receive();
      * functions.
      * In our example we'll use two TextBoxes which have
      * been extended to handle IO to the Device Server.
      * Data typed in the upper text box will be sent to
      * the Device Server, and data received will be
      * displayed in the lower text box.
      */
  
      /********************************************************/
      /* Start of custom application code */
      /* ADD YOUR CODE HERE */
      /********************************************************/
  
      text_io = new Text_io(gtp);
      while (text_io == null) {
          System.out.println("text_io is null!");
          try {
              Thread.sleep(1000);
          }
          catch (InterruptedException e) {
  
          }
      }
      try {
          Thread.sleep(1000);
      }
      catch (InterruptedException e) {
  
      }
      tankData = text_io.tankData;
  
      /********************************************************/
      /* End of custom application code */
      /********************************************************/
  }
  
  public void paint(Graphics g) {
  
      super.paint(g);
  
      System.out.println("Start paint()");
  
      this.setSize(200,600);                              // Set applet size
  
      setBackground(Color.green);                         // Background color
  
      g.setColor(Color.black);                            // Border color
      g.drawRect (0, 0, 198, 598);                        // Border
      g.drawRect (1, 1, 198, 598);                        // Border
  
      g.setColor(Color.black);                            // Header color
      g.setFont(new Font("Helvetica", Font.BOLD,20));     // Header font
  
      g.drawString("Joe's Eatery",   40, 40);             // Location
      g.drawString("126 Main St.",   40, 60);             // Location
      g.drawString("Cape Coral, FL", 40, 80);             // Location
  
      g.fillRect(2,100,196,2);                            //  Line 1
      g.fillRect(2,200,196,2);                            //  Line 2
      g.fillRect(2,300,196,2);                            //  Line 3
      g.fillRect(2,400,196,2);                            //  Line 4
      g.fillRect(2,500,196,2);                            //  Line 5
  
      g.drawString("Tank Pressure",      30,130);         // Header text
      g.drawString("Discharge Pressure",  9,230);         // Header text
      g.drawString("Tank Weight",        35,330);         // Header text
      g.drawString("Temperature (F" +
              (char) 176 + ")",          25,430);         // Header text
      g.drawString("Nitrogen Pressure",  18,530);         // Header text
  
      g.setColor(Color.red);                              // Color of digits
      g.setFont(new Font("Helvetica", Font.BOLD,48));     // Style of digits
  
  
      while(tankData == null) {
          System.out.println("tankData is null!");
          try {
              Thread.sleep(1000);
          }
          catch (InterruptedException e) {
  
          }
      }
  
      g.drawString(tankData.getTankPressure(),  55, 180); // Digits
      g.drawString(tankData.getDischPressure(), 55, 280); // Digits
      g.drawString(tankData.getWeight(),        55, 380); // Digits
      g.drawString(tankData.getTemperature(),   70, 480); // Digits
      g.drawString(tankData.getNitPressure(),   55, 580); // Digits
  
      System.out.println("End paint()");
  
  } // End of paint()
  
  public void destroy() {
      if (gtp != null)
      gtp.disconnect();
      gtp = null;
  }
  public void stop() {
  
  }
  public static void main(String[] args) {
      Frame frame = new Frame("VerifyGas");
      frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
              System.exit(0);
          }
      });
      VerifyGas ap = new VerifyGas();
      frame.add(ap);
      ap.init();
      ap.start();
      frame.pack();
  }
  }
  </code></pre>
  
  <p>Text_io.java</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.lang.*;
  
  import javax.swing.JPanel;
  
  public class Text_io extends JPanel implements Runnable {
  
  static final long serialVersionUID = 1;
  private tcpip gtp;
  Thread timer;
  TankData tankData;
  
  public Text_io(tcpip tp) {
      gtp = tp;
  
      // GUI WAS HERE
  
      tankData = new TankData();
      timer = new Thread(this);
      timer.start();
  }
  
  public void run() {
      this.validate();
      int i;
      byte[] in;
      Thread me = Thread.currentThread();
      while (timer == me) {
          try {
              Thread.sleep(1000);
          }
          catch (InterruptedException e) {
  
          }
          if ( (gtp != null) &amp;&amp; ((i = gtp.available()) &gt; 0) ) {
              try {
                  Thread.sleep(100);
              }
              catch (InterruptedException e) {
  
              }
              in = gtp.receive();
              try {
                  Thread.sleep(100);
              }
              catch (InterruptedException e) {
  
              }
              /* remove non-printing bytes */
              for (i = 0; i &lt; in.length; i++) {
                  if (in[i] &lt; 0x20)
                      in[i] = 0x20;
              }
              if(in.length &gt;= 5) {
                  tankData.loadTankData(in);
                  System.out.println("Text_io.TankData loaded!");
                  tankData.printValues();
              }
          }
      }
  }
  }
  </code></pre>
  
  <p>tcpip.java</p>
  
  <pre><code>import java.*;
  import java.lang.*;
  import java.net.*;
  import java.util.*;
  import java.io.*;
  
  /*
  * This class opens a TCP connection,
  * and allows reading and writing of byte arrays.
  */
  
  public class tcpip {
  
  protected Socket s = null;
  public DataInputStream dis = null;
  protected DataOutputStream dos = null;
  
  public tcpip(InetAddress ipa, int port) {
  
      Socket s1 = null;
  
      try { // Open the socket
          s1 = new Socket(ipa.getHostAddress(), port);
      }
      catch (IOException e) {
          System.out.println("Error opening socket");
          return;
      }
  
      s = s1;
  
      try { // Create an input stream
          dis = new DataInputStream(new
              BufferedInputStream(s.getInputStream()));
      }
      catch(Exception ex) {
          System.out.println("Error creating input stream");
      }
      try { // Create an output stream
          dos = new DataOutputStream(new
                  BufferedOutputStream(s.getOutputStream()));
      }
      catch(Exception ex) {
          System.out.println("Error creating output stream");
      }
  }
  public synchronized void disconnect() {
      if (s != null) {
          try {
              s.close();
          }
          catch (IOException e){
  
          }
      }
  }
  
  public synchronized void send(byte[] temp) {
      try {
          dos.write(temp, 0, temp.length);
          dos.flush();
      }
      catch(Exception ex) {
          System.out.println("Error sending data : " + ex.toString());
      }
  }
  
  public synchronized void send(byte[] temp, int len) {
      try {
          dos.write(temp, 0, len);
          dos.flush();
      }
      catch(Exception ex) {
          System.out.println("Error sending data : " + ex.toString());
      }
  }
  
  public synchronized void send(String given) {
      // WARNING: this routine may not properly convert Strings to bytes
      int length = given.length();
      byte[] retvalue = new byte[length];
      char[] c = new char[length];
      given.getChars(0, length, c, 0);
      for (int i = 0; i &lt; length; i++) {
          retvalue[i] = (byte)c[i];
      }
      send(retvalue);
  }
  
  public synchronized byte[] receive() {
  
      byte[] retval = new byte[0];
      try {
          while(dis.available() == 0); /* Wait for data */
      }
      catch (IOException e){
  
      }
      try {
          retval = new byte[dis.available()];
      }
      catch (IOException e){
  
      }
      try {
          dis.read(retval);
      }
      catch (IOException e){
  
      }
      return(retval);
  }
  
  public int available() {
      int avail;
      avail = 0;
      try {
          avail = dis.available();
      }
      catch (IOException e) {
  
      }
      return(avail);
  }
  }
  </code></pre>
  
  <p>TankData.java</p>
  
  <pre><code>public class TankData {
  
  private String Weight;
  private String TankPressure;
  private String DischPressure;
  private String NitPressure;
  private String Temperature;
  
  public TankData() {
  
      this.Weight = "";
      this.TankPressure = "";
      this.DischPressure = "";
      this.NitPressure = "";
      this.Temperature = "";
  
  }
  
  public void loadTankData(byte[] data) {
  
      String in = new String(data);
  
      String[] TankDataString = in.split(" ");
  
      this.Weight = TankDataString[0];
      this.TankPressure = TankDataString[1];
      this.DischPressure = TankDataString[2];
      this.NitPressure = TankDataString[3];
      this.Temperature = TankDataString[4];
  
  }
  
  public void printValues() {
      System.out.println ("Weight: " + Weight);
      System.out.println ("Tank Pressure: " + TankPressure);
      System.out.println ("Discharge Pressure: " + DischPressure);
      System.out.println ("Nitrogen Pressure: " + NitPressure);
      System.out.println ("Temperature: " + Temperature);
  }
  
  public String getWeight() {
      return Weight;
  }
  
  public String getTankPressure() {
      return TankPressure;
  }
  
  public String getDischPressure() {
      return DischPressure;
  }
  
  public String getNitPressure() {
      return NitPressure;
  }
  
  public String getTemperature() {
      return Temperature;
  }
  
  }
  </code></pre>