user2264734
user2264734

Reputation: 25

Loading image in java

I am trying to load an image using the following code, but no image comes up only the window is shown...

public class loadImg extends Component
{
public Image loadImageFile()
{
    Image i = null;
    Toolkit tk = Toolkit.getDefaultToolkit();
    i = tk.getImage("abcd.jpg");
    waitFrImg(i);
    return(i);
}
private void waitFrImg(Image a)
{
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(a, 1);
    try {
        mt.waitForAll();
    }
    catch(Exception e) {
        //e.printStackTrace();
        //System.exit(1);
        System.out.println("Loading of the image was interrupted" );
        System.exit(0);
    }
}
}

Upvotes: 0

Views: 426

Answers (3)

Charkey
Charkey

Reputation: 1

The problem is your image path.

I test your code with Absolute Path, and it works.

By the way, rewrite the JPanel to suit your needs.


public class MyJPanel extends JPanel {

    private Image image;

    public MyJPanel() {
    }

    public MyJPanel(Image image) {
        this.image = image;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
    }
}

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209102

Using this this "abcd.jpg" relative path, without loading from the class, your file structure needs to look something like this (if you're running the program from an IDE like NetBeans or Eclipse).

ProjectRoot
         adcd.jpg
         src 
         bin

Where the IDE will first search for the file in the root directory. This scenario is not good for production though. In a testing environment, it's fine.


NOTE

As @AndrewThompson noted "By the time of deployment, those resources will likely become an embedded-resource. That being the case, the resource must be accessed by URL instead of File.". In which case you would want to use getClass().getResource() which returns a URL

tk.getImage(getClass().getResource("resources/abcd.jpg"));

What you would need to is right click on your src folder and create new package. Call that package resources. Then just copy and paste your image into that package. The package will get copied into the build

---- To display the image you need to paint it on to the component, say a JPanel and override the paintComponent method, something like this

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class TestImage extends JPanel{
    private static final int SCREEN_WIDTH = 256;
    BufferedImage img;

    public TestImage(){
        try {
            img = ImageIO.read(getClass().getResource("resources/stack_reverse.png"));
        } catch (IOException ex) {
            System.err.println("Could not load image");
        }
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
    }

    public Dimension getPreferredSize() {
        return new Dimension(SCREEN_WIDTH, SCREEN_WIDTH);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestImage());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }
}

enter image description here

Upvotes: 0

Thudani Hettimulla
Thudani Hettimulla

Reputation: 774

what MediaTracker does is tracing the status of a image list. To view the image on the screen either you need to paint it on a component or add it to a component such as a JLabel.

Upvotes: 1

Related Questions