📄 graphics.java
字号:
int strWidth = currentFont.stringWidth(str); if ((anchor & RIGHT) != 0){ x -= strWidth; } else if ((anchor & HCENTER) != 0) { x -= (strWidth / 2); } } if ((anchor & BASELINE) == 0) { if ((anchor & TOP) != 0) { y += currentFont.getBaselinePosition(); } else if ((anchor & BOTTOM) != 0) { y -= currentFont.getHeight() - currentFont.getBaselinePosition(); } } gciTextRenderer.drawString(str, x, y); } /** * Draws the specified <code>String</code> using the current font and color. * The <code>x,y</code> position is the position of the anchor point. * See <a href="#anchor">anchor points</a>. * * <p>The <code>offset</code> and <code>len</code> parameters must * specify a valid range of characters within * the string <code>str</code>. * The <code>offset</code> parameter must be within the * range <code>[0..(str.length())]</code>, inclusive. * The <code>len</code> parameter * must be a non-negative integer such that * <code>(offset + len) <= str.length()</code>.</p> * * @param str the <code>String</code> to be drawn * @param offset zero-based index of first character in the substring * @param len length of the substring * @param x the x coordinate of the anchor point * @param y the y coordinate of the anchor point * @param anchor the anchor point for positioning the text * @see #drawString(String, int, int, int). * @throws StringIndexOutOfBoundsException if <code>offset</code> * and <code>length</code> do not specify * a valid range within the <code>String</code> <code>str</code> * @throws IllegalArgumentException if <code>anchor</code> * is not a legal value * @throws NullPointerException if <code>str</code> is <code>null</code> */ public void drawSubstring(String str, int offset, int len, int x, int y, int anchor) { // will throw NullPointerException int strLen = str.length(); if ((offset < 0) || (offset > strLen) || (len < 0) || (len > strLen) || ((offset + len) < 0) || ((offset + len) > strLen)) { throw new StringIndexOutOfBoundsException(); } drawString(str.substring(offset, offset + len), x, y, anchor); } /** * Draws the specified character using the current font and color. * @param character the character to be drawn * @param x the x coordinate of the anchor point * @param y the y coordinate of the anchor point * @param anchor the anchor point for positioning the text; see * <a href="#anchor">anchor points</a> * * @throws IllegalArgumentException if <code>anchor</code> * is not a legal value * * @see #drawString(java.lang.String, int, int, int) * @see #drawChars(char[], int, int, int, int, int) */ public void drawChar(char character, int x, int y, int anchor) { drawString(new String(new char[]{character}), x, y, anchor); } /** * Draws the specified characters using the current font and color. * * <p>The <code>offset</code> and <code>length</code> parameters must * specify a valid range of characters within * the character array <code>data</code>. * The <code>offset</code> parameter must be within the * range <code>[0..(data.length)]</code>, inclusive. * The <code>length</code> parameter * must be a non-negative integer such that * <code>(offset + length) <= data.length</code>.</p> * * @param data the array of characters to be drawn * @param offset the start offset in the data * @param length the number of characters to be drawn * @param x the x coordinate of the anchor point * @param y the y coordinate of the anchor point * @param anchor the anchor point for positioning the text; see * <a href="#anchor">anchor points</a> * * @throws ArrayIndexOutOfBoundsException if <code>offset</code> * and <code>length</code> * do not specify a valid range within the data array * @throws IllegalArgumentException if anchor is not a legal value * @throws NullPointerException if <code>data</code> is <code>null</code> * * @see #drawString(java.lang.String, int, int, int) */ public void drawChars(char[] data, int offset, int length, int x, int y, int anchor) { // this will throw NullPointerException if data == null int chLen = data.length; if ((offset < 0) || (offset > chLen) || (length < 0) || (length > chLen) || ((offset + length) < 0) || ((offset + length) > chLen)) { throw new ArrayIndexOutOfBoundsException(); } drawString(new String(data, offset, length), x, y, anchor); } /** * Draws the specified image by using the anchor point. * The image can be drawn in different positions relative to * the anchor point by passing the appropriate position constants. * See <a href="#anchor">anchor points</a>. * * <p>If the source image contains transparent pixels, the corresponding * pixels in the destination image must be left untouched. If the source * image contains partially transparent pixels, a compositing operation * must be performed with the destination pixels, leaving all pixels of * the destination image fully opaque.</p> * * <p>If <code>img</code> is the same as the destination of this Graphics * object, the result is undefined. For copying areas within an * <code>Image</code>, {@link #copyArea copyArea} should be used instead. * </p> * * @param img the specified image to be drawn * @param x the x coordinate of the anchor point * @param y the y coordinate of the anchor point * @param anchor the anchor point for positioning the image * @throws IllegalArgumentException if <code>anchor</code> * is not a legal value * @throws NullPointerException if <code>img</code> is <code>null</code> * @see Image */ public void drawImage(Image image, int x, int y, int anchor) { int[] point = new int[]{x, y}; // will throw NullPointerException as expected if image is null if (!checkAnchor(anchor, BASELINE) || !normalizeAnchor(point, image.getWidth(), image.getHeight(), anchor)) { throw new IllegalArgumentException(); } render(image, point[0] + transX, point[1]+ transY, anchor); } /** * Copies a region of the specified source image to a location within * the destination, possibly transforming (rotating and reflecting) * the image data using the chosen transform function. * * <p>The destination, if it is an image, must not be the same image as * the source image. If it is, an exception is thrown. This restriction * is present in order to avoid ill-defined behaviors that might occur if * overlapped, transformed copies were permitted.</p> * * <p>The transform function used must be one of the following, as defined * in the {@link javax.microedition.lcdui.game.Sprite Sprite} class:<br> * * <code>Sprite.TRANS_NONE</code> - causes the specified image * region to be copied unchanged<br> * <code>Sprite.TRANS_ROT90</code> - causes the specified image * region to be rotated clockwise by 90 degrees.<br> * <code>Sprite.TRANS_ROT180</code> - causes the specified image * region to be rotated clockwise by 180 degrees.<br> * <code>Sprite.TRANS_ROT270</code> - causes the specified image * region to be rotated clockwise by 270 degrees.<br> * <code>Sprite.TRANS_MIRROR</code> - causes the specified image * region to be reflected about its vertical center.<br> * <code>Sprite.TRANS_MIRROR_ROT90</code> - causes the specified image * region to be reflected about its vertical center and then rotated * clockwise by 90 degrees.<br> * <code>Sprite.TRANS_MIRROR_ROT180</code> - causes the specified image * region to be reflected about its vertical center and then rotated * clockwise by 180 degrees.<br> * <code>Sprite.TRANS_MIRROR_ROT270</code> - causes the specified image * region to be reflected about its vertical center and then rotated * clockwise by 270 degrees.<br></p> * * <p>If the source region contains transparent pixels, the corresponding * pixels in the destination region must be left untouched. If the source * region contains partially transparent pixels, a compositing operation * must be performed with the destination pixels, leaving all pixels of * the destination region fully opaque.</p> * * <p> The <code>(x_src, y_src)</code> coordinates are relative to * the upper left * corner of the source image. The <code>x_src</code>, * <code>y_src</code>, <code>width</code>, and <code>height</code> * parameters specify a rectangular region of the source image. It is * illegal for this region to extend beyond the bounds of the source * image. This requires that: </P> * <TABLE BORDER="2"> * <TR> * <TD ROWSPAN="1" COLSPAN="1"> * <pre><code> * x_src >= 0 * y_src >= 0 * x_src + width <= source width * y_src + height <= source height </code></pre> * </TD> * </TR> * </TABLE> * <P> * The <code>(x_dest, y_dest)</code> coordinates are relative to * the coordinate * system of this Graphics object. It is legal for the destination * area to extend beyond the bounds of the <code>Graphics</code> * object. Pixels * outside of the bounds of the <code>Graphics</code> object will * not be drawn.</p> * * <p>The transform is applied to the image data from the region of the * source image, and the result is rendered with its anchor point * positioned at location <code>(x_dest, y_dest)</code> in the * destination.</p> * * @param src the source image to copy from * @param x_src the x coordinate of the upper left corner of the region * within the source image to copy * @param y_src the y coordinate of the upper left corner of the region * within the source image to copy * @param width the width of the region to copy * @param height the height of the region to copy * @param transform the desired transformation for the selected region * being copied * @param x_dest the x coordinate of the anchor point in the * destination drawing area * @param y_dest the y coordinate of the anchor point in the * destination drawing area * @param anchor the anchor point for positioning the region within * the destination image * * @throws IllegalArgumentException if <code>src</code> is the * same image as the * destination of this <code>Graphics</code> object * @throws NullPointerException if <code>src</code> is <code>null</code> * @throws IllegalArgumentException if <code>transform</code> is invalid * @throws IllegalArgumentException if <code>anchor</code> is invalid * @throws IllegalArgumentException if the region to be copied exceeds * the bounds of the source image */ public void drawRegion(Image src, int x_src, int y_src, int width, int height, int transform, int x_dest, int y_dest, int anchor) { if ((transform < Sprite.TRANS_NONE) || (transform > Sprite.TRANS_MIRROR_ROT90)) { throw new IllegalArgumentException(); } int[] point = new int[]{x_dest, y_dest}; if (!checkAnchor(anchor, BASELINE) || !normalizeAnchor(point, width, height, anchor)) { throw new IllegalArgumentException(); } // will generate NullPointerException if src is null as expected int imgWidth = src.getWidth(); int imgHeight = src.getHeight(); if (img == src) { throw new IllegalArgumentException(); } if ((height < 0) || (width < 0) || (x_src < 0) || (y_src < 0) || ((x_src + width) > imgWidth) || ((y_src + height) > imgHeight)) { throw new IllegalArgumentException(); } renderRegion(src, x_src, y_src, width, height, transform, point[0] + transX, point[1]+ transY, anchor); } /** * Copies the contents of a rectangular area * <code>(x_src, y_src, width, height)</code> to a destination area, * whose anchor point identified by anchor is located at * <code>(x_dest, y_dest)</code>. The effect must be that the * destination area * contains an exact copy of the contents of the source area * immediately prior to the invocation of this method. This result must * occur even if the source and destination areas overlap. * * <p>The points <code>(x_src, y_src)</code> and <code>(x_dest, * y_dest)</code> are both specified * relative to the coordinate system of the <code>Graphics</code> * object. It is * illegal for the source region to extend beyond the bounds of the * graphic object. This requires that: </P> * <TABLE BORDER="2"> * <TR> * <TD ROWSPAN="1" COLSPAN="1"> * <pre><code> * x_src + tx >= 0 * y_src + ty >= 0 * x_src + tx + width <= width of Graphics object's destination * y_src + ty + height <= height of Graphics object's destination * </code></pre> * </TD> * </TR> * </TABLE> * * <p>where <code>tx</c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -