Java Mobile Software graphical interface API-a low-level GUI Component

Source: Internet
Author: User

In advanced API programming, you cannot control the content displayed on the screen, or even control these components in programming mode. The system implementation body is responsible for selecting the optimal display mode of the device. However, some game applications may need more control over screen rendering. The MIDP javax. microedition. lcdui package also provides low-level APIs for processing such programming.

To draw straight lines, text, and shapes on the screen, you must use the Canvas class. This class provides a blank screen on which a MIDlet can draw. For example, draw the string "HelloWorld" on the screen ". There is a simple way to implement this function: subclass the Canvas class (which is an abstract class inherited from Displayable) and reload the paint () method. For details, see section 1.

The implementation of the paint () method uses the drawing function of the javax. microedition. lcdui. Graphics class. In method paint (), set the drawing color to red and then draw a rectangle in red. The getWidth () and getHeight () Methods return the width and height of the Canvas object respectively. Next, the setColor () method sets the drawing color to white. Then, the string "Hello World! "Is drawn in the upper left corner of the screen.

Example 1: subclass Canvas

import javax.microedition.lcdui.*;
public class MyCanvas extends Canvas {
 public void paint(Graphics g) {
  g.setColor(255, 0, 0);
  g.fillRect(0, 0, getWidth(), getHeight( ));
  g.setColor(255, 255, 255);
  g.drawString("Hello World!", 0, 0, g.TOP | g.LEFT);
 }
}

Now, to view the MyCanvas, you must display it after instantiation. Since Canvas is a subclass of Displayable, you can use the same setCurrent () method as other screen classes to display it. For details, see section 2.

Example 2: instantiate and display MyCanvas

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MyMidlet extends MIDlet {
 public MyMidlet( ) {}
 public void startApp( ) {
  Canvas canvas = new MyCanvas( );
  Display display = Display.getDisplay(this);
  display.setCurrent(canvas);
 }
 public void pauseApp( ) {}
 public void destroyApp(boolean unconditional) {}
}

If you run it in the simulator provided by the Wireless Development Kit, you may get the effect shown in 1. Note: In code segment 5-1, the color is set to red and white, but since a grayscale screen is used, the color here is mapped to different gray levels of black and white. Try to adjust the display to see which device has better color display.


Figure 1. Draw "Hello World!" on the Canvas! "
  

1. Drawing

Coordinates (0, 0) represent the upper left corner of the display. The value of X coordinates increases from left to right, and the value of Y coordinates increases from top to bottom. The application should always check the size of the drawing area, which can be achieved through the methods provided by the Canvas class below:

public int getHeight( );
public int getWidth( );

The two methods return the height and width of the display area in pixels.

The plot model used is a pixel replacement method-used to replace the target pixel value with the current pixel value specified in the graphics object. A 24-bit color model is provided by 8 bits (RGB) in red, green, and blue. However, since not all devices support color, the colors required in the application will be mapped to available color values in the device. However, a good application should check whether a device supports color-this can be done using the Display class method isColor () and numColors.

The Graphics class provides setColor () and getColor () Methods to set and obtain colors. However, unlike AWT/Swing, there are no methods setBackground () and setForeground (), so you must call fillRect () explicitly (see example 5-1 ). Most other methods of the Graphics class are self-explanatory, similar to the methods in the AWT version of the class. However, we still need to take a closer look at how several of them work in the environment of j2s.

2. Double Buffering

The double buffering mechanism is often used to achieve smooth animation display. In this technique, you do not draw on the display, but draw on a copy of the display (a off-screen buffer)-it is part of the memory. When the buffer zone is drawn, you can copy the buffer content to the display. The basic principle here is that directly copying the memory content to the display is faster than using the prototype.

To achieve dual buffering, you can convert an image of the same size as the screen:

int width = getWidth( );
int height = getHeight( );
Image buffer = Image.createImage(width, height);

Then, obtain the graphic context of the buffer zone:

Graphics gc = buffer.getGraphics( );
Now you can plot in the buffer:

// animate
// ..
gc.drawRect(20, 20, 25, 30);

When you need to copy the buffer to the screen, you can reload the method paint () to draw the buffer content to the device display:

public void paint(Graphics g) {
 g.drawImage(buffer, 0, 0, 0);
}

Note that some MIDP implementations already adopt a dual-buffer mechanism, so the work here may be unnecessary. You can use the isDoubleBuffered () method of Canvas to check whether the image is implemented in double buffering mode.

3. Thread Problems

The midp gui api is thread-safe. That is, the gui api method can be called from any thread at any time. The only exception is the serviceRepaints () method of the Canvas class. It immediately calls the paint () method to force the display to be re-painted immediately. This means that if the paint () method wants to synchronize on the locked object when any application calls serviceRepaints (), the application will produce a deadlock. To avoid deadlocks, do not lock the object to be used by the paint () method when the serviceRepaints () method is used.
In addition, after all the unexecuted redraws are met, you can use the Display class method callSerially () to execute the code, as shown below:

Class TestCanvas extends Canvas implements Runnable {
Void doSomething (){
// Code segment 1
CallSerially (this );
}
Public void run (){
// Code segment 2
}
}

Here, the object's run () method is called after initialization.

4. Font

Applications cannot generate their own fonts. Instead, applications should require a font based on certain attributes (such as size, font name, and font shape, the underlying system will try to return a font that is the most similar to the required font. The Font class is used to describe various fonts and sizes. A total of three Font attributes are defined in the Font class, each of which has different values, as shown below:

Attribute Value
Face MONOSPACE, PROPORTIONAL, SYSTEM
Size SMALL, MEDIUM, LARGE
Style BOLD, ITALIC, PLAIN, UNDERLINED

For example, to specify a medium-size Font, you can use Font. SIZE_MEDIUM; Use Font. STYLE_ITALIC to specify the skewed Font. The font attribute values can be combined with the OR (|) operator, and other attribute values cannot be combined. For example, the following attribute value specifies a regular, underlined Font:

STYLE_PLAIN | STYLE_UNDERLINED
The following is an invalid combination:

SIZE_SMALL | SIZE_MEDIUM
The following is also invalid:

FACE_SYSTEM | FACE_MONOSPACE
Each font in the system is actually implemented separately. Therefore, to obtain the object that describes the font, you can use the getFont () method. This method has three parameters that correspond to the literal of the font, size and font. For example, the following code obtains a Font object with the specified literal, size, and Font attributes:

Font font = Font.getFont(FACE_SYSTEM,STYLE_PLAIN, SIZE_MEDIUM);
If there is no matching font, the system will try to provide the most similar match-always a valid font object.

Once you get a Font, you can use the Font class to retrieve the Font information. For example, you can use the getFace (), getSize (), and getStyle () Methods to retrieve the literal, size, and font information of the font.

Let's look at another example: In Example 3, the Code subclass is the Canvas class. Here, the drawing color is set to white, and a rectangle is drawn with the color, and the drawing color is set to black. The remaining part of the Code draws the system font on the device screen, as shown in figure 2.


Figure 2. Draw the system font on the device display
  

Example 3: Use a font

import javax.microedition.lcdui.*;
public class FontCanvas extends Canvas {
 public void paint(Graphics g) {
  g.setColor(0xffffff);
  g.fillRect(0, 0, getWidth(), getHeight( ));
  g.setColor(0x000000);
  g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,Font.SIZE_LARGE));
  g.drawString("System Font", 0, 0, g.LEFT | g.TOP);
  g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,Font.SIZE_MEDIUM));
  g.drawString("Medium Size", 0, 15, g.LEFT | g.TOP);
  g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,Font.SIZE_MEDIUM));
  g.drawString("Bold Style", 0, 30, g.LEFT | g.TOP);
  g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC,Font.SIZE_MEDIUM));
  g.drawString("Italic Style", 0, 45, g.LEFT | g.TOP);
  g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM));
  g.drawString("Underlined Sty

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.