Sprites wouldnt load

Hello,

Here are the class files DrawImage.java and Board.java which are supposed to load jvm.png , if not is ther another way of loading sprites with JFrame or java.applet.Applet

package gms;

import java.awt.EventQueue;
import javax.swing.JFrame;

public class DrawImage extends JFrame {

public DrawImage() {

    initUI();
}

private void initUI() {

    add(new Board());

    pack();

    setTitle("uc");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
}

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            DrawImage c = new DrawImage();
            c.setVisible(true);
        }
    });
}

}

Board.java

package gms;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Board extends JPanel {

private Image uc;

public Board() {

    initBoard();
}

private void initBoard() {
    
    loadImage();
    
    int w = uc.getWidth(this);
    int h =  uc.getHeight(this);
    setPreferredSize(new Dimension(w, h));        
}

private void loadImage() {
    
    ImageIcon ii = new ImageIcon("jvm.png");
    uc = ii.getImage();        
}

@Override
public void paintComponent(Graphics g) {

    g.drawImage(uc, 0, 0, null);
}

}

Thanks

Okay, I’m not entirely sure if I understand your question correctly, half of it appears to be a statement, and the other half a question of sorts.

It seems that you’re asking if you are loading the images correctly, am I correct?
If that is what you are asking, then the answer is “not really”. Let’s look at your “loadImage” method.


private void loadImage() {
    ImageIcon ii = new ImageIcon("jvm.png");
    uc = ii.getImage();        
}

There are a few problems with this.

  1. Where is this image located? Is it just sitting randomly in your code base, or is it in a resources folder?
  2. You’re loading the image as an Image Icon, and then grabbing the image from that.

Let’s look at these one at a time.

The code you use to reference your image might work for now, and load your image, but it won’t work for long. I did this when I was first learning Java and this was the source of never ending confusion. From an organization stand point, you really ought to place resources in their own directory (“res/image” for images or “res/localization” for localization files, for example). But either way, you need to provide the full path to the image. Let’s say that you place your image in a “res/image” directory. In this case, you need to turn “jvm.png” into…


getClass().getResourceAsStream("image/jvm.png")

Now some people will say that you should use the ImageIcon class, grab the Image from the ImageIcon, and pass that onto a BufferedImage. I don’t believe this is necessary at all, but I’ll show what that is like as well. Either way, if you wind up using BufferedImage, you’ll want to change uc’s type from an Image to a BufferedImage.

Still using ImageIcon:


private void loadImage() {
    ImageIcon ii = new ImageIcon(getClass().getResourceAsStream("image/jvm.png"));
    uc = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);    
    Graphics2D bGr = uc.createGraphics();
    bGr.drawImage(ii.getImage(), 0, 0, null);
    bGr.dispose();
}

Not using ImageIcon:


private void loadImage() {
    uc = ImageIO.read(getClass().getResourceAsStream("image/jvm.png"));
}

Hopefully that makes sense.

thanks for the detailed reply, changed the file path good now