我正在尝试将卡的png图像加载到对象,但我不断收到以下错误:
"C:\Program Files\Java\jdk-9\bin\java" "-javaagent:C:\Users\trevo\Documents\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\lib\idea_rt.jar=60524:C:\Users\trevo\Documents\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\trevo\Desktop\Deck\out\production\Deck com.company.Card_Class
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
at com.company.Card_Class.main(Card_Class.java:21)
Process finished with exit code 1
这是我的代码:
package com.company;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Card_Class {
private String suit, face;
private int value;
private BufferedImage cardimage;
public Card_Class(String suit, String face, int value, BufferedImage cardimage) {
this.suit = suit;
this.face = face;
this.value = value;
this.cardimage = cardimage;
}
public static void main(String[] args) throws IOException {
Card_Class KingOfAxes = new Card_Class("Diamonds", "King", 13, ImageIO.read(new File("KingOfAxes.png")));
System.out.println("King");
}
}
我将所有png卡文件放在标有deck的文件夹中,这是项目名称。
答案 0 :(得分:1)
尝试将完整文件路径写入控制台,以查看文件路径是否正确。
也许将文件的绝对路径打印到stdout以查看您的路径是否正确。您还应该在使用之前检查图像是否存在且可读。以下是两个示例:
public static void main(String[] args) throws IOException {
System.out.println(new File("KingOfAxes.png").getAbsolutePath()); // Try this to pinpoint your issue
File king = new File("KingOfAxes.png");
if(king.canRead()){ // Check if your file exists and is readable before you use it
JavaAssignmentPanel KingOfAxes = new JavaAssignmentPanel("Diamonds", "King", 13, ImageIO.read(new File("KingOfAxes.png")));
} else{
throw new IOException(king.getName() + " is not readable!"); // Not readable -> Throw exception
}
System.out.println("King");
}