Canvas Vs Game Canvas
Article Metadata
This articles shows some differences between Canvas and GameCanvas. The accompanying example shows an actor represented by a square that moves freely on the screen.
Canvas
Known as low-level user interface, it provides a rich variety of possibilities to draw onto the display. A class which extends Canvas has to implement the method paint() with instructions that draw elements in the screen.
GameCanvas
GameCanvas is a Canvas extension to simplify game development and to fix weak points from Canvas. Using GameCanvas is easier to control key events and update the screen. To control key events you just have to use the method getKeyStates() instead of using 3 methods (keyPressed, keyReleased, keyRepeated) used in Canvas and to update the screen you just use flushGraphics() method.
Example:
Let’s suppose we’ve created an actor that is represented by a square in the screen. It has X, Y, Dx and Dy parameters. X and Y are coordinates from the upper left vertex and they cannot be less than zero. Dx and Dy represents the actor’s width and height respectively.
The following snippet of code below will show the difference between Canvas and GameCanvas for handling key events.
Using Canvas
protected void keyPressed(int keyCode) {
switch (getGameAction(keyCode)) {
case UP:
actor.changePosition(actor.getX(), actor.getY() - 5);
break;
case DOWN:
actor.changePosition(actor.getX(), actor.getY() + 5);
break;
case RIGHT:
actor.changePosition(actor.getX() + 5, actor.getY());
break;
case LEFT:
actor.changePosition(actor.getX() - 5, actor.getY());
break;
}
//This method prevents the actor to get out of the screen
verifyPosition();
}
protected void keyRepeated(int keyCode) {
switch (getGameAction(keyCode)) {
case UP:
actor.changePosition(actor.getX(), actor.getY() - 5);
break;
case DOWN:
actor.changePosition(actor.getX(), actor.getY() + 5);
break;
case RIGHT:
actor.changePosition(actor.getX() + 5, actor.getY());
break;
case LEFT:
actor.changePosition(actor.getX() - 5, actor.getY());
break;
}
//This method prevents the actor to get out of the screen
verifyPosition();
}
Using GameCanvas
public void createMoviment() {
int key = getKeyStates();
// changing actor coordinates according to key events
if ((key & LEFT_PRESSED) != 0) {
actor.changePosition(actor.getX() - 5, actor.getY());
} else if ((key & RIGHT_PRESSED) != 0) {
actor.changePosition(actor.getX() + 5, actor.getY());
} else if ((key & UP_PRESSED) != 0) {
actor.changePosition(actor.getX(), actor.getY() - 5);
} else if ((key & DOWN_PRESSED) != 0) {
actor.changePosition(actor.getX(), actor.getY() + 5);
}
// This method prevents the actor to get out of the screen
verifyPosition();
}
As you can see using GameCanvas for handling key events is simplier than use Canvas because you only have to implement one method and call getKeyStates() to know about key events. In this example, when we use Canvas, the methods keyPressed() and keyRepeated() had to be implemented with the same code. The keyPressed() method is called when you press one key and release it. On the other hand, the keyRepeated() method is called when you press one key and holds it, dismissing after an interval time.
The following snippet of code shows the differences between Canvas and GameCanvas to update the screen.
Using Canvas
Image offScreenBuffer;
(...)
protected void paint(Graphics graphics) {
Graphics aux = offScreenBuffer.getGraphics();
// clean the screen
aux.setColor(0xffffffff);
aux.fillRect(0, 0, getWidth(), getHeight());
// draw the actor in the screen
aux.setColor(actor.getColor());
aux.fillRect(actor.getX(), actor.getY(), actor.getDx(), actor.getDy());
graphics.drawImage(offScreenBuffer, 0, 0, Graphics.LEFT| Graphics.TOP);
}
Using GameCanvas
Graphics graphics;
(...)
private void paint() {
// clean the screen
graphic.setColor(0xffffffff);
graphic.fillRect(0, 0, getWidth(), getHeight());
// draw the actor in the screen
graphic.setColor(actor.getColor());
graphic.fillRect(actor.getX(), actor.getY(), actor.getDx(), actor.getDy());
flushGraphics();
}
Updating screen with GameCanvas is very simple, you just have to use flushGraphics() method every time you need to update the screen. However, if you are using Canvas, you will have two buffers, one is on the screen and the other you are updating all elements that you need (everything inside paint() method) in the end of paint() method you update everything. So, it is simpler to control your game cycle and update the screen using GameCanvas than using Canvas.


18 Sep
2009
This article provides an excellent introduction for beginners to the NetBeans IDE, one of the leading IDEs for Java ME development (Eclipse being the major competitor). The article describes the various features of the NetBeans mobility plug-in.
The article walks beginners through the process of creating a new Java ME project in NetBeans. Also demonstrated are the application flow designer (which allows developers to organise the flow of their application between different screens), the visual designer (which allows developers to design user interfaces using the standard UI components such as choice elements) and the emulator (which allows MIDlets to be run on a simulated mobile device). The article also covers important issues, such as selecting a platform SDK and deploying via Nokia PC Suite to a mobile device.
Over and above the usual IDE features such as code completion, the NetBeans mobility plug-in offers developers of mobile applications a range of useful features, such as integration of different SDKs, the flow designer, screen designer and J2MEUnit testing support. As a user of the NetBeans mobility plugin for almost five years, I’ve found it to be an extremely useful and easy to use development environment.
30 Sep
2009
As we know that Canvas is the blank window on which, Drawing of objects is possible. Here This article clearly differentiates between Canvas and Game Canvas. When you are making game, Game Canvas concept is helpful to Game Developer. So through this article, Developer can easily understand how to use Game Canvas. Because it not only reduces the code (Portability) but also reduces the time and also provides flexibility.
When you use Game Canvas, you just need to obtain an integer returned from getKeyStates(). And implementation of a method is necessary that is used to change position of the actor.
This article is very nice and provides good knowledge to beginners and all thing is explained with different modules with code example. And clearly differentiate between Canvas and Game Canvas through code so that every body can easily understand it.