📄 canvas.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc on Wed Sep 24 14:57:49 PDT 2003 -->
<TITLE>
MID Profile: Class Canvas
</TITLE>
<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../stylesheet.css" TITLE="Style">
</HEAD>
<BODY BGCOLOR="white">
<!-- ========== START OF NAVBAR ========== -->
<A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0">
<TR>
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">
<TR ALIGN="center" VALIGN="top">
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD>
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/Canvas.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
<strong>MID Profile</strong></EM>
</TD>
</TR>
<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../javax/microedition/lcdui/AlertType.html"><B>PREV CLASS</B></A>
<A HREF="../../../javax/microedition/lcdui/ChoiceGroup.html"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../index.html" TARGET="_top"><B>FRAMES</B></A>
<A HREF="Canvas.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD>
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY: INNER | <A HREF="#field_summary">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL: <A HREF="#field_detail">FIELD</A> | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD>
</TR>
</TABLE>
<!-- =========== END OF NAVBAR =========== -->
<HR>
<!-- ======== START OF CLASS DATA ======== -->
<H2>
<FONT SIZE="-1">
javax.microedition.lcdui</FONT>
<BR>
Class Canvas</H2>
<PRE>
<A HREF="../../../java/lang/Object.html">java.lang.Object</A>
|
+--<A HREF="../../../javax/microedition/lcdui/Displayable.html">javax.microedition.lcdui.Displayable</A>
|
+--<B>javax.microedition.lcdui.Canvas</B>
</PRE>
<DL>
<DT><B>Direct Known Subclasses:</B> <DD><A HREF="../../../javax/microedition/lcdui/game/GameCanvas.html">GameCanvas</A></DD>
</DL>
<HR>
<DL>
<DT>public abstract class <B>Canvas</B><DT>extends <A HREF="../../../javax/microedition/lcdui/Displayable.html">Displayable</A></DL>
<P>
The <code>Canvas</code> class is a base class for writing applications that need to handle low-level events and to issue graphics calls for drawing to the display. Game applications will likely make heavy use of the <code>Canvas</code> class. From an application development perspective, the <code>Canvas</code> class is interchangeable with standard <code>Screen</code> classes, so an application may mix and match <code>Canvas</code> with high-level screens as needed. For example, a List screen may be used to select the track for a racing game, and a <code>Canvas</code> subclass would implement the actual game. <P>The <code>Canvas</code> provides the developer with methods to handle game actions, key events, and pointer events (if supported by the device). Methods are also provided to identify the device's capabilities and mapping of keys to game actions. The key events are reported with respect to <em>key codes</em>, which are directly bound to concrete keys on the device, use of which may hinder portability. Portable applications should use game actions instead of key codes.</p> <p> Like other subclasses of <code>Displayable</code>, the <code>Canvas</code> class allows the application to register a listener for commands. Unlike other <code>Displayables</code>, however, the <code>Canvas</code> class requires applications to subclass it in order to use it. The <code>paint()</code> method is declared <code>abstract</code>, and so the application <em>must</em> provide an implementation in its subclass. Other event-reporting methods are not declared <code>abstract,</code> and their default implementations are empty (that is, they do nothing). This allows the application to override only the methods that report events in which the application has interest. </p> <p> This is in contrast to the <A HREF="../../../javax/microedition/lcdui/Screen.html"><CODE>Screen</CODE></A> classes, which allow the application to define listeners and to register them with instances of the <code>Screen</code> classes. This style is not used for the <code>Canvas</code> class, because several new listener interfaces would need to be created, one for each kind of event that might be delivered. An alternative would be to have fewer listener interfaces, but this would require listeners to filter out events in which they had no interest. </p> <a name="keyevents"></a> <h3>Key Events</h3> <p> Applications receive keystroke events in which the individual keys are named within a space of <em>key codes</em>. Every key for which events are reported to MIDP applications is assigned a key code. The key code values are unique for each hardware key unless two keys are obvious synonyms for each other. MIDP defines the following key codes: <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM0"><CODE>KEY_NUM0</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM1"><CODE>KEY_NUM1</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM2"><CODE>KEY_NUM2</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM3"><CODE>KEY_NUM3</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM4"><CODE>KEY_NUM4</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM5"><CODE>KEY_NUM5</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM6"><CODE>KEY_NUM6</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM7"><CODE>KEY_NUM7</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM8"><CODE>KEY_NUM8</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_NUM9"><CODE>KEY_NUM9</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_STAR"><CODE>KEY_STAR</CODE></A>, and <A HREF="../../../javax/microedition/lcdui/Canvas.html#KEY_POUND"><CODE>KEY_POUND</CODE></A>. (These key codes correspond to keys on a ITU-T standard telephone keypad.) Other keys may be present on the keyboard, and they will generally have key codes distinct from those list above. In order to guarantee portability, applications should use only the standard key codes. </p> <p>The standard key codes' values are equal to the Unicode encoding for the character that represents the key. If the device includes any other keys that have an obvious correspondence to a Unicode character, their key code values should equal the Unicode encoding for that character. For keys that have no corresponding Unicode character, the implementation must use negative values. Zero is defined to be an invalid key code. It is thus possible for an application to convert a keyCode into a Unicode character using the following code: </p> <TABLE BORDER="2"> <TR> <TD ROWSPAN="1" COLSPAN="1"> <pre><code> if (keyCode > 0) { char ch = (char)keyCode; // ... } </code></pre> </TD> </TR> </TABLE> <p>This technique is useful only in certain limited cases. In particular, it is not sufficient for full textual input, because it does not handle upper and lower case, keyboard shift states, and characters that require more than one keystroke to enter. For textual input, applications should always use <A HREF="../../../javax/microedition/lcdui/TextBox.html"><CODE>TextBox</CODE></A> or <A HREF="../../../javax/microedition/lcdui/TextField.html"><CODE>TextField</CODE></A> objects.</p> <p> It is sometimes useful to find the <em>name</em> of a key in order to display a message about this key. In this case the application may use the <A HREF="../../../javax/microedition/lcdui/Canvas.html#getKeyName(int)"><CODE>getKeyName()</CODE></A> method to find a key's name. </p> <a name="gameactions"></a> <h3>Game Actions</h3> <p> Portable applications that need arrow key events and gaming-related events should use <em>game actions</em> in preference to key codes and key names. MIDP defines the following game actions: <A HREF="../../../javax/microedition/lcdui/Canvas.html#UP"><CODE>UP</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#DOWN"><CODE>DOWN</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#LEFT"><CODE>LEFT</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#RIGHT"><CODE>RIGHT</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#FIRE"><CODE>FIRE</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#GAME_A"><CODE>GAME_A</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#GAME_B"><CODE>GAME_B</CODE></A>, <A HREF="../../../javax/microedition/lcdui/Canvas.html#GAME_C"><CODE>GAME_C</CODE></A>, and <A HREF="../../../javax/microedition/lcdui/Canvas.html#GAME_D"><CODE>GAME_D</CODE></A>. </P> <P> Each key code may be mapped to at most one game action. However, a game action may be associated with more than one key code. The application can translate a key code into a game action using the <A HREF="../../../javax/microedition/lcdui/Canvas.html#getGameAction(int)"><CODE>getGameAction(int keyCode)</CODE></A> method, and it can translate a game action into a key code using the <A HREF="../../../javax/microedition/lcdui/Canvas.html#getKeyCode(int)"><CODE>getKeyCode(int gameAction)</CODE></A> method. There may be multiple keycodes associated with a particular game action, but <code>getKeyCode</code> returns only one of them. Supposing that <code>g</code> is a valid game action and <code>k</code> is a valid key code for a key associated with a game action, consider the following expressions:</p> <TABLE BORDER="2"> <TR> <TD ROWSPAN="1" COLSPAN="1"> <pre><code> g == getGameAction(getKeyCode(g)) // (1) k == getKeyCode(getGameAction(k)) // (2) </code></pre> </TD> </TR> </TABLE> <p>Expression (1) is <em>always</em> true. However, expression (2) might be true but is <em>not necessarily</em> true.</p> <P>The implementation is not allowed to change the mapping of game actions and key codes during execution of the application.</P> <p> Portable applications that are interested in using game actions should translate every key event into a game action by calling the <A HREF="../../../javax/microedition/lcdui/Canvas.html#getGameAction(int)"><CODE>getGameAction()</CODE></A> method and then testing the result. For example, on some devices the game actions <code>UP</code>, <code>DOWN</code>, <code>LEFT</code> and <code>RIGHT</code> may be mapped to 4-way navigation arrow keys. In this case,<code> getKeyCode(UP)</code> would return a device-dependent code for the up-arrow key. On other devices, a possible mapping would be on the number keys <code>2</code>, <code>4</code>, <code>6</code> and <code>8</code>. In this case, <code>getKeyCode(UP)</code> would return <code>KEY_NUM2</code>. In both cases, the <code>getGameAction()</code> method would return the <code>LEFT</code> game action when the user presses the key that is a "natural left" on her device. </P> <a name="commands"></a> <h3>Commands</h3> <p> It is also possible for the user to issue <A HREF="../../../javax/microedition/lcdui/Command.html"><CODE>commands</CODE></A> when a canvas is current. <code>Commands</code> are mapped to keys and menus in a device-specific fashion. For some devices the keys used for commands may overlap with the keys that will deliver key code events to the canvas. If this is the case, the device will provide a means transparent to the application that enables the user to select a mode that determines whether these keys will deliver commands or key code events to the application. When the <code>Canvas</code> is in normal mode (see <a href="#fullscreen">below</a>), the set of key code events available to a canvas will not change depending upon the number of commands present or the presence of a command listener. When the <code>Canvas</code> is in full-screen mode, if there is no command listener present, the device may choose to deliver key code events for keys that would otherwise be reserved for delivery of commands. Game developers should be aware that access to commands will vary greatly across devices, and that requiring the user to issue commands during game play may have a great impact on the ease with which the game can be played. </p> <a name="eventdelivery"></a> <h3>Event Delivery</h3> <P> The <code>Canvas</code> object defines several methods that are called by the implementation. These methods are primarily for the purpose of delivering events to the application, and so they are referred to as <em>event delivery</em> methods. The set of methods is: </p> <ul> <li> <code>showNotify()</code> </li> <li> <code>hideNotify()</code> </li> <li> <code>keyPressed()</code> </li> <li> <code>keyRepeated()</code> </li> <li> <code>keyReleased()</code> </li> <li> <code>pointerPressed()</code> </li> <li> <code>pointerDragged()</code> </li> <li> <code>pointerReleased()</code> </li> <li> <code>paint()</code> </li> </ul> <p> These methods are all called serially. That is, the implementation will never call an event delivery method before a prior call to <em>any</em> of the event delivery methods has returned. The <code>serviceRepaints()</code> method is an exception to this rule, as it blocks until <code>paint()</code> is called and returns. This will occur even if the application is in the midst of one of the event delivery methods when it calls <code>serviceRepaints()</code>. </p> <p>The <A HREF="../../../javax/microedition/lcdui/Display.html#callSerially(java.lang.Runnable)"><CODE>Display.callSerially()</CODE></A> method can be used to serialize some application-defined work with the event stream. For further information, see the <a href="./package-summary.html#events">Event Handling</a> and <a href="./package-summary.html#concurrency">Concurrency</a> sections of the package summary. </p> <p> The key-related, pointer-related, and <code>paint()</code> methods will only be called while the <code>Canvas</code> is actually visible on the output device. These methods will therefore only be called on this <code>Canvas</code> object only after a call to <code>showNotify()</code> and before a call to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -