📜 ⬆️ ⬇️

Java ME: Midlet Structure

In this article we will examine in detail one of the options for constructing the structure of the midlet. This material will be useful for beginners.

Imagine the following situation.


We have 3 screens:

  1. Splash screen (which will be displayed first);
  2. Menu screen;
  3. Screen "Game".

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

So, having this information already, for our screens you can pick up a general abstract class, and its name is “Screen”.

What is common between all screens?



Abstract class for all screens


public abstract class Screen extends GameCanvas { protected final int screenWidth; //  protected final int screenHeight; //  protected final Graphics graphics; //   public Screen() { super(false); setFullScreenMode(true); screenWidth = getWidth(); screenHeight = getHeight(); graphics = getGraphics(); } //,  ,      public abstract void start(); } 

We inherited our common interface from GameCanvas and made some changes to it:
Added own fields with the protected modifier and defined a general event.
Only descendant classes can see protected fields.
abstract in the definition of a method means that the method is abstract. That is, we do not write the body of the method, but we know for sure that the descendants implement it (unless they are abstract). If a method contains at least one abstract method, the class must be marked abstract, otherwise the compiler will generate an error.

Next, creating our screens, we will inherit them from the Screen, and not from GameCanvas. We have the following hierarchy:

Object -> GameCanvas -> Screen -> MyScreen

In Java, all classes are implicitly inherited from Object. Screen inherited from GameCanvas to remain a “canvas” for drawing and handling keystrokes (although this is not all). We will inherit all our screens from Screen in order to define a common interface. It would not be entirely correct to write the same thing in each class, especially since we will need a common interface for another construct, which you will learn about later.

Main class


 public class MIDletStructure extends MIDlet { public static MIDletStructure midlet; //   private final Display display; public MIDletStructure() { midlet = this; display = Display.getDisplay(this); } //   public void setCanvas(Screen canvas) { display.setCurrent(canvas); canvas.start(); } public void startApp() {} public void pauseApp() {} public void destroyApp(boolean unconditional) {} } 

In the main class, we define the midlet and display fields. The first one refers to itself, and the second is a link to Display. Link midlet is needed in order to have a connection with the main class. For example, to turn off an application or install another screen, which we will do later.

Consider the setCanvas method in more detail: the setCanvas method takes as a parameter a link to any class inherited from Screen, then, since Screen inherits from GameCanvas (which in turn inherits from Displayable), it sets it to the display and starts it using the start method. The whole snag in Screen is that we can pass into the method an instance of any class inherited from Screen, whether it be a menu screen or a game, and thanks to the common interface we know for sure that any instance has an implemented start method. All this is due to the late binding in Java, if not for it, then for each class descendant of Screen would have to write a separate method.

First screen


 public class Splash extends Screen { //,  ,      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 see that in the start method we already use classes that were initialized in our ancestor class.

Now we write a similar class for the game screen:

 public class Game extends Screen { //,  ,      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:

 public class MIDletStructure extends MIDlet { public static MIDletStructure midlet; //   private final Display display; public final Screen splash; // “” public final Screen menu; //  public final Screen game; //  public MIDletStructure() { midlet = this; display = Display.getDisplay(this); splash = new Splash(); menu = new Menu(); game = new Game(); } //   public void setCanvas(Screen canvas) { display.setCurrent(canvas); canvas.start(); } public void startApp() { //   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 in which it is located. Why do you need it? public means that the link can be accessed anywhere in the program, static means that the link can be accessed through the class (Main.midlet), and since all screen fields and the setCanvas method are also public, therefore, the screen can be replaced anywhere programs:

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

Conclusion


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

Full source

Source: https://habr.com/ru/post/238567/


All Articles