Using NetBeans for J2ME Development (III): Low-level graphical user interface canvas

Source: Internet
Author: User
Tags event listener netbeans

In this article, I'll introduce you to the low-level graphical user interface in the graphical user interface. The so-called low-level graphical user interface, refers to the kind of control we can paint on the above, it is the textbox,list and so on these user controls just the concept, because the shape of these user controls are in advance, without our programmers to worry about, so called advanced graphical interface. Low-level graphical user interface what needs our own painting, so more complex, of course, more flexible, only unexpected, no painting, so we introduce it first.

In the J2ME development, the low-level graphical user interface is implemented by the Javax.microedition.lcdui.Canvas class, and as long as we inherit this class and implement the paint method of this class, we can draw as we like. Of course, before painting, we need to know how much paint space on our mobile screen, which can be achieved by invoking the getwidth and GetHeight methods of the canvas class. Here is a short program that shows you how to get the canvas size and, at the same time, a small frame. First of all, it is of course to create our own canvas, the code is as follows: Package com.xkland.j2me;

Import javax.microedition.lcdui.Canvas
Import javax.microedition.lcdui.Graphics
/** *//**
*
* @author Beach Foam
*/
public class MyCanvas extends Canvas {
/** *//** creates a new instance of MyCanvas */
Public MyCanvas () {
}
public void Clearbackground (Graphics g) {
int color = G.getcolor (); br> G.setcolor (0XFFFFFF);
G.fillrect (0,0,getwidth (), getheight ());
G.setcolor (color);     
}
public void Paint (Graphics g) {
//clear Background
Clearbackground (g);
Displays the size of the area available for drawing
g.drawstring (width:), 10,10,graphics.left| Graphics.top);
g.DrawString (string.valueof (GetWidth ()), 50,10,graphics.left| Graphics.top);
g.drawstring (height:), 10,25,graphics.left| Graphics.top);
g.DrawString (string.valueof (GetHeight ()), 50,25,graphics.left| Graphics.top);
}
}

The second class is of course our midlet, because it is the main program. When the program starts, call Display.setcurrent to set the canvas as the primary interface and set the event listener. The code is relatively simple, as follows: Packagecom.xkland.j2me;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/** *//**
*
* @author 海边沫沫
* @version
*/
public class CanvasTest extends MIDlet{
   private Canvas canvas = new MyCanvas();
   private Display display = null;
   private Command exitCommand = new Command("退出",Command.EXIT,1);
   public void startApp() {
     if(display==null){
       display = Display.getDisplay(this);
       canvas.addCommand(exitCommand);
       canvas.setCommandListener(new MyCommandListener(this,canvas));
       display.setCurrent(canvas);
     }
   }
   public void pauseApp() {
   }
   public void destroyApp(boolean unconditional) {
   }
}

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.