J2ME Learning Notes (Nine)

xiaoxiao2021-03-06  20

nine

MIDP low graphics processing

The graphics processing method provided by MIDP is in javax.microedition.lcdui. Mainly as follows: Display, Graphic

S, Canvas, Font, Image. 1, Display: In each MIDlet, it is from a Display, belongs to Singleton.

The category encapsulated by Pattern, you need to use getDisplay to get. When using the setCurrent () method, one DIS is simultaneously

The subclass of Playable is used as a parameter to display the relevant screen on the screen. You can also use getCurrent () to get the current active

Displayable object. At the same time, in order to obtain the screen-related attribute, you can use IsColor () to return to whether it is color, Numcolors.

) Returns the number of colors supported by the screen, if the return value is 2, it is black and white. Whenever Display's setCurrent method gets Canvas

As the parameters, the MIDP system performs the PAINT method and regards the Graphics object as a parameter. There is a little attention in it, because

The screen size of the handheld device is not fixed, so we must pass getWidth (), gethe when filling the background color, the foreground color

IgHT () two methods get the length and wide of the screen. 2, image: When processing graphics, the Image class is used, and can be divided into two aspects of modification and unmodifying according to the image of the Image class.

This image class can be modified by the IsMutable method. You can also get iMag by getWidth (), getHeight () method

E objects of the E object, wide. You can create an invisible image object by reading image files, network downloads, resource bundle, etc. In one way

Can't modify the image object, then the content of this image will not be changed again.

Package com.graph;

/ ** * @AuThor you li * * Copyright (c) 2005 * / import javax.microedition.midlet. *; Import javax.microedition.lcdui. *;

Public class imageex extends midlet imports commandlistener {

Private command cmdexit; private imagecanvas canvas; public imageex () {cmdexit = new command ("exit", command.screen, 2); canvas = new imagecanvas (); // TODO automatically generates constructor stub}

Protected void Startapp () throws midletStateChangeException {// TODO automatic generation method Store canvas.addcommand (cmdexit); canvas.setcommandListener (this); display.getdisplay (this) .Setcurrent (canvas);

Protected void pauseapp () {// TODO automatic generation method stub

}

Protected Void DestroyApp (Boolean Arg0) THROWS MIDLETSTATECHANGEEXCEPTION {// TODO automatic generation method stub

}

Public void command (Command Command, Displayable Screen) {if (Command == cmdexit) {notifyDestroyed ();}}}

Package com.graph;

/ ** * @Author you li * * CopyRight (c) 2005 * / import javax.microedition.lcdui. *;

Public class imagecanvas extends canvas {

Public void Paint (Graphics G) {Try {Image Image ("/ javapowered-2.png"); g.drawImage (image, 0, 0, graphics.top | graphics.left);} catch (Exception e) {E.PrintStackTrace ();}}

}

Establish two classes, then run the program, you can see the image is displayed on the screen of the simulator. In the example, we use createImage

Read an existing picture to create an uncharacted image object. There are also two other constructors to build the same image.

Object, one is createImage (byte [] imagedata, int imageoffset, int imageoffset, int imageoffset

ImageLength, there is a CreateImage (Image source). Of course, if we want to build a modified image object, you can use the constructor CreateImage (int Width, Int)

Height). Because a modified image object is actually the OFF Scree on the background picture, it can be modified in establishing a modification.

The image object should be used to use the canvas's ISDOUFFERED () method to determine if the device supports this method.

Font font

Font.getDefaultFont () - Get the default font of the system. Font.getFont () - get the font currently used. () "Isbold (); isbold (); isplain (); isunderLined (), getStyle (), getSize ()) font.set () - Setting the font About font There are three parameters: Face: graphics .FACE_MONOSPACE; Graphics.FACE_PROPORTIONAL; Graphics.FACE_SYSTEM style: Graphics.STYLE_BOLD; Graphics.STYLE_ITALIC; Graphics.STYLE_PLAIN; Graphics.STYLE_UNDERLINED size: Graphics.SIZE_LARGE; Graphics.SIZE_MEDIUM; Graphics.SIZE_SMALL

Several important methods about font: getBaselinePosition (); getHeight (); charwidth (); charswidth (); StringWidth (); SubstringWidth ()

location point:

The positioning point defines 7 screen points: graphics.top; graphics.bottom; graphics.Right; graphics.hcenter; graphics.vcenter; graphics.baseline. 3, Graphics

Obtaining a Graphics object has two methods, with the Paint () method to pass the parameters, and you can get the Graphics object with the GetGraphics method of the Image class. Both differences in the graphics object obtained by the Paint method We can use the relevant method to draw on the screen, while the GRAPHICS object obtained using the GETGRAPHICS method does not work for any call. When the drawing, the coordinates we used in the graphics approach are not representing the pixel itself, but the coordinates constituted between the pixels and the pixels! This concept of this and our traditional Cartesian coordinates is some to table, and it is 0,0 coordinates above the screen. The setcolor (int Red, int Green, int Blue) method of the GRAPHICS object is used to set the color of the pixel. getGrayscale () gets the grayscale used in the current, and setgrayscale () is used to set the grayscale. There is no way to set up the foreground color background color in the MIDP, so it is necessary to process it by filling. Text and graphic text use Drawchar (), drawchars (), drawstring () drawing and screen padding line: Drawline (), 4 parameters, input x, y start point left, x, y end point coordinate arc Shaped: Drawarc (), 6 parameters, the first 4 coordinates indicating the rectangular range of the curved shape, the fifth decision start angle, the sixth determination arc covers the angle. Rectangle: DrawRect (), 4 parameters, x, y start point coordinates, wide, long garden corner rectangle: DrawroundRect () 6 parameters, the first 4 represents the coordinates of the rectangular range of the curved shape, the fifth is where the garden angle is The rectangular width, the sixth representation of the height of the rectangle. Fill curved: Fillarc (), 6 parameters, the first four decisions range the rectangle of the curved arc, the fifth decision start angle, the sixth determination of the angle filled rectangle: FillRect (), 4 Parameters, X, Y starting point coordinates, wide, long filling garden corner rectangle: FillRoadRect (), 6 parameters, the first 4 coordinates of the rectangular range indicating the curved shape, the fifth is the rectangular width of the garden angle, sixth A high degree of rectangle.

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

New Post(0)