尝试绘画时非法争论

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

我试图在我创建的棋盘上画一个棋子,但不断收到错误,似乎找不到我的图片。

Exception in thread "main" java.lang.IllegalArgumentException: input == null!`
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1358)`
at main.pieces.Piece.<init>(Piece.java:21)`
at main.pieces.Knight.<init>(Knight.java:8)`
at main.Board.addPieces(Board.java:23)`
at main.Board.<init>(Board.java:19)`
at main.App.main(App.java:14)

我尝试移动我的 res 文件夹,但该作品仍然没有被绘制。我什至仔细检查了我的文件以确保它是 png。

这是我的代码:

应用程序.java

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

public class App {
    public static void main(String[] args) throws Exception {
        
        JFrame frame = new JFrame();
        frame.getContentPane().setBackground(new Color(30, 30, 30));
        frame.setLayout(new GridBagLayout());
        frame.setMinimumSize(new Dimension(1000, 1000));
        frame.setLocationRelativeTo(null);

        Board board = new Board();
        frame.add(board);

        frame.setVisible(true);

    }
}

board.java

package main;
import main.pieces.Piece;
import main.pieces.Knight;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class Board extends JPanel {

    public int tileSize = 85;

    int cols = 8;
    int rows = 8;

    ArrayList<Piece> pieceList = new ArrayList<>();

    public Board() {
        this.setPreferredSize(new Dimension(cols * tileSize, rows * tileSize));
        addPieces();
    }

    public void addPieces() {
        pieceList.add(new Knight(this, 2, 0, false));
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        for (int r = 0; r < rows; r++) 
        for (int c = 0; c < cols; c++) {
            g2d.setColor((c + r) %2 == 0 ? new Color(230, 230, 230) : new Color(0, 0, 0));
            g2d.fillRect(c * tileSize, r * tileSize, tileSize, tileSize);
        }

        for (Piece piece : pieceList) {
            piece.paint(g2d);
        }
    }
}

piece.java

package main.pieces;

import main.Board;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Piece {
    
    public int col, row;
    public int xPos, yPos; 

    public boolean isWhite;
    public String name;
    public int value;
    
    BufferedImage sheet;
    {
        try {
            sheet = ImageIO.read(ClassLoader.getSystemResourceAsStream("pieces.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected int sheetScale = sheet.getWidth() / 6;

    Image sprite;

    Board board;

    public Piece(Board board) {
        this.board = board;
    }

    public void paint(Graphics2D g2d) {

        g2d.drawImage(sprite, xPos, yPos, null);

    }

}

knight.java

package main.pieces;

import main.Board;
import java.awt.image.BufferedImage;

public class Knight extends Piece {
    public Knight(Board board, int col, int row, boolean isWhite) {
        super(board);
        this.col = col;
        this.row = row;
        this.xPos = col * board.tileSize;
        this.yPos = row * board.tileSize;


        this.isWhite = isWhite;
        this.name = "Knight";

        this.sprite = sheet.getSubimage(3 * sheetScale, isWhite ? 0 : sheetScale, sheetScale, sheetScale).getScaledInstance(sheetScale, sheetScale, BufferedImage.SCALE_SMOOTH);

    }
}
java graphics jframe jpanel chess
1个回答
0
投票

查看错误信息:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!`
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1358)
at main.pieces.Piece.<init>(Piece.java:21)
at main.pieces.Knight.<init>(Knight.java:8)

问题是您似乎正在将

null
传递到 ImageIO.read

这(我认为)与 Piece.java 中的这一行相关:

    sheet = ImageIO.read(ClassLoader.getSystemResourceAsStream("pieces.png"));

您需要检查对

ClassLoader.getSystemResourceAsStream
的调用并找出为什么返回 null

https://www.tutorialspoint.com/java/lang/classloader_getsystemresourceasstream.htm

© www.soinside.com 2019 - 2024. All rights reserved.