⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 canvas.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public int getGameAction(int keyCode) {        // SYNC NOTE: no locking necessary as we are doing a static        // table lookup and getGameAction() is implemented natively        int n = Display.getGameAction(keyCode);        if (n == -1) {            throw new IllegalArgumentException();        }        return n;    }    /**     * <P>Called when a key is pressed.</P>     *     * <P>The getGameAction() method can be called to     * determine what game action, if any, is mapped to the key.     * Class Canvas has an empty implementation of this method, and     * the subclass has to redefine it if it wants to listen this method.     * @param keyCode The key code of the key that was pressed.     */    protected void keyPressed(int keyCode) {    }    /**     * Called when a key is repeated (held down). The getGameAction() method can     * be called to determine what game action,     * if any, is mapped to the key.     * Class Canvas has an empty implementation of this method, and     * the subclass has to redefine it if it wants to listen this method.     * @param keyCode The key code of the key that was repeated     * @see #hasRepeatEvents()     */    protected void keyRepeated(int keyCode) {    }    /**     * Called when a key is released. The getGameAction() method     * can be called to     * determine what game action, if any, is mapped to the key.     * Class Canvas has an empty implementation of this method, and     * the subclass has to redefine it if it wants to listen this method.     * @param keyCode The key code of the key that was released     */    protected void keyReleased(int keyCode) {    }        /**     * Called when the pointer is pressed. The     *  {@link #hasPointerEvents() hasPointerEvents()}     * method may be called to determine if the device supports pointer events.     * Class Canvas has an empty implementation of this method, and     * the subclass has to redefine it if it wants to listen this method.     * @param x The horizontal location where the pointer was pressed (relative     * to the Canvas)     * @param y The vertical location where the pointer was pressed (relative to     * the Canvas)     */    protected void pointerPressed(int x, int y) {    }        /**     * Called when the pointer is released. The     * {@link #hasPointerEvents() hasPointerEvents()}     * method may be called to determine if the device supports pointer events.     * Class Canvas has an empty implementation of this method, and     * the subclass has to redefine it if it wants to listen this method.     * @param x The horizontal location where the pointer was released (relative     * to the Canvas)     * @param y The vertical location where the pointer was released     * (relative to the Canvas)     */    protected void pointerReleased(int x, int y) {    }        /**     * Called when the pointer is dragged. The     * {@link #hasPointerMotionEvents() hasPointerMotionEvents()}     * method may be called to determine if the device supports pointer events.     * Class Canvas has an empty implementation of this method, and     * the subclass has to redefine it if it wants to listen this method.     * @param x The horizontal location where the pointer was dragged (relative     * to the Canvas)     * @param y The vertical location where the pointer was dragged (relative to     * the Canvas)     */    protected void pointerDragged(int x, int y) {    }        /**     * <p> Requests a repaint for the specified region of the Screen. Calling     * this method may result in subsequent call to paint(), where the passed     * Graphics object's clip region will include at least the specified region.     * </p>     *     * <p> If the canvas is not visible, or if width and height are zero     * or less, or if the rectangle does not specify a visible region     * of the display, this call has no effect. </p>     *     * <p> The call to paint() occurs independently of the call to repaint().     * That is, repaint() will not block waiting for paint() to finish. The     * paint() method will either be called after the caller of repaint()     * returns     * to the implementation (if the caller is a callback) or on another thread     * entirely. </p>     *     * <p> To synchronize with its paint() routine, applications can use either     * {@link Display#callSerially(Runnable) Display.callSerially()} or     * {@link #serviceRepaints() serviceRepaints()}, or they can code explicit     * synchronization into their paint() routine. </p>     *     * <p> The origin of the coordinate system is above and to the left of the     * pixel in the upper left corner of the displayable area of the Canvas. The     * X-coordinate is positive right and the Y-coordinate is positive     * downwards.     * </p>     *     * @param x the x coordinate of the rectangle to be repainted     * @param y the y coordinate of the rectangle to be repainted     * @param width the width of the rectangle to be repainted     * @param height the height of the rectangle to be repainted     *     * @see Display#callSerially(Runnable)     * @see #serviceRepaints()     */    public final void repaint(int x, int y, int width, int height) {        synchronized (Display.LCDUILock) {            super.repaint(x, y, width, height);        }    }    /**     * Requests a repaint for the entire Canvas. The effect is identical to     * <p> <code> repaint(0, 0, getWidth(), getHeight()); </code>     */    public final void repaint() {        synchronized (Display.LCDUILock) {            super.repaint();        }    }        /**     * <p> Forces any pending repaint requests to be serviced immediately. This     * method blocks until the pending requests have been serviced. If there are     * no pending repaints, or if this canvas is not visible on the display,     * this call does nothing and returns immediately. </p>     *     * <p> <b> WARNING: </b> This method blocks until the call to the     * application's paint() method returns. The application has no control over     * which thread calls paint(); it may vary from implementation to     * implementation. If the caller of serviceRepaints() holds a lock that the     * paint() method acquires, this may result in deadlock. Therefore, callers     * of serviceRepaints() <em>must not</em> hold any locks that might be     * acquired within the paint() method. The     * {@link Display#callSerially(Runnable) Display.callSerially()}     * method provides a facility where an application can be called back after     * painting has completed, avoiding the danger of deadlock.     * </p>     *     * @see Display#callSerially(Runnable)     */    public final void serviceRepaints() {        // SYNC NOTE: unlike most public API methods, no locking is done        // here.  This is necessary because Display.serviceRepaints()        // needs to handle its own locking.  We avoid locking by making        // a copy of currentDisplay -- an atomic operation -- before        // testing and using it.        Display d = currentDisplay;        if (d != null) {            d.serviceRepaints();        }    }    /**     * The implementation calls showNotify()     * immediately prior to this Canvas being made visible on the display.     * Canvas subclasses may override     * this method to perform tasks before being shown, such     * as setting up animations, starting timers, etc.     * The default implementation of this method in class Canvas is empty.     */    protected void showNotify() {    }        /**     * The implementation calls hideNotify() shortly after the Canvas has been      * removed from the display.     * Canvas subclasses may override this method in order to pause animations,      * revoke timers, etc.  The default implementation of this      * method in class Canvas is empty.     */    protected void hideNotify()  {    }    /**     * Renders the Canvas. The application must implement this method     * in order to paint any graphics.<p>     *     * The Graphics object's clip region defines the area of the screen that is     * considered to be invalid. A correctly-written paint() routine must paint     * <em>every</em> pixel within this region. Applications <em>must not</em>     * assume that     * they know the underlying source of the paint() call and use this     * assumption     * to paint only a subset of the pixels within the clip region. The     * reason is     * that this particular paint() call may have resulted from multiple     * repaint()     * requests, some of which may have been generated from outside the     * application. An application that paints only what it thinks is     * necessary to     * be painted may display incorrectly if the screen contents had been     * invalidated by, for example, an incoming telephone call. <p>     *     * Operations on this graphics object after the paint() call returns are     * undefined. Thus, the application <em>must not</em> cache this Graphics     * object for later use or use by another thread. It must only be     * used within     * the scope of this method. <p>     *     * The implementation may postpone visible effects of     * graphics operations until the end of the paint method.<p>     *     * <p> The contents of the Canvas are never saved if it is hidden and then     * is made visible again. Thus, shortly after showNotify() is called,     * paint() will always be called with a Graphics object whose clip region     * specifies the entire displayable area of the Canvas.  Applications     * <em>must not</em> rely on any contents being preserved from a previous     * occasion when the Canvas was current. This call to paint() will not     * necessarily occur before any other key, pointer, or commandAction()     * methods are called on the Canvas.  Applications whose repaint     * recomputation is expensive may create an offscreen Image, paint into it,     * and then draw this image on the Canvas when paint() is called. </p>     *     * <P>The application code must never call paint(); it is called only by     * the implementation.</P>     *     * <P>The Graphics object passed to the paint() method has the following      * properties:</P>     * <UL>     * <LI>the destination is the actual display, or if double buffering is in      * effect, a back buffer for the display;</LI>     * <LI>the clip region includes at least one pixel     * within this Canvas;</LI>     * <LI>the current color is black;</LI>     * <LI>the font is the same as the font returned by     * {@link Font#getDefaultFont() Font.getDefaultFont()};</LI>     * <LI>the stroke style is {@link Graphics#SOLID SOLID};</LI>     * <LI>the origin of the coordinate system is located at the upper-left     * corner of the Canvas; and</LI>     * <LI>the Canvas is visible, that is, a call to isShown() will return     * <code>true</code>.</LI>     * </UL>     *     * @param g the Graphics object to be used for rendering the Canvas     */    protected abstract void paint(Graphics g);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -