Java Doc for GameCanvas.java in  » 6.0-JDK-Modules » j2me » javax » microedition » lcdui » game » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » javax.microedition.lcdui.game 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


javax.microedition.lcdui.Canvas
   javax.microedition.lcdui.game.GameCanvas

GameCanvas
abstract public class GameCanvas extends Canvas (Code)
The GameCanvas class provides the basis for a game user interface. In addition to the features inherited from Canvas (commands, input events, etc.) it also provides game-specific capabilities such as an off-screen graphics buffer and the ability to query key status.

A dedicated buffer is created for each GameCanvas instance. Since a unique buffer is provided for each GameCanvas instance, it is preferable to re-use a single GameCanvas instance in the interests of minimizing heap usage. The developer can assume that the contents of this buffer are modified only by calls to the Graphics object(s) obtained from the GameCanvas instance; the contents are not modified by external sources such as other MIDlets or system-level notifications. The buffer is initially filled with white pixels.

The buffer's size is set to the maximum dimensions of the GameCanvas. However, the area that may be flushed is limited by the current dimensions of the GameCanvas (as influenced by the presence of a Ticker, Commands, etc.) when the flush is requested. The current dimensions of the GameCanvas may be obtained by calling javax.microedition.lcdui.Canvas.getWidth getWidth and javax.microedition.lcdui.Canvas.getHeight getHeight .

A game may provide its own thread to run the game loop. A typical loop will check for input, implement the game logic, and then render the updated user interface. The following code illustrates the structure of a typical game loop:

 // Get the Graphics object for the off-screen buffer
 Graphics g = getGraphics();
 while (true) {
 // Check user input and update positions if necessary
 int keyState = getKeyStates();
 if ((keyState & LEFT_PRESSED) != 0) {
 sprite.move(-1, 0);
 }
 else if ((keyState & RIGHT_PRESSED) != 0) {
 sprite.move(1, 0);
 }
 // Clear the background to white
 g.setColor(0xFFFFFF);
 g.fillRect(0,0,getWidth(), getHeight());
 // Draw the Sprite
 sprite.paint(g);
 // Flush the off-screen buffer
 flushGraphics();
 }
 



Field Summary
final public static  intDOWN_PRESSED
     The bit representing the DOWN key.
final public static  intFIRE_PRESSED
     The bit representing the FIRE key.
final public static  intGAME_A_PRESSED
     The bit representing the GAME_A key (may not be supported on all devices).
final public static  intGAME_B_PRESSED
     The bit representing the GAME_B key (may not be supported on all devices).
final public static  intGAME_C_PRESSED
     The bit representing the GAME_C key (may not be supported on all devices).
final public static  intGAME_D_PRESSED
     The bit representing the GAME_D key (may not be supported on all devices).
final public static  intLEFT_PRESSED
     The bit representing the LEFT key.
final public static  intRIGHT_PRESSED
     The bit representing the RIGHT key.
final public static  intUP_PRESSED
     The bit representing the UP key.

Constructor Summary
protected  GameCanvas(boolean suppressKeyEvents)
     Creates a new instance of a GameCanvas.

Method Summary
public  voidflushGraphics(int x, int y, int width, int height)
     Flushes the specified region of the off-screen buffer to the display.
public  voidflushGraphics()
     Flushes the off-screen buffer to the display.
protected  GraphicsgetGraphics()
     Obtains the Graphics object for rendering a GameCanvas.
public  intgetKeyStates()
     Gets the states of the physical game keys.
 GameCanvasLFImplgetLFImpl()
    
public  voidpaint(Graphics g)
     Paints this GameCanvas.

Field Detail
DOWN_PRESSED
final public static int DOWN_PRESSED(Code)
The bit representing the DOWN key. This constant has a value of 0x0040 (1 << Canvas.DOWN).



FIRE_PRESSED
final public static int FIRE_PRESSED(Code)
The bit representing the FIRE key. This constant has a value of 0x0100 (1 << Canvas.FIRE).



GAME_A_PRESSED
final public static int GAME_A_PRESSED(Code)
The bit representing the GAME_A key (may not be supported on all devices). This constant has a value of 0x0200 (1 << Canvas.GAME_A).



GAME_B_PRESSED
final public static int GAME_B_PRESSED(Code)
The bit representing the GAME_B key (may not be supported on all devices). This constant has a value of 0x0400 (1 << Canvas.GAME_B).



GAME_C_PRESSED
final public static int GAME_C_PRESSED(Code)
The bit representing the GAME_C key (may not be supported on all devices). This constant has a value of 0x0800 (1 << Canvas.GAME_C).



GAME_D_PRESSED
final public static int GAME_D_PRESSED(Code)
The bit representing the GAME_D key (may not be supported on all devices). This constant has a value of 0x1000 (1 << Canvas.GAME_D).



LEFT_PRESSED
final public static int LEFT_PRESSED(Code)
The bit representing the LEFT key. This constant has a value of 0x0004 (1 << Canvas.LEFT).



RIGHT_PRESSED
final public static int RIGHT_PRESSED(Code)
The bit representing the RIGHT key. This constant has a value of 0x0020 (1 << Canvas.RIGHT).



UP_PRESSED
final public static int UP_PRESSED(Code)
The bit representing the UP key. This constant has a value of 0x0002 (1 << Canvas.UP).




Constructor Detail
GameCanvas
protected GameCanvas(boolean suppressKeyEvents)(Code)
Creates a new instance of a GameCanvas. A new buffer is also created for the GameCanvas and is initially filled with white pixels.

If the developer only needs to query key status using the getKeyStates method, the regular key event mechanism can be suppressed for game keys while this GameCanvas is shown. If not needed by the application, the suppression of key events may improve performance by eliminating unnecessary system calls to keyPressed, keyRepeated and keyReleased methods.

If requested, key event suppression for a given GameCanvas is started when it is shown (i.e. when showNotify is called) and stopped when it is hidden (i.e. when hideNotify is called). Since the showing and hiding of screens is serialized with the event queue, this arrangement ensures that the suppression effects only those key events intended for the corresponding GameCanvas. Thus, if key events are being generated while another screen is still shown, those key events will continue to be queued and dispatched until that screen is hidden and the GameCanvas has replaced it.

Note that key events can be suppressed only for the defined game keys (UP, DOWN, FIRE, etc.); key events are always generated for all other keys.


Parameters:
  suppressKeyEvents - true to suppress the regularkey event mechanism for game keys, otherwise false.





Method Detail
flushGraphics
public void flushGraphics(int x, int y, int width, int height)(Code)
Flushes the specified region of the off-screen buffer to the display. The contents of the off-screen buffer are not changed as a result of the flush operation. This method does not return until the flush has been completed, so the app may immediately begin to render the next frame to the same buffer once this method returns.

If the specified region extends beyond the current bounds of the GameCanvas, only the intersecting region is flushed. No pixels are flushed if the specified width or height is less than 1.

This method does nothing and returns immediately if the GameCanvas is not currently shown or the flush request cannot be honored because the system is busy.


See Also:   GameCanvas.flushGraphics()
Parameters:
  x - the left edge of the region to be flushed
Parameters:
  y - the top edge of the region to be flushed
Parameters:
  width - the width of the region to be flushed
Parameters:
  height - the height of the region to be flushed




flushGraphics
public void flushGraphics()(Code)
Flushes the off-screen buffer to the display. The size of the flushed area is equal to the size of the GameCanvas. The contents of the off-screen buffer are not changed as a result of the flush operation. This method does not return until the flush has been completed, so the app may immediately begin to render the next frame to the same buffer once this method returns.

This method does nothing and returns immediately if the GameCanvas is not currently shown or the flush request cannot be honored because the system is busy.


See Also:   GameCanvas.flushGraphics(int,int,int,int)




getGraphics
protected Graphics getGraphics()(Code)
Obtains the Graphics object for rendering a GameCanvas. The returned Graphics object renders to the off-screen buffer belonging to this GameCanvas.

Rendering operations do not appear on the display until flushGraphics() is called; flushing the buffer does not change its contents (the pixels are not cleared as a result of the flushing operation).

A new Graphics object is created and returned each time this method is called; therefore, the needed Graphics object(s) should be obtained before the game starts then re-used while the game is running. For each GameCanvas instance, all of the provided graphics objects will render to the same off-screen buffer.

The newly created Graphics object has the following properties:

  • the destination is this GameCanvas' buffer;
  • the clip region encompasses the entire buffer;
  • the current color is black;
  • the font is the same as the font returned by javax.microedition.lcdui.Font.getDefaultFontFont.getDefaultFont() ;
  • the stroke style is Graphics.SOLID SOLID ; and
  • the origin of the coordinate system is located at the upper-left corner of the buffer.

the Graphics object that renders to this GameCanvas' off-screen buffer
See Also:   GameCanvas.flushGraphics()
See Also:   GameCanvas.flushGraphics(int,int,int,int)




getKeyStates
public int getKeyStates()(Code)
Gets the states of the physical game keys. Each bit in the returned integer represents a specific key on the device. A key's bit will be 1 if the key is currently down or has been pressed at least once since the last time this method was called. The bit will be 0 if the key is currently up and has not been pressed at all since the last time this method was called. This latching behavior ensures that a rapid key press and release will always be caught by the game loop, regardless of how slowly the loop runs.

For example:

 // Get the key state and store it
 int keyState = getKeyStates();
 if ((keyState & LEFT_KEY) != 0) {
 positionX--;
 }
 else if ((keyState & RIGHT_KEY) != 0) {
 positionX++;
 }
 

Calling this method has the side effect of clearing any latched state. Another call to getKeyStates immediately after a prior call will therefore report the system's best idea of the current state of the keys, the latched bits having been cleared by the first call.

Some devices may not be able to query the keypad hardware directly and therefore, this method may be implemented by monitoring key press and release events instead. Thus the state reported by getKeyStates might lag the actual state of the physical keys since the timeliness of the key information is be subject to the capabilities of each device. Also, some devices may be incapable of detecting simultaneous presses of multiple keys.

This method returns 0 unless the GameCanvas is currently visible as reported by javax.microedition.lcdui.Displayable.isShown . Upon becoming visible, a GameCanvas will initially indicate that all keys are unpressed (0); if a key is held down while the GameCanvas is being shown, the key must be first released and then pressed in order for the key press to be reported by the GameCanvas.


See Also:   GameCanvas.UP_PRESSED
See Also:   GameCanvas.DOWN_PRESSED
See Also:   GameCanvas.LEFT_PRESSED
See Also:   GameCanvas.RIGHT_PRESSED
See Also:   GameCanvas.FIRE_PRESSED
See Also:   GameCanvas.GAME_A_PRESSED
See Also:   GameCanvas.GAME_B_PRESSED
See Also:   GameCanvas.GAME_C_PRESSED
See Also:   GameCanvas.GAME_D_PRESSED An integer containing the key state information (one bit per key), or 0 if the GameCanvas is not currently shown.




getLFImpl
GameCanvasLFImpl getLFImpl()(Code)
Gets look&feel implementation associated with this GameCanvas GameCanvasLFImpl instance of this GameCanvas



paint
public void paint(Graphics g)(Code)
Paints this GameCanvas. By default, this method renders the the off-screen buffer at (0,0). Rendering of the buffer is subject to the clip region and origin translation of the Graphics object.
Parameters:
  g - the Graphics object with which to render the screen.
throws:
  NullPointerException - if g is null



Fields inherited from javax.microedition.lcdui.Canvas
final public static int DOWN(Code)(Java Doc)
final public static int FIRE(Code)(Java Doc)
final public static int GAME_A(Code)(Java Doc)
final public static int GAME_B(Code)(Java Doc)
final public static int GAME_C(Code)(Java Doc)
final public static int GAME_D(Code)(Java Doc)
final public static int KEY_NUM0(Code)(Java Doc)
final public static int KEY_NUM1(Code)(Java Doc)
final public static int KEY_NUM2(Code)(Java Doc)
final public static int KEY_NUM3(Code)(Java Doc)
final public static int KEY_NUM4(Code)(Java Doc)
final public static int KEY_NUM5(Code)(Java Doc)
final public static int KEY_NUM6(Code)(Java Doc)
final public static int KEY_NUM7(Code)(Java Doc)
final public static int KEY_NUM8(Code)(Java Doc)
final public static int KEY_NUM9(Code)(Java Doc)
final public static int KEY_POUND(Code)(Java Doc)
final public static int KEY_STAR(Code)(Java Doc)
final public static int LEFT(Code)(Java Doc)
final public static int RIGHT(Code)(Java Doc)
final public static int UP(Code)(Java Doc)

Methods inherited from javax.microedition.lcdui.Canvas
public int getGameAction(int keyCode)(Code)(Java Doc)
public int getKeyCode(int gameAction)(Code)(Java Doc)
public String getKeyName(int keyCode)(Code)(Java Doc)
public boolean hasPointerEvents()(Code)(Java Doc)
public boolean hasPointerMotionEvents()(Code)(Java Doc)
public boolean hasRepeatEvents()(Code)(Java Doc)
protected void hideNotify()(Code)(Java Doc)
public boolean isDoubleBuffered()(Code)(Java Doc)
protected void keyPressed(int keyCode)(Code)(Java Doc)
protected void keyReleased(int keyCode)(Code)(Java Doc)
protected void keyRepeated(int keyCode)(Code)(Java Doc)
abstract protected void paint(Graphics g)(Code)(Java Doc)
protected void pointerDragged(int x, int y)(Code)(Java Doc)
protected void pointerPressed(int x, int y)(Code)(Java Doc)
protected void pointerReleased(int x, int y)(Code)(Java Doc)
final public void repaint(int x, int y, int width, int height)(Code)(Java Doc)
final public void repaint()(Code)(Java Doc)
final public void serviceRepaints()(Code)(Java Doc)
public void setFullScreenMode(boolean mode)(Code)(Java Doc)
protected void showNotify()(Code)(Java Doc)
protected void sizeChanged(int w, int h)(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.