Java-JPanel中的Sprite错误 [英] Java - Sprite in JPanel error

查看:123
本文介绍了Java-JPanel中的Sprite错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次运行时,面板的一角都是白线,其余的都是黑色. 我想将KeyListener添加到程序中,以便当我按"W"键时,它将使子画面产生动画效果.

Every time i run the panel is filled with a white line in the corner of it and the rest is black. I would like to add KeyListener to the program so when i push the "W" key it will animate the sprite as it seems to be going forward.

如果sprite图像块到达面板边缘时,如果sprite并非一直处于居中状态,那么我将如何编写碰撞检测代码以重绘地图.

IF the sprite is not centered all the time than how would i code a collision detect to repaint the map when the sprite image block reaches the edge of the panel.

我知道我问了多个问题,但仍想找出一个问题.

I know im asking more than one question yet im tring to figure out one problem.

我将如何在平铺的地图上尚未绘制的特定位置生成一个简单的NPC.让这个NPC变得棘手.

How would i spawn a simple NPC at a specific location on the tiled map that has not yet been painted. Have this NPC to be intractable.

我想做的是在屏幕中央放置一个精灵.在背景中有平铺的地图,碰撞检测取决于颜色方块.如果瓷砖层为红色,则不允许通过如果瓷砖层为红色,则允许通过如果瓷砖颜色为黄色,则应采取行动,因为它是一个宝藏.

What I would like to do is have a sprite in the center of the screen. Have a tiled map in the background with collision detect depended on color squares. IF tile layer is colored red not allow to pass through IF tile layer is colored red allow to pass through If tile color yellow apply action for it is a treasure chest.

  public class gamePanel extends JPanel {
   private BufferedImage image;

    public gamePanel() throws IOException{
        this.setBackground(Color.red);  

    }
    public static void loadMap(Graphics g, int frame){
       try {
           BufferedImage bigImg = ImageIO.read(new File("C:\\Users\\czar\\Documents\\NetBeansProjects\\Inter\\src\\inter\\menu.jpg"));
       } catch (IOException ex) {
           Logger.getLogger(gamePanel.class.getName()).log(Level.SEVERE, null, ex);
       }


    }
    public static void loadSprite(Graphics g, int move_int) throws IOException{
        BufferedImage bigImg = ImageIO.read(new File("C:\\Users\\czar\\Documents\\NetBeansProjects\\Inter\\src\\inter\\sprite.jpg"));
        // The above line throws an checked IOException which must be caught.

        final int width = 25;
        final int height = 40;

        final int cols = 2;
        BufferedImage[] left_move = new BufferedImage[3];
        BufferedImage[] right_move = new BufferedImage[3];
        BufferedImage[] up_move = new BufferedImage[3];
        BufferedImage[] down_move = new BufferedImage[3];

        for(int i = 0; i < 4; i++){
            if(move_int == 0){
                left_move[i] = bigImg.getSubimage(
                0 * width, 0 * height, width, height );
            }
            if(move_int == 1){
                right_move[i] = bigImg.getSubimage(
                i * width, 1 * height, width, height );
            }
            if(move_int == 2){
                up_move[i] = bigImg.getSubimage(
                i * width, 2 * height, width, height );
            }
            if(move_int == 3){
                down_move[i] = bigImg.getSubimage(
                i * width, 3 * height, width, height );
            }
        }


    }

    @Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
     try {

         loadMap(g, 0);
         loadSprite(g, 0);

     } catch (IOException ex) {
            // handle exception...
        }
}


}

推荐答案

我不确定您要做什么,但是这里有一些快速的帮助您解决错误.

I'm not really sure what you're trying to do, but here is some quick help on solving your error.

此代码...

BufferedImage[] down_move = new BufferedImage[3];

创建一个可以容纳3个图像的数组,即:

creates an array which can hold 3 images, namely:

down_move [0]
down_move [1]
down_move [2]

down_move[0]
down_move[1]
down_move[2]

这是因为数组是从零开始的.如果您这样做:

That is because arrays are zero-based. If you do:

System.out.println(down_move.length);

您将看到它的输出3.这是因为您的数组仅适合3张图像.

you will see it output 3. This is because your array only fits 3 images.

我假设您要在阵列上放置4张图像.因此,您需要将代码修改为:

I assume you want 4 images on your array. Therefore you will need to modify your code to:

BufferedImage[] down_move = new BufferedImage[4];

这将产生4张图像,即:

This will result in 4 images, namely:

down_move [0]
down_move [1]
down_move [2]
down_move [3]

down_move[0]
down_move[1]
down_move[2]
down_move[3]

在您的代码中,您尝试访问仅上升到down_move [2]的数组中的down_move [3].

In your code you are trying to access down_move[3] in an array that only goes up to down_move[2].

读取错误: ArrayIndexOutOfBoundsException:3

您的图像数组有3个索引(0、1和2),并且您正在尝试访问第4个索引的地方(3).

Your image arrays have 3 indices (0, 1, and 2), and somewhere you're trying to access the 4th index (3).

将图像数组的大小更改为4而不是3(如下所示),并且它不再会出现这种异常.

Change your image array sizes to 4 instead of 3 (as shown below) and it should no longer give you this exception.

BufferedImage[] up_move = new BufferedImage[4];
BufferedImage[] down_move = new BufferedImage[4];
BufferedImage[] left_move = new BufferedImage[4];
BufferedImage[] right_move = new BufferedImage[4];

这篇关于Java-JPanel中的Sprite错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆