gpt4 book ai didi

java - ImageIcon 方法无限循环

转载 作者:行者123 更新时间:2023-12-01 19:31:06 27 4
gpt4 key购买 nike

我正在开发一个 Java swing 应用程序,我正在尝试加载国际象棋棋子的图像,一切都工作得很好,直到突然出现错误:

Exception in thread "main" java.lang.StackOverflowError
at java.base/java.lang.String.indexOf(String.java:1612)
at java.base/java.lang.String.indexOf(String.java:1569)
at java.base/java.net.URLStreamHandler.parseURL(URLStreamHandler.java:151)
at java.base/sun.net.www.protocol.file.Handler.parseURL(Handler.java:67)
at java.base/java.net.URL.<init>(URL.java:696)
at java.base/java.net.URL.<init>(URL.java:563)
at java.base/jdk.internal.loader.URLClassPath$FileLoader.getResource(URLClassPath.java:1222)
at java.base/jdk.internal.loader.URLClassPath$FileLoader.findResource(URLClassPath.java:1211)
at java.base/jdk.internal.loader.URLClassPath.findResource(URLClassPath.java:294)
at java.base/jdk.internal.loader.BuiltinClassLoader.findResourceOnClassPath(BuiltinClassLoader.java:499)
at java.base/jdk.internal.loader.BuiltinClassLoader.findResource(BuiltinClassLoader.java:323)
at java.base/java.lang.ClassLoader.getResource(ClassLoader.java:1400)
at java.base/java.lang.Class.getResource(Class.java:2741)
at model.Board.getImageIcon(Board.java:246)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)
at model.Board.setBoard(Board.java:49)
at model.Board.<init>(Board.java:37)
at model.Board.getBoardInstance(Board.java:28)
at controler.FigureAction.<init>(FigureAction.java:15)
at model.Figure.<init>(Figure.java:30)

并且来自 model.Board.setBoard(Board.java:49) 的部分在之后被记录了很多次(仅该部分)。在调试错误时,我看到它一遍又一遍地进入相同的方法,直到发生堆栈溢出错误。在那里,我从本地地址检索图像。(这一切以前都有效......)方法如下:

private ImageIcon getImageIcon(String path,String description) {
URL imgURL = getClass().getResource(path);
if (imgURL != null) {
Image ii=new ImageIcon(imgURL, description).getImage().getScaledInstance(70, 70, Image.SCALE_SMOOTH);
ImageIcon im=new ImageIcon(ii);
return im;

}
else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

这里调用它

public void setBoard(Color c1,Color c2,Dimension dim) {

for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
Point point=new Point(i,j);
if ((i+j)%2==0) {
if(i==0 && j==0) {
***Figure f=new Figure (null,null,"white_rook",null,getImageIcon("/images/WhiteRook.png",null));***
fields[i][j]=new Field(c1,new Position(point.x,point.y),i*8+j,f);
f.field=fields[i][j];
f.pos=new Position(point.x,point.y);
fields[i][j].add(f);
figures[i][j]=f;
}

这是 setBoard 方法的一部分,它基本上将每个棋子放在 field 上。图类看起来像:

public class Figure extends JPanel {

public Field field;
public Color color;
public String name;
public Movement movement;
public ImageIcon imageIcon;
public Position pos;

public Figure(Field field, Color color, String name, Movement movement, ImageIcon icon) {
this.field = field;
this.color = color;
this.name = name;
this.movement = movement;
this.imageIcon = icon;
this.setPreferredSize(new Dimension(70, 70));
this.addMouseMotionListener(new FigureAction());
this.addMouseListener(new FigureAction());
this.setBackground(null);


}
}

在这里,我从 png 图像中制作缩放后的 imageIcon 并将其检索回来。如果有人能够告诉我可能出了什么问题,我将非常感激。我想我在这里遗漏了一些重要的东西,但目前不知道还能在哪里看。提前致谢。

最佳答案

第一个 getClass() 实际上可以是一个子类,因此为了良好的顺序:

    URL imgURL = Board.class.getResource(path);

然后,优化内存使用和速度。我忽略这里的描述。

Map<String, ImageIcon> sharedIcons = new HashMap<>();

private ImageIcon getImageIcon(String path, String description) {
ImageIcon icon = sharedIcons.get(path);
if (icon == null) {
URL imgURL = getClass().getResource(path);
if (imgURL == null) {
System.err.println("Couldn't find file: " + path);
return null;
}
icon = new ImageIcon(imgURL, description);
Image ii = new ImageIcon(imgURL, description)
.getImage().getScaledInstance(70, 70, Image.SCALE_SMOOTH);
icon = new ImageIcon(ii);

sharedIcons.put(path, icon);
}
return icon;
}

检查是否存在递归。 FigureAction 调用 setBoard 左右。

关于java - ImageIcon 方法无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59755579/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com