Java mobile game programming-MIDP Graphic Design

Source: Internet
Author: User

I. MIDlet graphics

  1. MIDlet graphics Overview

Mobile Information device profile (MIDP) defines an application programming interface (API) for running the MIDlet application in the MIDP container. This API is built on the Application Programming Interface of the limited connection device configuration (cldc. The Application Programming Interface Class of the MIDP user interface is not designed based on the Java Abstract Window Toolkit (AWT. They are designed for small mobile information devices such as mobile phones and pagers, which feature only small screens and keyboards. When writing a MIDP graphic application, a programmer may only use the MIDP or cldc application programming interface.

The center abstraction of MIDP is screen, which means that the user interface design of MIDP is screen-based ). That is to say, the screen class encapsulates the device-specific graphics and user interaction. All user interface components are on the screen and only one screen is displayed at a time, you can only browse or use entries on this screen. The screen handles all user interface events. Only advanced events are transmitted to the application. This screen-oriented method is mainly used because there are too many display screens and keyboards for mobile devices. Almost every manufacturer has a different screen-oriented screen. Figure 1 shows some examples of screen-based midp gui.


Figure 1: screen-based MIDP graphic user interface

The MIDP application programming interface provides both advanced and low-level user interfaces. Advanced User Interface classes (such as form, list, Textbox, textfield, alert, and ticker) can be adapted to devices: images, text, text input fields, and single-choice buttons are supported. The canvas class allows developers to draw arbitrary images as needed. The MIDlet can run on mobile phones of different sizes of colors, gray levels, or black and white screens. The advanced user interface class is the abstraction of common user interface elements. It is used to improve the portability of the MIDlet across different devices, and can use the appearance of local devices. The low-level application programming interface can control the display content more directly, but the MIDlet designer should ensure its portability on different devices (display size, keyboard, color, etc. The above example uses both advanced application programming interfaces and low-level application programming interfaces.

All MIDP graphical user interface classes are part of the javax. microedition. lcdui package.

2. MIDlet Screen

MIDP has two main screen types:

A advanced Screen

It includes simple advanced screen classes, such as list and textbox. You cannot add additional graphical user interface components to this type of screen. The screen used by the jiugongge MIDlet sample program is inherited from the List class named choosepiecescreen, which is used by gamers to select pawns at the beginning of the game.

The form screen class is similar to the list class, but it allows additional graphic elements, such as images, read-only text fields, editable text fields, editable data fields, rulers, and rule groups. Form entries can be added or deleted at will. The Form class is not used in the JIU Gong Ge routine.

B Low-Level Screen

Canvas screens (similar to graphics and image) can be used to compile user interfaces based on low-level application programming interfaces. These classes give the MIDlet programmer a great deal of painting flexibility. Programmers can draw various types of graphic elements, such as line, arc, rectangle, rounded rectangle, circle, text (different colors, fonts, sizes), Bitmap Editing, and so on. Most of the Games use the canvas-based screen-based primary graphical user interface elements.

A MIDlet User Interface usually contains one or more screens. Because only one screen can be displayed at a time, it is very important for the MIDlet to have a well-designed structure, which makes it easier to handle switching between screens.

The following code snippet illustrates how to switch the screen in a MIDlet, based on the screen class and the corresponding MIDlet callback.

Code Segment 1:

Class mymidlet extends MIDlet
{
Private firstscreen;
Private secondscreen;
Public mymidlet ()
{
...
}
Public void Startapp ()
{
Displayable current = display. getdisplay (this). getcurrent ();
If (current = NULL)
{
Firstscreen = new firstscreen (this ,...);
Display. getdisplay (this). setcurrent (firstscreen );
// Display the first user interface screen of the application
}
Else
{
Display. getdisplay (this). setcurrent (current );
}
}
// Switch the firstscreen callback to the next Screen
Public void firstscreendone ()
{
...
Secondscreen = new secondscreen (this ,...);
Display. getdisplay (this). setcurrent (secondscreen );
}
// Secondscreen calls back to terminate the application
Public void secondscreenquit ()
{
...
Destroyapp (false );
Yydestroyed ();
}
...
}

This MIDlet uses two screen classes (firstscreen and secondscreen) as the user interface. When you start executing the MIDlet, it sets the current display screen to firstscreen. When switching from firstscreen to secondscreen, firstscreen calls the parent MIDlet method firstscreendone (see the following code ). Create the firstscreendone method and set secondscreen to the current screen.

Code snippet 2: firstscreen example with MIDlet callback

Class firstscreen extends form implements commandlistener {
Private mymidlet MIDlet;
Public firstscreen (mymidlet MIDlet)
{
This. MIDlet = MIDlet;
...
}
Public void commandaction (command C)
{
If (C = Hangzhou quit)
{
Parent. firstscreendone ();
}
...
}
...
}

  3. MIDP User Interface Application Programming Interface

It is the responsibility of the MIDP device to ensure the portability and applicability of user interface objects based on advanced application programming interfaces.

On the other hand, low-level classes such as canvas and graphics provide programmers with more free space to control the visual presentation of their user interfaces and listen to low-level Keyboard Events. Programmers are also responsible for ensuring the portability of applications on mobile devices with different features (such as Display sizes, colors, black and white, and different keyboard types. For example, you may need to use the getwidth () and getheight () Methods to adjust the appearance of the user interface to adapt it to the available canvas size of one or more devices.

The following nine-phase Lattice MIDlet routine will be introduced:

Simple Application advanced application programming interface;

Use low-level application programming interfaces to draw lines, arcs, strings, images, and other images;

How to port MIDlet between mobile devices with different Display sizes

Ing between keyboard code and game actions

This chapter provides an overview of the MIDP graphic user interface design. For more information, see
Http://java.sun.com/products/midp.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.