views:

748

answers:

3

Hey guys thanks for reading!

I am currently trying to learn J2ME and build a connect four game (some of you might know this as 'four in a row'). I've More or less got all of the aspects of my game working, apart from one thing that is driving me mad! This is of course getting the text from the user!

For the two player mode of the game I want to be able to allow each player to enter their name. I am struggling to find a working example of text input that doesn't use the main Midlet.

For example the examples on java2x.com just use a single midlet (no classes or canvases or anything).

As it stands my application's main midlet start method simply opens a main menu class:

    package midlet;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import view.*;

public class Main extends MIDlet {
    public void startApp() { 
        MainMenu mm = new MainMenu();
        showScreen(mm);
    }

    public static void showScreen(Displayable screen) {
        Display.getDisplay(instance).setCurrent(screen);
    }

    public void pauseApp() {
    }

    public static void quitApp() {
        instance.notifyDestroyed();
    }

    public void destroyApp(boolean unconditional) {
    }
}

The main menu class is as follows:

 package view;

    import javax.microedition.lcdui.*;
    import lang.*;
    import model.*;
    import midlet.Main;

    public class MainMenu extends List implements CommandListener {

        private Command ok = new Command(StringDefs.currDefs.getString("TEXT_OK"), Command.OK, 1);

        public MainMenu() {
            super(StringDefs.currDefs.getString("TEXT_TITLE"), List.IMPLICIT);
            // we we add in the menu items
            append(StringDefs.currDefs.getString("TEXT_PLAY1"), null);
            append(StringDefs.currDefs.getString("TEXT_PLAY2"), null);
            append(StringDefs.currDefs.getString("TEXT_HIGHSCORETABLE"), null);
            append(StringDefs.currDefs.getString("TEXT_HELP"), null);
            append(StringDefs.currDefs.getString("TEXT_QUIT"), null);
            this.addCommand(ok);
            this.setCommandListener(this);
        }

        public void commandAction(Command c, Displayable d) {
            if (c == ok) {
                int selectedItem = this.getSelectedIndex();
                if (selectedItem != -1) {
                    switch (selectedItem) {
                        case 0:
                            GameBoard gameBoard = new model.GameBoard();
                            GameCanvasOnePlayer board = new GameCanvasOnePlayer(gameBoard);
                            Main.showScreen(board);
                            break;
                        case 1:
                            GameBoard gameBoardTwo = new model.GameBoard();
                            GameCanvasTwoPlayer GameCanvasTwoPlayer = new GameCanvasTwoPlayer(gameBoardTwo);
                            Main.showScreen(GameCanvasTwoPlayer);
                            break;
                        case 2:
                            HighScores hc = new HighScores();
                            midlet.Main.showScreen(hc);
                            break;
                        case 3:
                            Help he = new Help();
                            midlet.Main.showScreen(he);
                            break;
                        case 4:
                            QuitConfirmation qc = new QuitConfirmation();
                            midlet.Main.showScreen(qc);
                            break 
                    }
                }
            }
        }
    }

When a two player game is selected (case 1 in the above switch) from this menu I would like two text boxes to appear so that I can get both player names and store them.

What would be the best way of going about this? is this even possible with canvases? And do you know where I can find a relevant example or at least something which may help?

Thanks in advance! Exile.......

+1  A: 

That's a really sticky situation. Basically you will need to use J2ME's input text widget (which by the way looks horrible). If you don't, you'll end up having to implement all the logic behind the different types of phone keyboards and you won't have access to the dictionary... Your canvas will basically only be capturing keystrokes, not text input...

Sorry.

ruibm
hmm thats not great, but I suppose its good to know! You've nothing to be sorry for I'm extremely thankful for any advice!so is all j2me text input done through midlets then?
Exile
Yes, pretty much. If you were doing BlackBerry (which is an extension of J2ME's APIs) you might have been able to get away with a custom control but with J2ME you need to open up an input text screen and then retrieve the typed in text.
ruibm
Re: "J2ME's input text widget (which by the way looks horrible)". It will look different on every device, according to how the device's own UI looks. Whether it looks horrible on a particular device is in the eye of the beholder; it may not fit in with your own app's UI, but it will be what the user is used to in the phone UI.
funkybro
+2  A: 

You can either: 1. Make the user enter his input in an ugly Textbox (which takes the whole screen) 2. Use the textbox control I've written from scratch a long time ago which is available here and looks something like this (3 Textfields shown):

alt text

You can do whatever you want with the code i don't mind.

Orr Matarasso
Thanks for that! I've have a quick look through your solution and it should work! but like ruibm said I would need to end up having to implement all the logic behind the different types of phone keyboards.If I cant get around this problem I might have to use it! Thanks!
Exile
If it's for a game maybe you can take input the same way this guy did http://devlinslab.blogspot.com/2007/11/handling-text-input-for-character-or.html
Orr Matarasso
+1  A: 

I've got a solution! well sort of.

I can create a form without using the main midlet:

alt text

The following main class is part of a source package called midlet (much like in my project):

package midlet;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import view.*;

public class Main extends MIDlet {
    private static UsernameForm unameForm=new UsernameForm();
    private static MIDlet instance;
    public void startApp() {
        instance=this;
        showScreen(unameForm); // show user name form

    }

    public static String getUsername1() {
        return(unameForm.getUsername1());
    }

    public static String getUsername2() {
        return(unameForm.getUsername2());
    }

    public void pauseApp() {
    }

    public static void showScreen(Displayable d) {
        Display.getDisplay(instance).setCurrent(d); // show next screen

    }

    public void destroyApp(boolean unconditional) {
    }
}

The next bit of code is the username form class that is part of a source package called view:

package view;

import javax.microedition.lcdui.*;

public class UsernameForm extends Form implements CommandListener {
    private String username1="";
    private String username2="";
    private TextField tfUsername1=new javax.microedition.lcdui.TextField("User 1","User1",40,TextField.ANY);
    private TextField tfUsername2=new javax.microedition.lcdui.TextField("User 2","User2",40,TextField.ANY);
    private Command cmdOK=new Command("OK",Command.OK,1);
    public UsernameForm() {
        super("User details");
        append(tfUsername1);
        append(tfUsername2);
        addCommand(cmdOK);
        setCommandListener(this);
    }

public void commandAction(Command cmd,Displayable d) {
    if (cmd==cmdOK) {
        this.setUsername1(tfUsername1.getString());
        this.setUsername2(tfUsername2.getString());
        // TO DO, GO TO NEXT SCREEN
    }
}

/**
 * @return the username1
 */
public String getUsername1() {
    return username1;
}

/**
 * @param username1 the username1 to set
 */
public void setUsername1(String username1) {
    this.username1 = username1;
}

/**
 * @return the username2
 */
public String getUsername2() {
    return username2;
}

/**
 * @param username2 the username2 to set
 */
public void setUsername2(String username2) {
    this.username2 = username2;
}
}

So It Looks like theres no easy way of doing it using canvases, I think I am better of using 'ugly forms' instead! as they should work what ever the device!

Exile