Java ME: the structure of the MIDlet

In this article we analyze in detail one way of constructing the structure of a MIDlet. This material will be useful for beginners.

let us Imagine the following situation


We have 3 screen:

    the
  1. Screen "Splash" (which will appear first);
  2. the
  3. Display "Menu";
  4. the
  5. the Screen.

For example, I'm not going to write 3D games and menus with animation of space battles, because it will only distract. Each of the screens will do the following:

the
    the
  • Screen "Splash" — displays for 10 seconds the text "SPLASH";
  • the
  • Screen "Menu" — displays for 10 seconds the text "MENU";
  • on the Screen the Game displays the text "GAME".


So, having had this information for our screens, you can pick up a common abstract class, and his name is "Screen".

What is common between all the screens?

the
    the
  • each screen has its maximum dimensions;
  • the
  • each screen has its own subject to paint;
  • the
  • as we draw on the screen only after its installation on the display, it is possible to determine the overall event:
  • the
  • start is called after setting the screen on the Display.


Abstract class for all screens


the
public abstract class Screen extends GameCanvas {

protected final int screenWidth; //screen width
protected final int screenHeight; //screen height
protected final Graphics graphics; //object for drawing

public Screen() {

super(false);
setFullScreenMode(true);
screenWidth = getWidth();
screenHeight = getHeight();
graphics = getGraphics();
}

//method invoked first, after installing the screen on the display
public abstract void start();
}

We inherited our public interface from GameCanvas and made some changes:
Added a private field with a protected modifier, and determined the total event.
protected fields can only see the descendant classes.
abstract in the method definition means that a method is abstract. That is, we do not write the method body, but I do know that his descendants implement (unless they are abstract). If the method contains at least one abstract method, the class must be marked abstract, otherwise the compiler will issue an error.

Further, in creating our screens, we will inherit from Screen, not GameCanvas. We have the following hierarchy:

Object -> GameCanvas -> Screen -> MyScreen

In Java all classes implicitly inherit from Object. Screen inherited from GameCanvas to be a "canvas" for drawing and handling of the taps (although not all). All our screens mu will inherit from the Screen in order to define a common interface. Would be a shame to write the same thing in every class, especially as a General interface we will need for one design, which you will learn later.

Main class


the
public class MIDletStructure extends MIDlet {

public static MIDletStructure midlet; //points to itself
private final Display Display;

public MIDletStructure() {

midlet = this;
display = Display.getDisplay(this);
}

//set the new screen
public void setCanvas(canvas Screen) {

display.setCurrent(canvas);
canvas.start();
}

public void startApp() {}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}

In the main class of the midlet-defined fields and display. The first refers to itself, and the second is a reference to a Display. Reference midlet is needed in order to have a connection with the main class. For example, to disable the application or set another screen, we are going to do later.

Consider the method setCanvas more: setCanvas method takes in parameter a reference to any class derived from Screen, then Screen inherited from GameCanvas (which in turn is inherited from Displayable), then sets it on the display and with the start method starts it. All hitch in the Screen — we can pass an instance of any class derived from the Screen, be it menu screen or games, and thanks to the General interface, we know that every instance has implemented the start method. All of this is done with late binding in Java, if it does not, then for each class, a descendant of the Screen would have to write a separate method.

home screen


the
public class Splash extends Screen {

//method invoked first, after installing the screen on the display
public void start() {

graphics.setColor(0xFFFFFF);
graphics.fillRect(0, 0, screenWidth, screenHeight);
graphics.setColor(0x000000);
graphics.drawString("SPLASH", 0, 0, 0);
flushGraphics();
try {

Thread.sleep(10000);
} catch (InterruptedException interruptedException) {

System.out.println(interruptedException.getMessage());
MIDletStructure.midlet.notifyDestroyed();
}
}
}

It is easy to notice that in the start method we already use classes that are initialized in our class-ancestor.

Now write a similar class for the game screen:

the
public class Game extends Screen {

//method invoked first, after installing the screen on the display
public void start() {

graphics.setColor(0xFFFFFF);
graphics.fillRect(0, 0, screenWidth, screenHeight);
graphics.setColor(0x000000);
graphics.drawString("GAME", 0, 0, 0);
flushGraphics();
while (true) {

try {

Thread.sleep(20);
} catch (InterruptedException interruptedException) {

System.out.println(interruptedException.getMessage());
MIDletStructure.midlet.notifyDestroyed();
}
}
}
}

And add links to our screens in the main class:

the
public class MIDletStructure extends MIDlet {

public static MIDletStructure midlet; //points to itself
private final Display Display;
public final splash Screen; //screen “splash”
public final Screen menu; //menu screen
public final Screen game; //game screen

public MIDletStructure() {

midlet = this;
display = Display.getDisplay(this);
splash = new Splash();
menu = new Menu();
game = new Game();
}

//set the new screen
public void setCanvas(canvas Screen) {

display.setCurrent(canvas);
canvas.start();
}

public void startApp() {

//set the first screen
setCanvas(splash);
}

public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}

Pay attention to the link midlet: it is statistical, public and indicates the instance of the class. Why is it necessary? public indicates that the link can go anywhere on the program, static means that the link can be accessed through the class (Main.midlet), as well as all the fields in the screens and method setCanvas are also public, therefore, the screen can be replaced anywhere in the program:

the Main.midlet.setCanvas(Main.midlet.game);

Opinion


After the first reading of a beginner, you probably will not understand a single word, but a more thoughtful reading, you notice the simplicity of the algorithm and its logical solution, using the technology of the PLO. In the article I tried to describe in detail all that is necessary, however if you have any questions or inaccuracies can see the full source below:

Full source
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

March Habrameeting in Kiev

PostgreSQL load testing using JMeter, Yandex.Tank and Overload

Monitoring PostgreSQL with Zabbix