ImageIO 非法参数异常

问题描述 投票:0回答:0

我的程序运行良好,直到我尝试为我的精灵使用 ImageIO。我正在创建 2D 角色扮演游戏。当我追踪它时,它似乎将我的 Entity.Characters 包放在 .png 文件的前面,但我在资源目录内的另一个包中。我尝试将它们移到另一个包中,只是想看看是否可以修复它,但没有。我需要在 intellij 内部的路径中更改某些内容吗? IntelliJ 还一直将其突出显示为黄色,并说 Arugment 'Entity/Characters/wiz/WizardBckRight.png' 可能为空,并将其替换为 Objects.requireNonNull(getClass().getResourceAsStream("/Characters/wiz/WizardBckRight.png");但这也无济于事。与图像有关的所有内容都是正确的,如果单击它会正确显示。我有两个包 Entity.Characters 有 2 个类(Characters、Player)和 laststones 有 3 个类(LastStonesApp、Board、KeyHandler) .

package laststones;
import javafx.application.Application;
import javafx.stage.Stage;
import javax.swing.*;
import java.io.IOException;

public class LastStonesApp extends Application {

    @Override
    public void start(Stage stage) throws IOException {

        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.setTitle("The Last Stones");

        Board gamePanel = new Board();
        window.add(gamePanel);

        window.pack();

        window.setLocationRelativeTo(null);
        window.setVisible(true);

        gamePanel.startGameThread();
    }

    public static void main(String[] args) {
        launch();
    }
}
package laststones;

import Entity.Characters.Player;
import javax.swing.JPanel;
import java.awt.*;

public class Board extends JPanel implements Runnable {
    // SCREEN SETTINGS
    final int originalTileSize = 16;    // 16x16 tile
    final int scale = 3;

    public final int tileSize = originalTileSize * scale; // 48x48 tile
    final int maxScreenCol = 16;
    final int maxScreenRow = 12;
    final int screenWidth = tileSize * maxScreenCol;    // 768 pixels
    final int screenHeight = tileSize * maxScreenRow;   // 576 pixels
    // Frames Per Second
    int FPS = 60;
    KeyHandler keyHandler = new KeyHandler();
    Thread gameThread;

    Player player = new Player(this, keyHandler);

    // set player default position
    int playerX = 100;
    int playerY = 100;
    int playerSpeed = 4;


    public Board() {
        this.setPreferredSize(new Dimension(screenWidth,screenHeight));
        this.setBackground(Color.black);
        this.setDoubleBuffered(true);
        this.addKeyListener(keyHandler);
        // Board is "focused" to receive key input
        this.setFocusable(true);
    }

    public void startGameThread() {
        gameThread = new Thread(this);
        gameThread.start();
    }

    @Override
    public void run() {
        // Allocated time for a single loop
        // 1 second = 1000000000 nanoseconds
        double drawInterval = 1000000000 / FPS;     // 0.016666 seconds
        double delta = 0;                           // using delta time
        long lastTime = System.nanoTime();          // holds time since last frame
        long currentTime;                           // holds current time

        // Game loop implementing delta time
        while (gameThread != null) {
            // Check the current time
            currentTime = System.nanoTime();

            // add past-time divided by drawInterval(allocated time) to delta
            delta += (currentTime - lastTime) / drawInterval;

            // set last time of frame to current time
            lastTime = currentTime;

            if (delta >= 1) {
                update();
                repaint();
                delta--;
            }
        }
    }
    public void update() {
        player.update();
    }
    public void paintComponent(Graphics graphics) {

        super.paintComponent(graphics);

        Graphics2D graphics2D = (Graphics2D) graphics;

        player.draw(graphics2D);

        graphics2D.dispose();
    }
    private boolean dead = false;

    public boolean isDead ()
    {
        return this.dead;
    }
}

package Entity.Characters;

import laststones.Board;
import laststones.KeyHandler;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;

public class Player extends Characters{

    Board gamePanel;
    KeyHandler keyHandler;

    public Player(Board gamePanel, KeyHandler keyHandler) {
        this.gamePanel = gamePanel;
        this.keyHandler = keyHandler;
        setDefaultValues();
        getPlayerImage();
    }
    public void setDefaultValues() {
        x = 100;
        y = 100;
        speed = 4;
        direction = "down";
    }

    public void getPlayerImage() {
        try {
            up1 = ImageIO.read(getClass().getResourceAsStream("Entity/Characters/wiz/WizardBckLeft.png"));
            up2 = ImageIO.read(getClass().getResourceAsStream("Entity/Characters/wiz/WizardBckRight.png"));
            down1 = ImageIO.read(getClass().getResourceAsStream("Entity/Characters/wiz/Wizard_Ldown.png"));
            down2 = ImageIO.read(getClass().getResourceAsStream("Entity/Characters/wiz/Wizard_Rdown.png"));
            left1 = ImageIO.read(getClass().getResourceAsStream("Entity/Characters/wiz/Wizard_Leftface.png"));
            left2 = ImageIO.read(getClass().getResourceAsStream("Entity/Characters/wiz/Wizard_leftWalk.png"));
            right1 = ImageIO.read(getClass().getResourceAsStream("Entity/Characters/wiz/Wizard_Rightface.png"));
            right2 = ImageIO.read(getClass().getResourceAsStream("Entity/Characters/wiz/Wizard_rightWalk.png"));
        } catch (IOException e) {
            System.out.println("This was ran.");
            e.printStackTrace();
        }
    }
    public void update() {
        if (keyHandler.upPressed) {
            direction = "up";
            y -= speed;
        } else if (keyHandler.downPressed) {
            direction = "down";
            y += speed;
        } else if (keyHandler.leftPressed) {
            direction = "left";
            x -= speed;
        } else if (keyHandler.rightPressed) {
            direction = "right";
            x += speed;
        }
    }
    public void draw(Graphics graphics2D) {
        //graphics2D.setColor(Color.white);
        //graphics2D.fillRect(x, y, gamePanel.tileSize, gamePanel.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;
        }
        graphics2D.drawImage(image, x, y, gamePanel.tileSize, gamePanel.tileSize, null);
    }
}

这是我得到的错误,

image javafx 2d javax.imageio illegalargumentexception
© www.soinside.com 2019 - 2024. All rights reserved.