It's free to join Gamasutra!|Have a question? Want to know who runs this site? Here you go.|Targeting the game development market with your product or service? Get info on advertising here.||For altering your contact information or changing email subscription preferences.
Registered members can log in here.Back to the home page.

Search articles, jobs, buyers guide, and more.


Resource Guide

Code Listing: Number Picker

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.util.*;
 
public class NumberPick extends MIDlet implements CommandListener 
{
    // This hostname may be a server or the IP of another phone
    static final String hostname = "myserver.com";
    static final int sendport         = 9000;
    static final int receiveport      = 9001;
 
    static boolean gotResult = false;
 
    private Display myDisplay;
 
    Receive receiveThread = new Receive();
 
    private Alert responseAlert;
 
    private Form searchwait;
    private Form pickscreen;
 
    TextField pickedNumber;
    private Command PICK = new Command("Pick One", Command.OK, 1);
    private Command OK = new Command("Try Again", Command.OK, 1);
 
    public NumberPick()
    {
    }
 
    public void startApp()
    {
       // Set up the Pick A Number screen
       pickscreen = new Form("Pick A Number");
       pickedNumber = new TextField("Between 0 and 9:","",1, TextField.NUMERIC);
       pickscreen.append(pickedNumber);
       pickscreen.addCommand(PICK);
       pickscreen.setCommandListener(this);
 
       // Set up a Searching, Please Wait screen
       searchwait = new Form("Searching...");
       searchwait.append(new StringItem("Wait","Please wait..."));
       searchwait.addCommand(OK);
       searchwait.setCommandListener(this);
 
       // Set up a Response Alert screen
       responseAlert = new Alert("And Your Guess Is...");
       responseAlert.setTimeout(Alert.FOREVER);
 
       myDisplay = Display.getDisplay(this);
       myDisplay.setCurrent(pickscreen);
 
        // Start the Listening thread
        receiveThread.start();
    }
 
    public void pauseApp() 
    {
    }
 
    public void destroyApp(boolean unconditional)
    {
    }
 
   
    public void commandAction(Command c, Displayable d)
    {
        if (d == pickscreen && c == PICK)
        {
            String request = pickedNumber.getString();
           if ( !request.equals("") )
            {
              myDisplay.setCurrent(searchwait);
              sendPick(request);
           }
        }
    }
 
    void sendPick(String thepick)
    {
        // Now send the pick...
        byte[] message = new byte[1];
        System.arraycopy(thepick.getBytes(), 0, message, 0,1);
 
        DatagramConnection dc = null;
 
        String destAddr = "datagram://" + hostname + ":" + sendport;
 
        try {
            dc = (DatagramConnection)Connector.open(destAddr);
 
           // Create a datagram socket and send
            Datagram dgram= dc.newDatagram(message,1,destAddr);
            dc.send(dgram);
            dc.close();
        }
        catch (Exception e)
        {
            System.out.println("Connecting Exception: " + e.getMessage());
            if (dc != null) {
                try {
                    dc.close();
                }
                catch (Exception f)
                {
                   System.out.println("Exception Closing: " + f.getMessage());
                }
            }
        }
    }
 
    class Receive extends Thread
    {
        public void run()
        {
            doReceive();
        }
 
        void doReceive()
        {
            DatagramConnection dc = null;
            Datagram dgram;
 
            try
            {
                dc = (DatagramConnection)Connector.open("datagram://:"+receiveport);
 
                String reply;
 
                while (true)
                {
                    dgram = dc.newDatagram(dc.getMaximumLength());
                    dc.receive(dgram);
                    reply = new String(dgram.getData(), 0,dgram.getLength());
                    if (reply.equals("Y"))
                    {
                       responseAlert.setString("100% right!");
                       myDisplay.setCurrent(responseAlert,pickscreen);
                    }
                    else
                    {
                       myDisplay.setCurrent(responseAlert,pickscreen);
                       responseAlert.setString("Wrong as can be!");
                    }
                    try
                    {
                       Thread.sleep(500);
                    }
                    catch (Exception e)
                    { }
                }
 
            }
            catch (Exception e)
            {
                System.out.println("Exception: " + e.getMessage());
 
                if (dc != null) 
                {
                    try
                    {
                        dc.close();
 
                    }
                    catch (Exception f)
                    {
                        System.out.println("Exception: " + f.getMessage());
                    }
                }
            }
        }
     }
}

________________________________________________________

[Back To] Number Picker


join | contact us | advertise | write | my profile
news | features | companies | jobs | resumes | education | product guide | projects | store



Copyright © 2003 CMP Media LLC

privacy policy
| terms of service