# (MIDP1.0) - Adding soft keys to class extending Canvas

## Recommended Posts

benryves    1999
I'm working on a game, Crates3D, for my cellphone. I've got a basic menu working for the main intro bit using a form (It shows a title pic and has a menu with two items: new game and about). However, when I go to the main game I'm in a class called GameScreen which extends Canvas for all it's graphical goodness. How can I populate the white space at the bottom of the screen with softkey buttons? I tried just creating a form but I get all sorts of weird and wonderful errors, and when I tried to create a form with a canvas in another class that extended MIDlet it brought up "Canvas is abstract and couldn't be instantiated". Help!

##### Share on other sites
shmoove    821
You can just add Commands to the Canvas subclass:
public class GameScreen extends Canvas implements CommandListener {  // ...  Command menu = new Command("Menu",Command.SCREEN,1);  Command quit = new Command("Quit",Command.EXIT,1);  public GameScreen(...) {    addCommand(menu);    addCommand(quit);    setCommandListener(this);    // ...  }  // ...  public void commandAction(Command c, Displayable d) {    if (c == menu) {      goToMenu();    }    else if (c == quit) {      quitGame();    }  }}

You don't have to implement the CommandListener in GameScreen either. Some people prefer to have their Command handling centralized in one class, so you could just set the CommandListener to that class instead.

shmoove

##### Share on other sites
benryves    1999
Ah, that makes a bit more sense.
The whole concept of Classes is still a bit new to me (having come through BBC BASIC, VB6, QB, z80 Assembler) and so I'm finding things a little tricky :)

##### Share on other sites
shmoove    821
Have you tried the Java Basics trails from Sun yet?
Particularly the Classes and Inheritance and the Interfaces and Packages trails.

shmoove