Hello...
I am try to develop some game where I need to move the background Image Please help me
I have written this code but this is not working properly...
I mean I am not getting any errors but the backgroud Image is not moving as I want it to move.
please help me....
Thankz in Advance
Chandu
==============================================
import javax.microedition.lcdui.*;
public class MyCanvas extends javax.microedition.lcdui.Canvas implements Runnable {
Image offImage,bgImage; // An Image for double buffering.
Graphics offGraphics; // A global variable for graphics.
int w,h; //Screen-width and height
Thread thread; // The Thread
int blackCol= 0x00000000;
int whiteCol= 0x00ffffff;
boolean forceTerminate=false;
static int X1,Y1;
public MyCanvas() {
w = 120;
h = 120;
offImage = Image.createImage(150,150); // Prepare image for double buffer.
offGraphics = offImage.getGraphics(); // Prepare graphics for double buffer.
offGraphics.setClip(0, 0, w, h); // Set clipping area.
}
protected void paint(Graphics g) {
g.drawImage(offImage,0,0,20);
g.translate(X1,Y1);
}
public void doStart() {
thread = new Thread(this);
// Prepare the thraed...
thread.start();
// Go...
}
public void run(){
loadImagies();
while(!forceTerminate) {
//Repeat for ever
offGraphics.drawImage(bgImage,0,0,20);
repaint();
serviceRepaints();
//System.out.println("XXX");
}
}
void loadImagies(){
try{
bgImage=Image.createImage("/bgImage.png");
}catch(Exception ex){System.out.println("Error while loading the Image"+ex);}
}
///////////////////////////////////////////////////////////////////////
public void keyPressed(int keyCode)
{
int action = getGameAction(keyCode); // Store the value for the key pressed.
if(action==LEFT || keyCode==KEY_NUM4) //Left Arrow ( move curser to left )
{
X1=X1-50;
// offGraphics.translate(X1,Y1);
System.out.println("X1 --->"+X1);
}
else if(action==RIGHT || keyCode==KEY_NUM6)
//Right Arrow ( move curser to right )
{
X1=X1+50;
//offGraphics.translate(X1,Y1);
System.out.println("X1 --->"+X1);
}
else if(action==UP || keyCode==KEY_NUM2)
//Up Arrow ( move curser to Up)
{
Y1=Y1-50;
// offGraphics.translate(X1,Y1);
System.out.println("Y1 --->"+Y1);
}
else if(action==DOWN || keyCode==KEY_NUM8)
//Down Arrow ( move curser to Down )
{
Y1=Y1+50;
//offGraphics.translate(X1,Y1);
System.out.println("Y1 --->"+Y1);
}
repaint();
serviceRepaints();
}
}



Reply With Quote

