Question

So I am trying to make a small game for a project in college. I have an image class that loads the images etc. I know this class works because I tested it all when I made it. But then I decided to use a form maker, in this case WindowsBuilder Pro to make a form that was better then I could code. I am now trying to call a function that in theory will load call the images class and load the image then add that image as an imageicon to a label within a jPanel. But I am getting nothing. Any help?

    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();
}

And here is the Image functions I am using:

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;
}

}

Was it helpful?

Solution

Here is my answer ^^

ClassLoader cldr = this.getClass().getClassLoader(); 
java.net.URL imageURL = cldr.getResource("path/to/your/images/picture.gif"); 
ImageIcon  imgIcon = new ImageIcon(imageURL);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top