0 Replies - 3906 Views - Last Post: 26 May 2010 - 03:16 PM

#1 eddie_birdcage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-May 10

J2ME Chat Application

Posted 26 May 2010 - 03:16 PM

I have here a perfectly (i think) working chat application:
(AFAIK it works when emulated twice on a single PC or with 2 PCs in a network)

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;


public class ChaatAppMIDllet extends MIDlet implements Runnable, CommandListener {
    private int port = 8888;
    private static int NumberOfLines = 8;
    private static int MaxLine = 32;

    private Display playdis;
    private Form mForm = null;
    private StringBuffer chatTxt[] = new StringBuffer[NumberOfLines];
    private int chatPosi = 0;
    private StringBuffer logTxt = new StringBuffer(NumberOfLines*MaxLine);
    private String hostName = "";
    private String url = "datagram://:" + port;
    private boolean isServer = true;

    DatagramConnection datcon = null;

    private Command exitC = new Command("Quit", Command.EXIT, 1);
    private Command newC = new Command("New", Command.EXIT, 1);
    private Command sendC = new Command("Send", Command.OK, 1);
    private Command cancelC = new Command("Cancel", Command.EXIT, 1);
    private Command connectC = new Command("Connect", Command.OK, 1);
    private StringItem cLog = new StringItem(null, "");
    private TextField cLine = new TextField("Send:", "",  32, TextField.ANY);
    private TextBox remTxtBox = new TextBox("Remote Hostname or IP", "", 64, TextField.ANY);

    public ChaatAppMIDllet(){
        for (int i = 0; i < NumberOfLines; i++){
            chatTxt[i] = new StringBuffer(MaxLine);
        }
    }

    public void startApp() {
         if (playdis == null){
            playdis = Display.getDisplay(this);
            mForm = new MainForm();
            playdis.setCurrent(mForm);
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    class MainForm extends Form {
        MainForm(){
            super("Chat MIDLET");

            addCommand(exitC);
            addCommand(newC);

            remTxtBox.addCommand(connectC);
            remTxtBox.addCommand(cancelC);
            setCommandListener(ChaatAppMIDllet.this);
            remTxtBox.setCommandListener(ChaatAppMIDllet.this);
        }
    }

    public void commandAction(Command c, Displayable d){
        if (c == exitC){
            notifyDestroyed();
        }
        if (c == newC){
            playdis.setCurrent(remTxtBox);
        }
        if (c == sendC){
            try {
                String line = cLine.getString();
                sendMessage(line);
                logAppend("> " + line);
                cLine.setString("");
            } catch (Exception e){}
        }
        if (c == cancelC){
            playdis.setCurrent(mForm);
        }
        if (c == connectC){
            try {
                hostName = remTxtBox.getString();
                url = "datagram://" + hostName + ":" + port;
                isServer = hostName.length() == 0;

                mForm.removeCommand(newC);
                mForm.addCommand(sendC);

                mForm.append(cLine);
                mForm.append(cLog);

                playdis.setCurrent(mForm);

                this.start();
            } catch (Exception e){}
        }
    }

    void logAppend(String line){
        chatTxt[chatPosi].setLength(0);
        chatTxt[chatPosi].append(line).append("\n");
        chatPosi = (chatPosi+1)%NumberOfLines;
        logTxt.setLength(0);
        for (int i=0; i<NumberOfLines; i++){
            logTxt.append(chatTxt[(chatPosi+i)%NumberOfLines]);
        }
        cLog.setText(logTxt.toString());
    }

    private void sendMessage(String line){
        try {
            byte[] bytes = line.getBytes();
            Datagram dgram = null;

            dgram = datcon.newDatagram(bytes, bytes.length, url);

            datcon.send(dgram);
        } catch (Exception ioe) {}
    }

    public void run() {
        try {
            logAppend("#Connecting to: " +  url);
            datcon = (DatagramConnection) Connector.open(url);
            logAppend("#Connected to server");

            while (true) {
                Datagram dgram = datcon.newDatagram(128);
                datcon.receive(dgram);
                if (isServer){
                    url = dgram.getAddress();
                }
                if (dgram.getLength() > 0){
                    logAppend("< " +  new String(dgram.getData(), 0, dgram.getLength()));
                }
            }
        }
        catch (IOException ioe) {}
        catch (Exception e) {}
    }

    public void stop() {
    }

    public void start() {
        Thread t = new Thread(this);
        t.start();
    }
}



The assignment is to "improve" this application.

Since I am completely preoccupied with another project (j2me also), I have no idea what to do with this one.

The only thing I thought that I can do with this one is to import the whole thing into a canvas (or even a game canvas) just to make it barely presentable, but with no forms available in canvas, is it even possible?

I'm open to other ideas, but at this moment I really need someone to steer me into a specific direction. I really don't need code (okay, maybe a little, just to get me started) but honestly I don't know where to begin.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1