How to draw large image in canvas and move upon key event

3.3k views Asked by At

I have a large image. This image has some text for user. So I must not resize the image to fit in the smaller screen. Image is much larger than device screen size. My intention is to draw the complete image in canvas without comprising size. I want to move image bit by bit upon user key event (left, right, up, down), something like scrolling.

I can draw an image in canvas by:-

g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);

I don't know how to act according to key event to bring other parts of the images like scrolling.
I have seen many j2me games having such feature.
Where to look for this information?

1

There are 1 answers

0
hasanghaforian On

You can use Pngcrush Its main purpose is to reduce the size of the PNG IDAT datastream by trying various compression levels and PNG filter methods.If size of width or length is very large and you intend draw it on canvas,after creating image,you can use drawRegion method of Graphics in paint method of canvas to draw desired piece of image on it.You can change drawed piece of image(for example when user press a key)by change parameters of drawRegion() method and repaint canvas:

public class CanvasButterfly extends Canvas implements ... {


private int ix, iy;
//image
private Image picture;
/*
* Constructor
*/
public CanvasButterfly() {
    init();
}

/* Function   : paint(Graphics)
* Description : This method is used for rendering Graphics
* Input       : Graphics
* return      : Void
*/
protected void paint(Graphics g) {
    g.setColor(255, 255, 255);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    if (picture != null) {
        g.drawRegion(picture, ix, iy,
              picture.getWidth() - ix, picture.getHeight() - iy,
              Sprite.TRANS_NONE, 0, 0, Graphics.TOP | Graphics.LEFT);
    }
}

/* Function   : moveImage(int)
* Description : This method handle Canvas events
* Input       : void
* return      : Void
*/
private void moveImage(int keyCode) {

    int key = -1;

    try {
        key = getGameAction(keyCode);
    } catch (Exception ex) {
        key = keyCode;
    }

    switch (key) {
        case Canvas.DOWN:
            iy = Math.min(iy + 1,picture.getHeight());
            break;
        case Canvas.UP:
            iy = Math.max(iy - 1,0);
            break;
        case Canvas.LEFT:
            ix = Math.max(ix - 1,0);
            break;
        case Canvas.RIGHT:
            ix = Math.min(ix + 1,picture.getWidth());
            break;
    }

}

//keyPressed 
public void keyPressed(int keyCode) {
    moveImage(keyCode);
    repaint();
}
//keyRepeated
public void keyRepeated(int keyCode) {
    moveImage(keyCode);
    repaint();
}

/* Function   : init()
* Description : This method initialized the class objects
* Input       : void
* return      : Void
*/
private void init() {
//
    ix = ...
    iy = ...

    try {
            picture= Image.createImage("/" + image + ".png");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
}

Here,first time picture drawn from Coordinate(ix,iy) in canvas.