0

When I run the following code, the applet viewer appears and the serial connection works but when I create this thread in another class and start it applet viewer does not appear but the the serial connection works

package parkingStatus;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;

import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;


public class lifeDisplay extends Applet implements Runnable {

    SerialPort serialPort; 
    private static final String PORT_NAMES[] = {"COM20"};
    private BufferedReader input;
    private OutputStream output = null;
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 9600;
    String ParkingStatus = "";
    Image backGround;

    public lifeDisplay (){}

    public void run() {
        repaint();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while (true){
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        //String ParkingStatus = "";
        repaint();
        ParkingStatus = initialize("2", "2");//to serial // in the uc 0 = enter, 1 = exit, 2 = life display
        System.out.println(ParkingStatus);
        System.out.println ("========================");


        for (int i = 0; i < 5000; i++){}
        }
    }

    public String initialize(String num1, String num2) {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();


        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); 
            for (String portName : PORT_NAMES) {
                if (currPortId.getName().equals(portName)) {
                    portId = currPortId;
                    break; 
                }
            }
        }


        if (portId == null) {
            System.out.println("Could not find COM port.");
            return null; 
        }
        String Status = "";
        try {

            serialPort = (SerialPort) portId.open(this.getClass().getName(),TIME_OUT);  
            serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            output = serialPort.getOutputStream();

            Thread.sleep(1000);
            output.write(num1.getBytes()); 
            Thread.sleep(1000);


            System.out.println("Life display of the parking lots");
            String convert = "";
            int lots;


            while (true){
                lots = input.read() - '0';
                if (lots == 9)
                    break;
                convert = Integer.toString(lots);
                Status += convert;  
            }



            serialPort.removeEventListener();
            serialPort.close();
            //serialPort.addEventListener(this);
            //serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
        return Status;
    }

    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }

    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                String inputLine=input.readLine();
                System.out.println(inputLine);
            } catch (Exception e) {
                System.err.println(e.toString());
            }
        }

    }

    ///////////////////////applet/////////////////////////////
    Thread t;
    public void init() {
        setSize(480, 600);
        System.out.println("in init");
        t = new Thread(this);
        t.start();

    }

    /*public void start() {
        System.out.println("in start");
        Thread t;
        t = new Thread(this);
        t.start();
    }*/

    public void paint(Graphics g) {
        System.out.println("in paint");
        super.paint(g);
        backGround = getImage(getCodeBase(), "park.png");
        g.drawImage( backGround, 0, 0, (int)getBounds().getWidth(), (int)getBounds().getHeight(), this);
        String first = "1111100000";

        if (first.charAt(4) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,56,63,50);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("5",370 , 90);

        if (first.charAt(3) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,108,63,50);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("4",370 , 140);

        if (first.charAt(2) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,160,63,47);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("3",370 , 190);

        if (first.charAt(1) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,377,63,50);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("2",370 , 410);

        if (first.charAt(0) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,429,63,50);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("1",370 , 460);

        if (first.charAt(8) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,440,63,47);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("9",95 , 471);


        if (first.charAt(9) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,489,63,42);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("10",88 , 520);

        if (first.charAt(7) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,244,63,49);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("8",95 , 276);

        if (first.charAt(6) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,196,63,45);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("7",95 , 225);

        if (first.charAt(5) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,144,63,49);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("6",95 , 177);
    }


}

The way that I am creating and starting this thread in another class is as following

lifeDisplay display = new lifeDisplay ();
Thread parkingLots = new Thread (display);
parkingLots.start();

I am trying to use multithreading

what is the problem?

  • 1
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. .. – Andrew Thompson Mar 22 '14 at 09:08
  • 1
    .. 3) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Mar 22 '14 at 09:08

0 Answers0