所以我试图在大学里为一个项目制作一个小游戏。我有一个图像类加载图像等我知道这个类的作品,因为我测试了它,当我做到了。但后来我决定使用一个表单制造商,在这种情况下,WindowsBuilder Pro制作的表单更好,然后我就可以编写代码。我现在试图调用一个函数,理论上它会加载调用图像类并加载图像,然后将该图像作为图像添加到jPanel中的标签。但我什么都没有。任何帮助?ImageIcon无法加载
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;
}
}
可能有助于告诉你,布局框架/面板是grouplayout,并且BGImage是我在WYSIWUG编辑器WBP中添加的标签。该标签的宽度为501,高度为250或接近。 – Tempus35
将你的图片放到你的src文件夹中,试试这个^^: ClassLoader cldr = this.getClass()。getClassLoader(); java.net.URL imageURL = cldr.getResource(“path/to/your/images/picture.gif”); ImageIcon imgIcon = new ImageIcon(imageURL); – hungneox
请学习java命名约定并坚持使用它们 – kleopatra