Question

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?

Was it helpful?

Solution

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

OTHER TIPS

I've got a solution! well sort of.

I can create a form without using the main midlet:

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 there's no easy way of doing it using canvases, I think I am better of using 'ugly forms' instead as they should work whatever the device.

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.

Here you need to, implement custom Items, all you need to do is to extend the part of the canvas where to want the user/player to enter his/her name to the CustomItems, and implement the customItems predefined abstract methods, and write method for Key Strokes and that's available in the nokia forum. They have explained it pretty good. Check out the Nokia forum.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top