Click here to Skip to main content
14,870,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package pak;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;
 
public class MyMidlet extends MIDlet implements CommandListener{
 
public String ip;
public String port;
private Command exitCommand;
private Command connectCommand;
public Form form;
private Displayable current;
private Display display;
    MyCanvas myCanvas = new MyCanvas(this);
    public void startApp() {
    display=Display.getDisplay(this);
    if (current!=null)
      display.setCurrent(current);
    else{
      display.setCurrent(getForm());
      current=form;
    }
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
    }
 
    public Display getDisplay () {
        return Display.getDisplay(this);
    }
 
    public Form getForm(){
         form = new Form("My Form");
         form.addCommand(getExitCommand());
         form.addCommand(getConnectCommand());
         form.setCommandListener(this);
         return form;
    }
   public Command getExitCommand() {
        if (exitCommand == null) {
            exitCommand = new Command("Exit", Command.EXIT, 0);
        }
        return exitCommand;
    }
   public Command getConnectCommand() {
        if (connectCommand == null) {
            connectCommand = new Command("Connect", Command.ITEM, 0);
        }
        return connectCommand;
    }
   public void commandAction(Command c, Displayable d) {
        if (d == form) {
            if (c == exitCommand) {
               exitMIDlet() ;
            }
            else{
                if(c == connectCommand){
                   myCanvas.addCommand(getExitCommand());
                   myCanvas.setCommandListener(this);
         myCanvas.start();
        getDisplay().setCurrent(myCanvas);
 
                }
 }
           }
    }
   public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }
   public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        Display displa = getDisplay();
        if (alert == null) {
            displa.setCurrent(nextDisplayable);
        } else {
            displa.setCurrent(alert, nextDisplayable);
        }
    }
 
class MyCanvas extends GameCanvas implements Runnable {
 
    MyMidlet midlet;
    private boolean isRunning = true;
        private Graphics g;
    public MyCanvas(MyMidlet midlet){
        super(true);
        this.midlet = midlet;
    }
        public void start(){
 
        Thread runner = new Thread(this);
        runner.start();
    }
 
public void run(){
 
        g = getGraphics();
        flushGraphics();
 
        while (isRunning) {
            try{
        Thread.sleep(30);
            }
        catch(Exception ex){}
}//while is running
    }
}
}

When I switch to GameCanvas, exit command doesn't work.
Please, help.
Posted
Updated 12-Oct-10 11:57am
Comments
William Winner 12-Oct-10 17:57pm
   
First of all, I would recommend putting a description of your question along with the actual question at the very beginning. Then, include only the code that is actually giving you problems and not just all of your code.

For example, when do you "switch to GameCanvas"? and what "exit command" doesn't work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900