jar from website

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

Re: jar from website

Postby rahul7star » Mon Oct 29, 2007 4:51 pm

my original code

problem is with "mainscrn.png" image ( dimension 128 / 128), only this image is working and if i replace this with ny other (say 16/16 or ny other multiple of 16 or even other image ) , then its not working

error name is "uncaught exception java/lang/IndexOutOfBoundsException"


Code: Select all
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class ExampleGameCanvas 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
// Sprites to be used
private Sprite playerSprite;
private Sprite backgroundSprite;
// Layer Manager
private LayerManager layerManager;
// Constructor and initialization
public ExampleGameCanvas() throws Exception {
super(true);
width = getWidth();
height = getHeight();
currentX = width ;
currentY = height;
delay = 20;
// Load Images to Sprites
Image playerImage = Image.createImage("/mainscrn.png");
playerSprite = new Sprite (playerImage,32,32);
Image backgroundImage = Image.createImage("/GladiatorGallery2.jpg");
backgroundSprite = new Sprite(backgroundImage);
layerManager = new LayerManager();
layerManager.append(playerSprite);
layerManager.append(backgroundSprite);
}
// 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();
playerSprite.setFrame(0);
// Left
if ((keyStates & LEFT_PRESSED) != 0) {
currentX = Math.max(0, currentX - 1);
//backgroundSprite.setFrame(3);
}
// Right
if ((keyStates & RIGHT_PRESSED) !=0 )
if ( currentX + 5 < width) {
currentX = Math.min(width, currentX + 1);
playerSprite.setFrame(0);
}
// Up
if ((keyStates & UP_PRESSED) != 0) {
currentY = Math.max(0, currentY - 1);
playerSprite.setFrame(1);
}
// Down
if ((keyStates & DOWN_PRESSED) !=0)
if ( currentY + 10 < height) {
currentY = Math.min(height, currentY + 1);
playerSprite.setFrame(2);
}
}
// Method to Display Graphics
//private void drawScreen(Graphics g) {
//g.setColor(0x00C000);
private void drawScreen(Graphics g) {
//g.setColor(0x00C000);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xffff00);
// updating player sprite position
playerSprite.setPosition(currentX,currentY);
// display all layers
layerManager.paint(g,0,0);
flushGraphics();
}
}
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: jar from website

Postby DevelopmentTeam » Tue Oct 30, 2007 2:56 am

Here is the corrected code.

Code: Select all
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    public class ExampleGameCanvas 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
    // Sprites to be used
    private Sprite playerSprite;
    // Constructor and initialization
    public ExampleGameCanvas() throws Exception {
    super(true);
    width = getWidth();
    height = getHeight();
    currentX = width ;
    currentY = height;
    delay = 20;
    // Load Images to Sprites
    Image playerImage = Image.createImage("/mainscrn.png");
    playerSprite = new Sprite (playerImage,32,32);
    Image backgroundImage = Image.createImage("/GladiatorGallery2.jpg");
    }
    // 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();
    playerSprite.setFrame(0);
    // Left
    if ((keyStates & LEFT_PRESSED) != 0) {
    currentX = Math.max(0, currentX - 1);
    playerSprite.setFrame(3);
    }
    // Right
    if ((keyStates & RIGHT_PRESSED) !=0 )
    if ( currentX + 5 < width) {
    currentX = Math.min(width, currentX + 1);
    playerSprite.setFrame(0);
    }
    // Up
    if ((keyStates & UP_PRESSED) != 0) {
    currentY = Math.max(0, currentY - 1);
    playerSprite.setFrame(1);
    }
    // Down
    if ((keyStates & DOWN_PRESSED) !=0)
    if ( currentY + 10 < height) {
    currentY = Math.min(height, currentY + 1);
    playerSprite.setFrame(2);
    }
    }
    // Method to Display Graphics
    //private void drawScreen(Graphics g) {
    //g.setColor(0x00C000);
    private void drawScreen(Graphics g) {
    //g.setColor(0x00C000);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(0xffff00);
    // updating player sprite position
    playerSprite.setPosition(currentX,currentY);
    // display all layers
    g.drawImage(backgroundImage, 0, 0, 0);
    playerSprite.paint(g);
    flushGraphics();
    }
    }
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: jar from website

Postby rahul7star » Tue Oct 30, 2007 4:12 pm

HI


this line is having problem


" g.drawImage(backgroundImage, 0, 0, 0);"

as cannot find symbol



symbol : variable backgroundImage
location: class ExampleGameCanvas
g.drawImage(backgroundImage, 0, 0, 0);
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: jar from website

Postby DevelopmentTeam » Wed Oct 31, 2007 2:19 am

rahul7star wrote:HI
this line is having problem

" g.drawImage(backgroundImage, 0, 0, 0);"

as cannot find symbol

symbol : variable backgroundImage
location: class ExampleGameCanvas
g.drawImage(backgroundImage, 0, 0, 0);


ok, I forgot to declare the image...
You should be able to find these kind of mistakes as I dont check the code by compiling it. I just can give you the basic idea.

Code: Select all
       
        import javax.microedition.lcdui.*;
        import javax.microedition.lcdui.game.*;
        public class ExampleGameCanvas 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
        // Sprites to be used
        private Sprite playerSprite;
        Image backgroundImage;
        // Constructor and initialization
        public ExampleGameCanvas() throws Exception {
        super(true);
        width = getWidth();
        height = getHeight();
        currentX = width ;
        currentY = height;
        delay = 20;
        // Load Images to Sprites
        Image playerImage = Image.createImage("/mainscrn.png");
        playerSprite = new Sprite (playerImage,32,32);
        backgroundImage = Image.createImage("/GladiatorGallery2.jpg");
        }
        // 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();
        playerSprite.setFrame(0);
        // Left
        if ((keyStates & LEFT_PRESSED) != 0) {
        currentX = Math.max(0, currentX - 1);
        playerSprite.setFrame(3);
        }
        // Right
        if ((keyStates & RIGHT_PRESSED) !=0 )
        if ( currentX + 5 < width) {
        currentX = Math.min(width, currentX + 1);
        playerSprite.setFrame(0);
        }
        // Up
        if ((keyStates & UP_PRESSED) != 0) {
        currentY = Math.max(0, currentY - 1);
        playerSprite.setFrame(1);
        }
        // Down
        if ((keyStates & DOWN_PRESSED) !=0)
        if ( currentY + 10 < height) {
        currentY = Math.min(height, currentY + 1);
        playerSprite.setFrame(2);
        }
        }
        // Method to Display Graphics
        //private void drawScreen(Graphics g) {
        //g.setColor(0x00C000);
        private void drawScreen(Graphics g) {
        //g.setColor(0x00C000);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0xffff00);
        // updating player sprite position
        playerSprite.setPosition(currentX,currentY);
        // display all layers
        g.drawImage(backgroundImage, 0, 0, 0);
        playerSprite.paint(g);
        flushGraphics();
        }
        }
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: jar from website

Postby rahul7star » Wed Oct 31, 2007 6:01 pm

extremly sorry 4 dat 1


ok i would like to ask 1 more thing

loook at dis code

Image playerImage = Image.createImage("/box1.png");
playerSprite = new Sprite (playerImage,22,22);



box1.png is having dimension 66 /73 , so 66 is divisible by 22 , but 73 is not
so is dis is only reason dat m not able to load this image . or something else.
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: jar from website

Postby DevelopmentTeam » Thu Nov 01, 2007 1:41 am

rahul7star wrote:extremly sorry 4 dat 1
loook at dis code

Image playerImage = Image.createImage("/box1.png");
playerSprite = new Sprite (playerImage,22,22);



box1.png is having dimension 66 /73 , so 66 is divisible by 22 , but 73 is not
so is dis is only reason dat m not able to load this image . or something else.


yes this is the only reason that gives you error message. Coz a sprite class tries to split the images into smaller ones with the given tile dimension. And when the given image dimension is not multiples of the tile dimension then it is pointless and the class will end up in messing the last tile in the image so it gives you an error.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: jar from website

Postby rahul7star » Sat Nov 03, 2007 8:32 am

Hi

well m having a "gif" image wher der is som animation , if i use dat image in backgorund Image


then why d image is notshowing its animation , its just still in its position.




and 1 mor thing
lets suppose dat m having my jar n jad with me , but i want to put only my jump code in website
and whenever i click on jad i need to call this jump code 4m website , is it possible.
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: jar from website

Postby DevelopmentTeam » Sat Nov 03, 2007 8:47 am

rahul7star wrote:well m having a "gif" image wher der is som animation , if i use dat image in backgorund Image
then why d image is notshowing its animation , its just still in its position.


most of the devices do not support animated gif in a j2me application. You have use sprite.


rahul7star wrote:lets suppose dat m having my jar n jad with me , but i want to put only my jump code in website
and whenever i click on jad i need to call this jump code 4m website , is it possible.


you cannot put a part of the code in the server and rest here. I can understand from your post that you are trying to some kind of auto update feature like in most OS. But in our case the jar file's content cannot be changed. So you cannot bring the code and compile it here. Might be the option is, you can store data in the server and use it from your application. But not the code itself. In other possible method, you can do a part of the calculations there in the server. Say if you want to calculate sum of 2 numbers and put the addition code on the server then the server url has to be written such that it accepts 2 parameters and returns their sum. And you have to call it from the j2me code and read its output... This was you can take some code to the server
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Previous

Return to J2ME Games

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron