In previous tutorial we learned about a simple canvas class.
In this tutoial we will try to create a simple gaming interface using Canvas.
In this code we have created a canvas class and created its object in our Midlet.
Inside the constructor SampleCanvas() , we read a image and then create a sprite ,which represent the moving item inside the game.
You can download sample sprite image from here .You have to paste it inside source(src) folder of your project.
Then whenever a key is pressed ,the canvas function keyPressed() is called automatically.
we define this function to detect the key pressed and then call the check function for the first time(when thread is not started ).
And thereafter it changes the direction of movement by changing keycode.
Check function is created to start the thread and hence the movement of sprite.
Thread ,when started calls the handleaction function which decides the direction of movement of sprite according to the key pressed.
And finally repaint function changes the position of sprite accordingly.
We conclude this tutorial here and will discuss in depth about various other provisions that can be made in subsequent tutorial.
Code:-
import java.io.IOException;
import java.lang.Thread;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;
public class GameMidlet extends MIDlet {
private SampleCanvas gameCanvas;
private Thread t=null;
private Display d;
public void startApp() {
try {
this.gameCanvas = new SampleCanvas();
} catch (IOException ex) {
ex.printStackTrace();
}
d = Display.getDisplay(this);
d.setCurrent(gameCanvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class SampleCanvas extends Canvas implements Runnable {
int x, y; // Location of cross hairs
String event = ""; // Last key event type
int keyCode; // Last keyCode pressed
int w, h; // width and height of the canvas
Image img;
Sprite sprite;
boolean flag;
Thread t1;
SampleCanvas() throws IOException {
img=Image.createImage("/sprite.png");
sprite=new Sprite(img);
w = getWidth();
h = getHeight();
}
protected void keyPressed(int key) {
keyCode = key;
event = "Pressed";
if(t1==null)
Check();
}
protected void Check()
{
flag=true;
t1=new Thread(this);
t1.start();
}
void handleActions(int keyCode) {
int action = getGameAction(keyCode);
switch (action) {
case LEFT:
x -= 1;
break;
case RIGHT:
x += 1;
break;
case UP:
y -= 1;
break;
case DOWN:
y += 1;
break;
}
}
protected void paint(Graphics g) {
g.setColor(255,255, 255);
g.fillRect(0, 0, w, h);
sprite.setRefPixelPosition(0,0) ;
sprite.setPosition(x, y);
sprite.paint(g) ;
}
public void run() {
while(flag)
{
handleActions(keyCode);
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
In this tutoial we will try to create a simple gaming interface using Canvas.
In this code we have created a canvas class and created its object in our Midlet.
Inside the constructor SampleCanvas() , we read a image and then create a sprite ,which represent the moving item inside the game.
You can download sample sprite image from here .You have to paste it inside source(src) folder of your project.
Then whenever a key is pressed ,the canvas function keyPressed() is called automatically.
we define this function to detect the key pressed and then call the check function for the first time(when thread is not started ).
And thereafter it changes the direction of movement by changing keycode.
Check function is created to start the thread and hence the movement of sprite.
Thread ,when started calls the handleaction function which decides the direction of movement of sprite according to the key pressed.
And finally repaint function changes the position of sprite accordingly.
We conclude this tutorial here and will discuss in depth about various other provisions that can be made in subsequent tutorial.
Code:-
import java.io.IOException;
import java.lang.Thread;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;
public class GameMidlet extends MIDlet {
private SampleCanvas gameCanvas;
private Thread t=null;
private Display d;
public void startApp() {
try {
this.gameCanvas = new SampleCanvas();
} catch (IOException ex) {
ex.printStackTrace();
}
d = Display.getDisplay(this);
d.setCurrent(gameCanvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class SampleCanvas extends Canvas implements Runnable {
int x, y; // Location of cross hairs
String event = ""; // Last key event type
int keyCode; // Last keyCode pressed
int w, h; // width and height of the canvas
Image img;
Sprite sprite;
boolean flag;
Thread t1;
SampleCanvas() throws IOException {
img=Image.createImage("/sprite.png");
sprite=new Sprite(img);
w = getWidth();
h = getHeight();
}
protected void keyPressed(int key) {
keyCode = key;
event = "Pressed";
if(t1==null)
Check();
}
protected void Check()
{
flag=true;
t1=new Thread(this);
t1.start();
}
void handleActions(int keyCode) {
int action = getGameAction(keyCode);
switch (action) {
case LEFT:
x -= 1;
break;
case RIGHT:
x += 1;
break;
case UP:
y -= 1;
break;
case DOWN:
y += 1;
break;
}
}
protected void paint(Graphics g) {
g.setColor(255,255, 255);
g.fillRect(0, 0, w, h);
sprite.setRefPixelPosition(0,0) ;
sprite.setPosition(x, y);
sprite.paint(g) ;
}
public void run() {
while(flag)
{
handleActions(keyCode);
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
No comments:
Post a Comment