Java mobile software graphical interface API low-level GUI components

Source: Internet
Author: User

In the Advanced API programming, you cannot control what is displayed on the screen, and even the components are almost impossible to control programmatically. The system implementation body is responsible for selecting the best display mode for the device. However, some applications, such as game classes, may need more control over screen drawing. The MIDP Javax.microedition.lcdui package also provides low-level APIs for handling this type of programming.

In order to draw straight lines, text, and shapes directly on the screen, you must use the canvas class. This class provides a blank screen that MIDlet can draw on. For example, draw the string "HelloWorld" on the screen. There is an easy way to do this: Subclass the Canvas class (which is an abstract class that inherits from Displayable) and overload the Paint () method. See Code Section 1 for details.

The implementation of the paint () method uses the drawing capabilities of the Javax.microedition.lcdui.Graphics class. In method Paint (), the drawing color is set to red, and then a rectangle is drawn in red. Where the method GetWidth () and GetHeight () return the width and height of the canvas object, respectively. Next, the SetColor () method sets the drawing color to white; After that, the string "Hello world!" Draw in the upper-left corner of the screen.

Example 1: Sub-class 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 see the MyCanvas, you have to display the instantiation. Since canvas is a subclass of displayable, it can be displayed using the same setcurrent () method that is used with other screen classes. See Code Section 2 for details.

Example 2: instantiating and displaying 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 in the emulator provided by the Wireless development pack, you may get the effect shown in Figure 1. Note that in code snippet 5-1, the color is set to red and white, but since the grayscale screen is used, the color here is mapped to a different gray level of black and white. Try to adjust the display to see which device has a better color display.

Figure 1. Draw "Hello world!" on canvas

Related Article

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.