📄 canvas.java
字号:
* it is called only by * the implementation.</P> * * <P>The <code>Graphics</code> object passed to the * <code>paint()</code> 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 <code>Canvas</code>;</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 <code>Canvas</code>; and</LI> * <LI>the <code>Canvas</code> is visible, that is, a call to * <code>isShown()</code> will return * <code>true</code>.</LI> * </UL> * * @param g the <code>Graphics</code> object to be used for * rendering the <code>Canvas</code> */ protected abstract void paint(Graphics g); /** * Called when the drawable area of the <code>Canvas</code> has * been changed. This * method has augmented semantics compared to {@link * Displayable#sizeChanged(int, int) Displayable.sizeChanged}. * * <p>In addition to the causes listed in * <code>Displayable.sizeChanged</code>, a size change can occur on a * <code>Canvas</code> because of a change between normal and * full-screen modes.</p> * * <p>If the size of a <code>Canvas</code> changes while it is * actually visible on the * display, it may trigger an automatic repaint request. If this occurs, * the call to <code>sizeChanged</code> will occur prior to the call to * <code>paint</code>. If the <code>Canvas</code> has become smaller, the * implementation may choose not to trigger a repaint request if the * remaining contents of the <code>Canvas</code> have been * preserved. Similarly, if * the <code>Canvas</code> has become larger, the implementation * may choose to trigger * a repaint only for the new region. In both cases, the preserved * contents must remain stationary with respect to the origin of the * <code>Canvas</code>. If the size change is significant to the * contents of the * <code>Canvas</code>, the application must explicitly issue a * repaint request for the * changed areas. Note that the application's repaint request should not * cause multiple repaints, since it can be coalesced with repaint * requests that are already pending.</p> * * <p>If the size of a <code>Canvas</code> changes while it is not * visible, the * implementation may choose to delay calls to <code>sizeChanged</code> * until immediately prior to the call to <code>showNotify</code>. In * that case, there will be only one call to <code>sizeChanged</code>, * regardless of the number of size changes.</p> * * <p>An application that is sensitive to size changes can update instance * variables in its implementation of <code>sizeChanged</code>. These * updated values will be available to the code in the * <code>showNotify</code>, <code>hideNotify</code>, and * <code>paint</code> methods.</p> * * @param w the new width in pixels of the drawable area of the * <code>Canvas</code> * @param h the new height in pixels of the drawable area of * the <code>Canvas</code> * @since MIDP 2.0 */ protected void sizeChanged(int w, int h) { // this method is intended to be overridden by the application } /** * Notify this Canvas it is being shown on the given Display * * @param d the Display the Canvas is being shown on */ void callShowNotify(Display d) { super.callShowNotify(d); super.layout(); synchronized (Display.calloutLock) { try { this.showNotify(); } catch (Throwable t) { Display.handleThrowable(t); } } } /** * Notify this Canvas it is being hidden on the given Display * * @param d the Display the Canvas is being hidden on */ void callHideNotify(Display d) { synchronized (Display.calloutLock) { try { this.hideNotify(); } catch (Throwable t) { Display.handleThrowable(t); } } } /** * Paint this Canvas * * @param g the Graphics to paint to * @param target the target Object of this repaint */ void callPaint(Graphics g, Object target) { super.callPaint(g, target); if (g.getClipY() + g.getClipHeight() <= viewport[Y]) { return; } // We prevent the Canvas from drawing outside of the // allowable viewport - such as over the command labels g.clipRect(viewport[X], viewport[Y], viewport[WIDTH], viewport[HEIGHT]); synchronized (Display.calloutLock) { g.translate(viewport[X], viewport[Y]); try { paint(g); } catch (Throwable t) { Display.handleThrowable(t); } g.translate(-viewport[X], -viewport[Y]); } } /** * Handle a size change notification. * * @param w the new width of this Canvas * @param h the new height of this Canvas */ void callSizeChanged(int w, int h) { super.callSizeChanged(w, h); /* * sizeChangeOccurred is a boolean which (when true) indicates * that sizeChanged() will be called at a later time. So, if it * is false after calling super(), we go ahead and notify the * Canvas now, rather than later */ if (!super.sizeChangeOccurred) { synchronized (Display.calloutLock) { try { this.sizeChanged(w, h); } catch (Throwable t) { Display.handleThrowable(t); } } } } /** * Handle a key press * * @param keyCode The key that was pressed */ void callKeyPressed(int keyCode) { if (allowKey(keyCode)) { synchronized (Display.calloutLock) { try { this.keyPressed(keyCode); } catch (Throwable t) { Display.handleThrowable(t); } } } } /** * Handle a key release * * @param keyCode The key that was released */ void callKeyReleased(int keyCode) { if (allowKey(keyCode)) { synchronized (Display.calloutLock) { try { this.keyReleased(keyCode); } catch (Throwable t) { Display.handleThrowable(t); } } } } /** * Handle a repeated key press * * @param keyCode The key that was pressed */ void callKeyRepeated(int keyCode) { if (allowKey(keyCode)) { synchronized (Display.calloutLock) { try { this.keyRepeated(keyCode); } catch (Throwable t) { Display.handleThrowable(t); } } } } /** * Test to see if the given keyCode should be sent to * the application * * @param keyCode the key code to pass to the application * @return true if the key should be allowed */ private boolean allowKey(int keyCode) { if (!suppressKeyEvents) { return true; } // NOTE: we're using Canvas's public getGameAction() // method here switch (getGameAction(keyCode)) { case Canvas.UP: case Canvas.DOWN: case Canvas.LEFT: case Canvas.RIGHT: case Canvas.FIRE: case Canvas.GAME_A: case Canvas.GAME_B: case Canvas.GAME_C: case Canvas.GAME_D : // don't generate key events for // the defined game keys return false; default: return true; } } /** * Handle a pointer press event * * @param x The x coordinate of the press * @param y The y coordinate of the press */ void callPointerPressed(int x, int y) { synchronized (Display.calloutLock) { try { this.pointerPressed(x - viewport[X], y - viewport[Y]); } catch (Throwable t) { Display.handleThrowable(t); } } } /** * Handle a pointer release event * * @param x The x coordinate of the release * @param y The y coordinate of the release */ void callPointerReleased(int x, int y) { synchronized (Display.calloutLock) { try { this.pointerReleased(x - viewport[X], y - viewport[Y]); } catch (Throwable t) { Display.handleThrowable(t); } } } /** * Handle a pointer drag event * * @param x The x coordinate of the drag * @param y The y coordinate of the drag */ void callPointerDragged(int x, int y) { synchronized (Display.calloutLock) { try { this.pointerDragged(x - viewport[X], y - viewport[Y]); } catch (Throwable t) { Display.handleThrowable(t); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -