gpt4 book ai didi

java - 图像图标未加载

转载 作者:行者123 更新时间:2023-12-02 08:05:19 24 4
gpt4 key购买 nike

所以我正在尝试为大学项目制作一款小游戏。我有一个加载图像等的图像类。我知道这个类是有效的,因为我在制作它时测试了它。但后来我决定使用表单生成器(在本例中为 WindowsBuilder Pro)来制作一个比我可以编码更好的表单。我现在尝试调用一个函数,理论上该函数将加载调用图像类并加载图像,然后将该图像作为图像图标添加到 jPanel 内的标签中。但我什么也没得到。有什么帮助吗?

    private void loadres(){
String PROGRAM_DIRECTORY = "E:/WordGame/bin/Images/";

Functions.Resources rs = new Functions.Resources();
rs.loadResources();
Functions.ImageLib iL = rs.getIL();

try {
BGImage = new JLabel(new ImageIcon(iL.mergeImages(iL.getImageArray(0),iL.getImage(PROGRAM_DIRECTORY + "Astroid1Image.png"))));
} catch (IOException e) {
e.printStackTrace();
}

BGImage.revalidate();
BGImage.repaint();
}

这是我正在使用的图像函数:

public class ImageLib {

private ArrayList<BufferedImage> BIArray = new ArrayList<BufferedImage>();

public ImageLib(){
}

public BufferedImage getImage(String ref){
BufferedImage Bi = null;

try{
Bi = ImageIO.read(new File(ref));
}catch (Exception e) {
e.printStackTrace();
}

return Bi;
}

public BufferedImage resizeImage(BufferedImage Bi, int nW, int nH){
int w = Bi.getWidth();
int h = Bi.getHeight();

BufferedImage nBi = new BufferedImage(nW, nH, Bi.getType());
Graphics2D g = nBi.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

g.drawImage(Bi,0,0,nW,nH,0,0,w,h,null);
g.dispose();

return nBi;
}

public void addToArray(BufferedImage img){
BIArray.add(img);
}

public BufferedImage getImageArray(int index){
return (BufferedImage) BIArray.get(index);
}

public BufferedImage mergeImages(BufferedImage I1, BufferedImage I2) throws IOException{
int w = Math.max(I1.getWidth(), I2.getWidth());
int h = Math.max(I1.getHeight(), I2.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

Graphics g = combined.getGraphics();
g.drawImage(I1, 0, 0, null);
g.drawImage(I2, 0, 0, null);
g.dispose();

return combined;
}

}

最佳答案

这是我的回答^^

ClassLoader cldr = this.getClass().getClassLoader(); 
java.net.URL imageURL = cldr.getResource("path/to/your/images/picture.gif");
ImageIcon imgIcon = new ImageIcon(imageURL);

关于java - 图像图标未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8277195/

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