Search code examples
javajpanelcollision-detectionsprite

Java - Sprite in JPanel error


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.

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.

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...
        }
}


}

Solution

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

    This code...

    BufferedImage[] down_move = new BufferedImage[3];
    

    creates an array which can hold 3 images, namely:

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

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

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

    BufferedImage[] down_move = new BufferedImage[4];
    

    This will result in 4 images, namely:

    down_move[0]
    down_move[1]
    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].

    Read the error: ArrayIndexOutOfBoundsException: 3

    Your image arrays have 3 indices (0, 1, and 2), and somewhere you're trying to access the 4th index (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];