如何在画布上绘制大图像并根据关键事件移动

发布于 2024-12-28 21:31:30 字数 313 浏览 1 评论 0原文

我有一个大图像。该图像有一些供用户使用的文本。所以我不能调整图像大小以适应较小的屏幕。图像比设备屏幕尺寸大得多。我的目的是在画布上绘制完整的图像而不考虑尺寸。我想根据用户按键事件(左、右、上、下)一点一点地移动图像,比如滚动。

我可以通过以下方式在画布中绘制图像:-

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

我不知道如何根据关键事件进行操作以带来图像的其他部分(例如滚动)。
我见过很多j2me游戏都有这样的功能。
在哪里可以找到这些信息?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹊巢 2025-01-04 21:31:30

您可以使用 Pngcrush 它的主要目的是通过尝试各种压缩级别来减小 PNG IDAT 数据流的大小如果宽度或长度尺寸很大并且您打算将其绘制在画布上,创建图像后,您可以在画布的paint方法中使用Graphics的drawRegion方法来绘制所需的一块上面的图像。您可以更改通过更改drawRegion()方法的参数并重新绘制画布来绘制一块图像(例如当用户按下一个键时):

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();
    }
}
}

这里,第一次从画布中的坐标(ix,iy)绘制图片。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文