0

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);
    }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • And where exactly do you display the image? – Obicere Jan 07 '14 at 07:30
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. 3) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jan 07 '14 at 07:34

3 Answers3

1

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.

Thudani Hettimulla
  • 754
  • 1
  • 12
  • 32
0

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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

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);
    }
}
Charkey
  • 1
  • 1