Howdy,This is the first blog post of mine.I'm highly interested on Java and opensource technology.This blog will talk about wide spread of Java related things.It's all about my experiences.I have a thirsty about new technology and get use of them to create infinite solutions make the life easier.
This is about j2me Gaming.This post will guide you to make a simple game canvas controller.I do not know the title or topic is fully correct or not but below i'll show the thing that i mean to create.
Initial Requirements.
Configurations.
Lets Start....
1.Create a class call NavigationCanvas and it extends GameCanvas class and implements the Runnable interface.
below show the source code for NavgationCanvas class.
This clsaa is the main midlet that start the application.
This will give you a controllable cross.
You can see the code in below vedio also :)
This is about j2me Gaming.This post will guide you to make a simple game canvas controller.I do not know the title or topic is fully correct or not but below i'll show the thing that i mean to create.
Initial Requirements.
- Java installation JDK 1.5 or higher .
- sun java wireless toolkit-2.5.2_01.
- preferred IDE for your convenience.
Configurations.
- Device Configuration CLDC-1.1
- Device profile MIDP-2.0
Lets Start....
1.Create a class call NavigationCanvas and it extends GameCanvas class and implements the Runnable interface.
below show the source code for NavgationCanvas class.
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
/**
*
* @author kalpa
*/
public class NavigationCanvas extends GameCanvas implements Runnable {
private boolean isPlay; // Game Loop runs when isPlay is true
private long delay; // To give thread consistency
private int currentX, currentY; // To hold current position of the 'X'
private int width; // To hold screen width
private int height; // To hold screen height
private Image facesImage;
private Sprite facesSprite; // Constructor and initialization
public NavigationCanvas()
{
super(true);
width = getWidth();
height = getHeight();
currentX = width / 2;
currentY = height / 2;
delay = 20;
}
// Automatically start thread for game loop
public void start(){
isPlay = true;
Thread t = new Thread(this);
t.start();
}
public void stop(){
isPlay = false;
}
// Main Game Loop
public void run(){
Graphics g = getGraphics();
while (isPlay == true)
{
input();
drawScreen(g);
try
{
Thread.sleep(delay);
} catch (InterruptedException ie)
{
}
}
}
// Method to Handle User Inputs
private void input()
{
int keyStates = getKeyStates();
// Left
if ((keyStates & LEFT_PRESSED) != 0)
{
currentX = Math.max(0, currentX - 1);
}
// Right
if ((keyStates & RIGHT_PRESSED) != 0)
{
if (currentX + 5 < width)
{
currentX = Math.min(width, currentX + 1);
}
}
// Up
if ((keyStates & UP_PRESSED) != 0)
{
currentY = Math.max(0, currentY - 1);
}
// Down
if ((keyStates & DOWN_PRESSED) != 0)
{
if (currentY + 10 < height)
{
currentY = Math.min(height, currentY + 1);
}
}
}
// Method to Display Graphics
private void drawScreen(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
g.drawString("X", currentX, currentY, Graphics.TOP | Graphics.LEFT);
flushGraphics();
}
}
This clsaa is the main midlet that start the application.
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;
/**
* @author kalpa
*/
public class Navigation extends MIDlet
{
private Display display;
public void startApp()
{
display = Display.getDisplay(this);
NavigationCanvas gameCanvas = new NavigationCanvas();
gameCanvas.start();
display.setCurrent(gameCanvas);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
destroyApp(true);
notifyDestroyed();
}
}
This will give you a controllable cross.
You can see the code in below vedio also :)
No comments:
Post a Comment