Author: Natsuhiko end
When we chose CANVAS, it means that we are ready to adopt low-level APIs, which is called low API, is not because it is very low, it is exactly, we can use it to handle some very underlying system events, how to support , We have to see the level of hardware to determine.
The list of methods for judging the level of hardware support:
All the low-level APIs start from javax.microedition.lcdui.canvas, we must inherit Canvas can create a screen displayed on the screen, you need to pay attention to each CanvaS must have an abstract method Paint (), and Need to be introduced to a GRAPHICS, you can say that the essence of Canvas is in this Paint () method, we use this method to be responsible for the depiction of this screen screen.
At any time, we can use repaint () to generate a redraw event. Repaint () has two synonymous methods, one of which requires the starting coordinates (X, Y), and the width and height, and One does not need any parameters, it is representative of all the screens to resemble, which seems that the former is high, mostly in the actual development, mostly the former instead of the latter, we call repel () It will return immediately, and call Paint () is made by another thread that deals with the UI. If we want to wait until the Paint () is returned, we can add a servicerepaints () method after the repaint () method, it The role is to compare the over-draw events in the queue to complete as soon as possible. If there is no redraw event in the queue, it will not do anything, so when calling the repaint () method, we usually add a serviceRepaints () method later.
We draw image coordinate system on your mobile phone and the coordinate system we usually see is different.
X
Y
Usual coordinate system
X
Y
Coordinate system in MIDP
This is what we have to pay attention to when drawing images.
Let's talk about this object of Graphics, we can treat it as a white paper, as long as you call this method, we can use your own imagination to draw the pattern you want on this white paper.
Below I use a simple code to illustrate the application of this Graphics object:
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Public Class Test Extends Canvas
{
Public void Paint (Graphics G)
{
G.SetColor (255, 255, 0);
G.fillRect (0, 0, getWidth (), getHeight ());
INT C = g.getcolor ();
INT DC = G.GetdisPlayColor (g.getcolor ());
System.out.println ("The color of the current picture is:" integer.tohexstring (c));
System.out.println ("The R value of the current picture is:" g.GetredComponent ());
System.out.println ("The G value of the current picture is:" g.getgreenComponent ());
System.out.println ("The B value of the current picture is:" g.getBlueComponent ());
System.out.println ("The display color of the current picture is:" Integer.toHexString (DC)); System.out.Println ("The grayscale of the current picture is:" g.getgrayscale ());
}
}
You need to pay attention to the value of R, g, b can only be between 0-255, not exceeding this range, and we can use the 0x00rrggbb format to be used in colors.
The above code briefly said how many important parameters of Graphics get, let's talk about how to do specific graphics with graphics, if we need to draw a straight line, we can call the drawline () method, you need to define it Coordinates and end coordinates, a total of four parameters, while Graphics offers two forms of lines, one is a dotted line, which is graphics.dotted, one is a solid line, the graphics.solid, and we give a code for your reference:
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Public Class GraphicsTestcanvas2 Extends Canvas
{
Public void Paint (Graphics G)
{
G.SetColor (255, 255, 255);
G.fillRect (0, 0, getWidth (), getHeight ());
G.SetColor (255, 0, 0);
g.drawline (1, 1, 100, 10);
G.setstrokestyle (Graphics.dotted);
}
}
With a similar approach, we can implement a rectangular and rounded rectangle with Graphics's DrawRect () and DrawRect () methods, specific method applications, you can see the description of the WTK package.
Let's talk about the screen event of Canvas.
The Canvas itself has two states. One is a normal default, one is a full-screen state, and it can be set to it with the setFullScreenMode () method. The difference between the two is when we use the full-screen state, Title, Ticker, and our Command can't display on the screen, and when we call setFullscreenMode (), no matter what model, you will call the seizechanged () method, and pass it into the height and width of the screen as its parameters. For some emergencies, such as calls, etc., the screen will be covered by the system screen, the hidenotify () method will call our original screen when restored the original, then the system will at the same time. Call showNotify () method.
In Canvas, each of us will trigger the keypressed () function, and pass the integer value of the corresponding position, we can easily discover in the MIDP specification, key_num0 - key - Num9 ten constants represent the keyboard 0-9, there are two function keys, key_star, key_pound, if we are incorporated by less than 0, representing that we have incurred illegal keycode, some machines also support continuous buttons, but this is not JTWI It is necessary to support, so we must use the haasrepeatevents () method we mentioned earlier before performing actual development.
Sony Eric
910C
On such a high-end mobile phone, it also supports the touch event of the screen. This is also not the JTWI to do a hard rule, so we have to test in the actual development. We click on the screen, you can trigger the PointerPressed () function, and incorporated When the coordinates of the position, after leaving, the PointerReleased () function will also be incompatible, and the specific use method and keypressed () and keyreleased () are similar, you can refer to the WTK's description document to get a more detailed method Use rule articles Source: http://www.j2medev.com/Article/showArticle.asp?articleid=225