Question

I am trying to paint only one part of Image. The image human is 159x22 length.

In that image there is 8 human bodies (2 left, 2 right and etc.). If i try to set the Frame to humanSprite.setFrame(1); I will get an error since I specified in the Sprite constructor the size of an Image so there is only one frame.

Well I have tried to divide it by 8 and Ill getjava.lang.IllegalArgumentException`.

Here is the class :

package org.pack.rhynn;

import java.io.IOException;                     
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;                
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;  
import java.util.Random;


public class play extends GameCanvas implements Runnable{


int sleep = 30;
private Image map;
private Sprite mapSprite;

private Image human;
private Sprite humanSprite;
private int humanX = getWidth() /2;
private int humanY = getHeight() /2;

public play(){
    super(false);
}

public void start(){

    try {                           
        map = Image.createImage("/mapas.png");
        human = Image.createImage("/human.PNG");
    } catch (IOException ioex) {                
        System.out.println(ioex);           
    }


    mapSprite = new Sprite(map);                    
    mapSprite.defineReferencePixel(100, 150);                   
    mapSprite.setRefPixelPosition(0, 0);

    humanSprite = new Sprite(human,159,22);                 
    humanSprite.defineReferencePixel(1, 10);                    
    humanSprite.setRefPixelPosition(humanX, humanY);


    Thread thr = new Thread(this);
    thr.start();
}

public void run(){
    while(true){

        updateScreen(getGraphics());

    try{
        Thread.sleep(sleep);

    }catch(Exception e){}
}
    }

private void createBackground(Graphics g){

    g.setColor(0x000000);
    g.fillRect(0, 0, getWidth(), getHeight());

}

private void updateScreen(Graphics g){
    createBackground(g);



    mapSprite.setRefPixelPosition(0, 0);                
    mapSprite.paint(g);

    humanSprite.setRefPixelPosition(humanX, humanY);
    humanSprite.setFrame(0);

    humanSprite.setPosition(50,50);
    humanSprite.paint(g);

    flushGraphics();


}



}
Was it helpful?

Solution

The problem seems to be with your image. All frames must have the same width and height and the image final width and hight must be a multiple of them.

For example, let's say all your frames are in a single row. If the frame width is 19 and you have 8 frames, the final image width must be 152.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top