0

The problems are happening in the getPlayerImage method in the first line of the try-catch. So the panel pops up and runs, if the user presses w,a,s,d the filled square goes left,right,down,up accordingly, then I set up the logic for switching between png files etc so that I could swap out the white square for an actual character image. but once it gets to the getPlayerImage() method it's not able to find the images in my device I guess?? I think the problem has to do with how I've set up my src folder and java files, I want to so I have a source folder called src and one called res, but I'm not sure if I implemented res correctly, The png files are stored in a package on the res source folder, and the get player image method is within the src source folder, so maybe it has something to do with that. I've linked the image of how I've set it up ...

enter image description here

^^^^^^^^^^^^^^^^^^^^^^

package entity;

import main.GamePanel;
import java.awt.*;
import javax.swing.*;

import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Graphics2D;
import java.io.IOException;

import javax.imageio.ImageIO;

import main.KeyHandler;

public class Player extends Entity{

    GamePanel gp;
    KeyHandler keyH;

    public Player(GamePanel gp, KeyHandler keyH){
        this.gp = gp;
        this.keyH = keyH;
     
        setDefaultValues();
        getPlayerImage(); 
    }
    public void setDefaultValues(){

        x = 100;
        y= 100;
        speed = 4;
        direction = "down"; 
    }
    public void getPlayerImage(){
        try {
            up1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_up_1.png"));
            up2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_up_2.png"));
            down1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_down_1.png"));
            down2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_down_2.png"));
            left1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_left_1.png"));
            left2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_left_2.png"));
            right1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_right_1.png"));
            right2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_right _2.png")); 
        
        } catch (IOException e) {
           e.printStackTrace();
        } 
    }
    public void update(){

        if(keyH.upPressed){
            direction = "up";
            y -= speed;
        }
        else if(keyH.downPressed){
            direction = "down";
            y += speed;
        }
        else if(keyH.leftPressed){
            direction = "left";
            x -= speed;
        }
        else if(keyH.rightPressed){
            direction = "right";
            x += speed;
        }
     }
    public void draw(Graphics2D g2){ 
        // g2.setColor(Color.white);
        // g2.fillRect(x, y, gp.tileSize, gp.tileSize);

        BufferedImage image = null;

        switch(direction){
            case"up":
                image = up1;
                break;
            case"down":
                image = down1;
                break;    
            case"left":
                image = left1;
                break;
            case"right":
                image = right1;
                break;        }
        g2.drawImage(image, x, y, gp.tileSize, gp.tileSize, null);
         
    }
}

I've tryed importing every javax swing and awt utility, but it didn't work, and Ive tryed different ways of formating my src folders but I'm still clueless.

0 Answers0