Canvas class

xiaoxiao2021-03-06  17

When we chose the CANVAS class, we mean that we are ready to adopt low-level APIs, which are called low APIs.

It is not because it is very low, and it is exactly that we can use it to handle some very underlying system events, how to support it,

We have to see the level of hardware to determine.

All lower level APIs start from javax.microedition.lcdui.canvas, we must inherit Canvas

You can create a screen displayed on the screen, you need to pay attention to an abstract method in every CanvaS

Paint (), and needs 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 picture.

At any time, we can use repaint () to generate a redraw event, and repaint () has two simultaneous methods, of which

One requires four parameters, respectively, the start coordinates (X, Y), and the width and height, and the other does not require any parameters,

It represents all the entire screens to resemble, which seems that the former is high, most of the actual development is

Using the former rather than the latter, we call repaint (), will return immediately, calling Paint () is another special

Door handling UI thread to complete, if we want to wait until Paint () is completed, then return, we can be in the repaint () method

After adding a servicerepaints () method, its role is to force the over-draw event in the queue to complete as soon as possible, if there is no queue

Redraw events, it will not do anything, so we usually add one after the repaint () method.

ServiceRepaints () method.

We draw image coordinate system on your phone and the coordinate system we usually see:

The usual coordinate system is in the lower left corner, drawing in the first 90 degree sector;

The coordinate system in the MIDP is in the upper left corner, drawing in the fourth 90 degree sector;

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 this method is called, we

You can use your own imagination to draw the pattern you want in 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

Take the 0x00RrGGBB format for coloring.

The above code briefly said how many important parameters of Graphics get, let's talk about how to use it.

Graphics makes specific graphics, if we need to draw a straight line, we can call the draline () method, need to set

Righting the coordinates and end coordinates, a total of four parameters, while Graphics provides two forms of lines, one is dashed,

That is, graphics.dotted, one is a solid line, which is graphics.solid, and we give a piece of 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 achieve rectangles with Graphics's DrawRect () and DrawroundRect () methods.

And rounded rectangles, specific method applications, you can see the documentation of the WTK package.

Let's talk about the screen event of Canvas.

Canvas itself has two states, one is ordinary default, one is a full-screen state, you can use setFullscreen

Mode () method to set it, the difference between the two is when we use full-screen state, Title, Ticker,

And our Command can't display on the screen, and when we call setFullscreenMode (), no matter

What model is called the seizect () method, and incoming the height and width of the screen as its parameters.

Some emergencies, such as calls, etc., the screen will be covered by the system screen, hidenotify () this will be called.

Method, when restoring the protocol, we call our original screen, then the system will call showNotify ().

method.

In Canvas we press the next button to trigger the keypressed () function, and incorporate an integer of the corresponding position

Value, we can easily discover in the MIDP specification, key_num0 - key - Num9 ten constants represent

0-9 on the keyboard, there are two function keys, key_star, key_pound, if the value we paste is less than 0, represents us

Passing the illegal keycode, some machines also support the continuous button response, but this is not JTWI to support,

So we must use the haasrepeatevents () method we mentioned earlier before conducting actual development.

On the high-end mobile phone like Sony Ericsson, the screen's touch event is also supported. It is also the same as JTWI to do a hard regulation, so we have to test in the actual development. We click on the screen, you can trigger the PointerPressed () letter.

Number, and passed into the coordinates of the position, after let go, will trigger the PointerReleaseD () function, and also incoming coordinates,

How to use the body and keypressed () and keyreleased (), you can refer to the description of WTK

The file gets a more detailed method usage rules.

转载请注明原文地址:https://www.9cbs.com/read-42416.html

New Post(0)