📄 graphics.java
字号:
* of the anchor point must be one of the * horizontal constants <code>(LEFT, HCENTER, RIGHT)</code> * combined with one of the vertical constants * <code>(TOP, BASELINE, BOTTOM)</code> using the bit-wise * <code>OR</code> operator. * Zero may also be used as the value of an anchor point. * Using zero for the anchor point value gives results * identical to using <code>TOP | LEFT</code>.</p> * * <p> * Vertical centering of the text is not specified since it is not considered * useful, it is hard to specify, and it is burdensome to implement. Thus, * the <code>VCENTER</code> value is not allowed in the anchor point * parameter of text * drawing calls. </p> * <p> * The actual position of the bounding box * of the text relative to the <code>(x, y)</code> location is * determined by the anchor point. These anchor * points occur at named locations along the * outer edge of the bounding box. Thus, if <code>f</code> * is <code>g</code>'s current font (as returned by * <code>g.getFont()</code>, the following calls will all have * identical results: </P> * <TABLE BORDER="2"> * <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; /** * Create a Graphics object * @param gciDrawingSurface */ Graphics(GCIDrawingSurface gciDrawingSurface) { this.gciDrawingSurface = gciDrawingSurface; gciGraphicsContext = new GCIGraphicsContext(); createRenderers(this.gciDrawingSurface, gciGraphicsContext); } /** * Sets the width and height member variables of this * Graphics object to reflect the correct values e.g. for * clipping correctly * * @param w the width of this Graphics object * @param h the height of this Graphics object */ void setDimensions(int w, int h) { maxWidth = (short) (w & 0x7fff); maxHeight = (short) (h & 0x7fff); transX = transY = 0; setClip(0, 0, maxWidth, maxHeight); } /** * 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 synchronized 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 synchronized int getTranslateX() { return transX; } /** * Gets the Y coordinate of the translated origin of this graphics context. * @return Y of current origin */ public synchronized int getTranslateY() { return transY; } /** * Gets the current color. * @return an integer in form <code>0x00RRGGBB</code> * @see #setColor(int, int, int) */ public synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized int getGrayScale() { return gray; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -