0

所以我正在尝试为大学的一个项目制作一个小游戏。我有一个加载图像等的图像类。我知道这个类有效,因为我在制作它时对它进行了全部测试。但后来我决定使用表单制作器,在本例中是 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;
}

}

4

1 回答 1

2

这是我的答案^^

ClassLoader cldr = this.getClass().getClassLoader(); 
java.net.URL imageURL = cldr.getResource("path/to/your/images/picture.gif"); 
ImageIcon  imgIcon = new ImageIcon(imageURL);
于 2011-11-26T09:21:38.383 回答