📄 graphics.java
字号:
* <TR> * <TD ROWSPAN="1" COLSPAN="1"> * <pre><code> * g.drawString(str, x, y, TOP|LEFT); * g.drawString(str, x + f.stringWidth(str)/2, y, TOP|HCENTER); * g.drawString(str, x + f.stringWidth(str), y, TOP|RIGHT); * * g.drawString(str, x, * y + f.getBaselinePosition(), BASELINE|LEFT); * g.drawString(str, x + f.stringWidth(str)/2, * y + f.getBaselinePosition(), BASELINE|HCENTER); * g.drawString(str, x + f.stringWidth(str), * y + f.getBaselinePosition(), BASELINE|RIGHT); * * drawString(str, x, * y + f.getHeight(), BOTTOM|LEFT); * drawString(str, x + f.stringWidth(str)/2, * y + f.getHeight(), BOTTOM|HCENTER); * drawString(str, x + f.stringWidth(str), * y + f.getHeight(), BOTTOM|RIGHT); </code></pre> * </TD> * </TR> * </TABLE> * <p> * For text drawing, the inter-character and inter-line spacing (leading) * specified by the font designer are included as part of the values returned * in the {@link Font#stringWidth(java.lang.String) stringWidth()} * and {@link Font#getHeight() getHeight()} * calls of class {@link Font Font}. * For example, given the following code: </P> * <TABLE BORDER="2"> * <TR> * <TD ROWSPAN="1" COLSPAN="1"> * <pre><code> * // (5) * g.drawString(string1+string2, x, y, TOP|LEFT); * * // (6) * g.drawString(string1, x, y, TOP|LEFT); * g.drawString(string2, x + f.stringWidth(string1), y, TOP|LEFT); </code></pre> * </TD> * </TR> * </TABLE> * </P> * <P> * Code fragments (5) and (6) behave similarly if not identically. This * occurs because <code>f.stringWidth()</code> * includes the inter-character spacing. The exact spacing of may differ * between these calls if the system supports font kerning.</p> * * <p>Similarly, reasonable vertical spacing may be * achieved simply by adding the font height * to the Y-position of subsequent lines. For example: </P> * <TABLE BORDER="2"> * <TR> * <TD ROWSPAN="1" COLSPAN="1"> * <pre><code> * g.drawString(string1, x, y, TOP|LEFT); * g.drawString(string2, x, y + f.fontHeight(), TOP|LEFT); </code></pre> * </TD> * </TR> * </TABLE> * <P> * draws <code>string1</code> and <code>string2</code> on separate lines with * an appropriate amount of inter-line spacing. </p> * <p> * The <code>stringWidth()</code> of the string and the * <code>fontHeight()</code> of the font in which * it is drawn define the size of the bounding box of a piece of text. As * described above, this box includes inter-line and inter-character spacing. * The implementation is required to put this space below and to right of the * pixels actually belonging to the characters drawn. Applications that wish * to position graphics closely with respect to text (for example, to paint a * rectangle around a string of text) may assume that there is space below and * to the right of a string and that there is <em>no</em> space above * and to the * left of the string. </p> * <p> * Anchor points are also used for positioning of images. Similar to text * drawing, the anchor point for an image specifies the point on the bounding * rectangle of the destination that is to positioned at the * <code>(x,y)</code> location * given in the graphics request. Unlike text, vertical centering of images * is well-defined, and thus the <code>VCENTER</code> value may be * used within the anchor * point parameter of image drawing requests. Because images have no notion * of a baseline, the <code>BASELINE</code> value may not be used * within the anchor point * parameter of image drawing requests. </p> * * <h3>Reference</h3> * * <dl> * <dt>Porter-Duff * <dd>Porter, T., and T. Duff. "Compositing Digital Images." * <em>Computer Graphics V18 N3 (SIGGRAPH 1984)</em>, p. 253-259. * </dl> * * @since MIDP 1.0 */public class Graphics { // SYNC NOTE: the main Graphics class is entirely unlocked. There is only // one instance of Graphics (created in Display) and it is only ever legal // for the application to use it when the system calls the paint() method // of a Canvas. Since paint() calls are serialized, and these methods // modify only instance state, no locking is necessary. (If any method // were to read or modify global state, locking would need to be done in // those cases.) /** * Constant for centering text and images horizontally * around the anchor point * * <P>Value <code>1</code> is assigned to <code>HCENTER</code>.</P> */ public static final int HCENTER = 1; /** * Constant for centering images vertically * around the anchor point. * * <P>Value <code>2</code> is assigned to <code>VCENTER</code>.</P> */ public static final int VCENTER = 2; /** * Constant for positioning the anchor point of text and images * to the left of the text or image. * * <P>Value <code>4</code> is assigned to <code>LEFT</code>.</P> */ public static final int LEFT = 4; /** * Constant for positioning the anchor point of text and images * to the right of the text or image. * * <P>Value <code>8</code> is assigned to <code>RIGHT</code>.</P> */ public static final int RIGHT = 8; /** * Constant for positioning the anchor point of text and images * above the text or image. * * <P>Value <code>16</code> is assigned to <code>TOP</code>.</P> */ public static final int TOP = 16; /** * Constant for positioning the anchor point of text and images * below the text or image. * * <P>Value <code>32</code> is assigned to <code>BOTTOM</code>.</P> */ public static final int BOTTOM = 32; /** * Constant for positioning the anchor point at the baseline of text. * * <P>Value <code>64</code> is assigned to <code>BASELINE</code>.</P> */ public static final int BASELINE = 64; /** * Constant for the <code>SOLID</code> stroke style. * * <P>Value <code>0</code> is assigned to <code>SOLID</code>.</P> */ public static final int SOLID = 0; /** * Constant for the <code>DOTTED</code> stroke style. * * <P>Value <code>1</code> is assigned to <code>DOTTED</code>.</P> */ public static final int DOTTED = 1; /** * Intialize the native peer of this Graphics context */ private native void init(); /** * Create a Graphics object with the given width and height * * @param w The maximum width of the new Graphics object * @param h The maximum height of the new Graphics object */ Graphics(int w, int h) { destination = null; maxWidth = (short) (w & 0x7fff); maxHeight = (short) (h & 0x7fff); init(); reset(); } /** * Translates the origin of the graphics context to the point * <code>(x, y)</code> in the current coordinate system. All coordinates * used in subsequent rendering operations on this graphics * context will be relative to this new origin.<p> * * The effect of calls to <code>translate()</code> are * cumulative. For example, calling * <code>translate(1, 2)</code> and then <code>translate(3, * 4)</code> results in a translation of * <code>(4, 6)</code>. <p> * * The application can set an absolute origin <code>(ax, * ay)</code> using the following * technique:<p> * <code> * g.translate(ax - g.getTranslateX(), ay - g.getTranslateY()) * </code><p> * * @param x the x coordinate of the new translation origin * @param y the y coordinate of the new translation origin * @see #getTranslateX() * @see #getTranslateY() */ public void translate(int x, int y) { transX += x; transY += y; } /** * Gets the X coordinate of the translated origin of this graphics context. * @return X of current origin */ public int getTranslateX() { return transX; } /** * Gets the Y coordinate of the translated origin of this graphics context. * @return Y of current origin */ public int getTranslateY() { return transY; } /** * Gets the current color. * @return an integer in form <code>0x00RRGGBB</code> * @see #setColor(int, int, int) */ public int getColor() { return rgbColor; } /** * Gets the red component of the current color. * @return integer value in range <code>0-255</code> * @see #setColor(int, int, int) */ public int getRedComponent() { return (rgbColor >> 16) & 0xff; } /** * Gets the green component of the current color. * @return integer value in range <code>0-255</code> * @see #setColor(int, int, int) */ public int getGreenComponent() { return (rgbColor >> 8) & 0xff; } /** * Gets the blue component of the current color. * @return integer value in range <code>0-255</code> * @see #setColor(int, int, int) */ public int getBlueComponent() { return rgbColor & 0xff; } /** * Gets the current grayscale value of the color being used for rendering * operations. If the color was set by * <code>setGrayScale()</code>, that value is simply * returned. If the color was set by one of the methods that allows setting * of the red, green, and blue components, the value returned is * computed from * the RGB color components (possibly in a device-specific fashion) * that best * approximates the brightness of that color. * * @return integer value in range <code>0-255</code> * @see #setGrayScale */ public int getGrayScale() { return gray; } /** * Sets the current color to the specified RGB values. All subsequent * rendering operations will use this specified color. * @param red the red component of the color being set in range * <code>0-255</code> * @param green the green component of the color being set in range * <code>0-255</code> * @param blue the blue component of the color being set in range * <code>0-255</code> * @throws IllegalArgumentException if any of the color components * are outside of range <code>0-255</code> * @see #getColor */ public void setColor(int red, int green, int blue) { if ((red < 0) || (red > 255) || (green < 0) || (green > 255) || (blue < 0) || (blue > 255)) { throw new IllegalArgumentException("Value out of range"); } rgbColor = (red << 16) | (green << 8) | blue; gray = grayVal(red, green, blue); pixel = getPixel(rgbColor, gray, false); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -