The Game API
The Game API in J2ME facilitate easy development of game applications for mobile devices. Although most of the game applications are nowadays developed with the help of Eclipse, some small entry level games can be developed with the help of sun java wireless tool kit. The Java ME includes the javax.microedition.lcdui.game package that contains the classes such as GameCanvas class and Layer class. In addition to this, the following classes are present in this package.
The Game API in J2ME facilitate easy development of game applications for mobile devices. Although most of the game applications are nowadays developed with the help of Eclipse, some small entry level games can be developed with the help of sun java wireless tool kit. The Java ME includes the javax.microedition.lcdui.game package that contains the classes such as GameCanvas class and Layer class. In addition to this, the following classes are present in this package.
- The GameCanvas class
- The layer class
- The LayeManager class
- The TiledLayer class
- The Sprite class
The GameCanvas class
The GameCanvas class is a sub-class of the javax.microedition.lcdui.canvas class that allows developer to develop game user interface. Its functions include handling commands and input events that has been inherited from the canvas class.. Mobile developers mostly use the game canvas class for easy development of games.
The following code shows the use of GameCanvas class
The GameCanvas class is a sub-class of the javax.microedition.lcdui.canvas class that allows developer to develop game user interface. Its functions include handling commands and input events that has been inherited from the canvas class.. Mobile developers mostly use the game canvas class for easy development of games.
The following code shows the use of GameCanvas class
public void GameCanvasDemo extends GameCanvas iplements Runnable
{
public void run()
{
while(true)
{
repaint();
}
}
public void paint(Graphics g)
{
}
protected void keyPressed(int keycode)
{
}
}The Layer class
The Layer class represents a visual layer of a game. Each layer has a position that is represented in terms of coordinates. Also, each layer has height and width and can be made visible or invisible depending on the application requirements. Always the initial position of a layer has (0,0) as its coordinates. The layer class has the abstract method public void paint() that must be implemented in the game application.
The LayerManager class
The LayerManager class as its name implies is used to manage the elements of the layer class. This class can be used to maintain the objects of the Layer class in a sequential manner as required by the application. It also provides functionalities such as view window to control and render game's layer on the screen.
The following code shows the implementation of the LayerManager class
The Layer class represents a visual layer of a game. Each layer has a position that is represented in terms of coordinates. Also, each layer has height and width and can be made visible or invisible depending on the application requirements. Always the initial position of a layer has (0,0) as its coordinates. The layer class has the abstract method public void paint() that must be implemented in the game application.
The LayerManager class
The LayerManager class as its name implies is used to manage the elements of the layer class. This class can be used to maintain the objects of the Layer class in a sequential manner as required by the application. It also provides functionalities such as view window to control and render game's layer on the screen.
The following code shows the implementation of the LayerManager class
LayerManager manager = new LayerManager(); lmanager = new LayerManager(); int w = getWidth(); int h = getHeight(); lmanager.setViewWindow(96,0,w,h); Sprite sprite = new Sprite(imageframes, 16, 16); manager.append(sprite);
The TiledLayer class This class in used to represent the layer in a game application in terms of a number of tiles. A tiled layer is a visual object that is filled from a palette of tiles. A single large image can be divided into a number of tiles with the help of the TiledLayer class. Each tile will then be assigned a unique index number which can later on be used for reconstruction of the original image.
The following code shows the constructor of the TiledLayer class.
public TiledLayer(int columns, int rows, Image image, int width, int height)
The Sprite class
The sprite class is used to represent graphical images that can move around and interact with each other on a 2-dimensional game application. However, in the game API, these images are known as sprite. Each sprite is represented in the cartesian coordinated consisting of x and y coordinates. In a game application, the sprite class is mainly used to develop animated graphics.
The following code shows the implementation of the Sprite class
The following code shows the constructor of the TiledLayer class.
public TiledLayer(int columns, int rows, Image image, int width, int height)
The Sprite class
The sprite class is used to represent graphical images that can move around and interact with each other on a 2-dimensional game application. However, in the game API, these images are known as sprite. Each sprite is represented in the cartesian coordinated consisting of x and y coordinates. In a game application, the sprite class is mainly used to develop animated graphics.
The following code shows the implementation of the Sprite class
void init()
{
try
{
image = Image.createImage("/filename.png);
}
catch(Exception e)
{
}
msprite = new Sprite(image, 90, 34);
}
public void paint(Graphics g)
{
msprite.setRefPixelPosition(1,1);
msprite.nextFrame();
msprite.paint(g);
}

No comments:
Post a Comment