1

So I have a project on image segmentation to complete, and the first stage is to get Java to display an image.

The problem I'm having is that I can get a window to appear, however the image I have loaded does not get rendered.

If I am doing this completely wrong then please then me know. I've spent the afternoon looking for clear explanations on handling images with Java however I haven't found any good, clear resources.

I have two classes at the moment: the main class, and the image loading class.

This is my main class:

import javax.swing.*;

public class LoadImageMain extends JFrame {

public static void main(String[] args) {            
    displayImage("HelloWorld.png");
}

public static void displayImage(String path) {
    JFrame frame = new JFrame("Display Image");
    LoadImage panel = new LoadImage(path);

    frame.setSize(1200, 800);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setContentPane(panel);
    frame.setVisible(true);
}   
}

My second class which is meant to render the image:

public class LoadImage extends JPanel {
   private Image img;

public LoadImage(String path) {
    img = getImage(path);       
}

@Override
public void paintComponent(Graphics g) {    
    super.paintComponent(g);
    g.drawImage(img, 0, 0, null);       
}

public Image getImage(String path) {
    Image tempImg = null;

    try {
        tempImg = Toolkit.getDefaultToolkit().getImage(path);
    }
    catch (Exception e) {
        System.out.println("Image not found. Error: " + e.getMessage());
    }

    return tempImg;
}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Alex Neves
  • 616
  • 1
  • 7
  • 16
  • I hope this [answer](http://stackoverflow.com/a/11372350/1057230), will be able to sort thingies out for you, though if you using any IDE or want to know more, have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), which contains a detailed description of what needs to be done. Hope it helps :-) – nIcE cOw Oct 11 '13 at 14:46
  • 3
    Don't reinvent the wheel. Just use a JLabel. Read the Swing tutorial on [How to Use Icons](http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html) for a working example. `I've spent the afternoon looking for clear explanations on handling images with Java however I haven't found any good, clear resources` - the tutorial has the basics for all Swing components. Keep a link to the tutorial handy. – camickr Oct 11 '13 at 14:49
  • @nIcEcOw The second answer you posted solved my problem, thank you! If I use ImageIO.read() then the image is loaded correctly. – Alex Neves Oct 11 '13 at 16:27
  • 1
    @Alex : Glad the answer did helped you in some way, though the answer related to `ImageObserver` thingy is also worth noting, as it serves as a means of notifying the `JPanel` (in this case) about the newly loaded part of the image (Though using ImageIO, it seems it is not that important a part to play on). For the rest You're MOST WELCOME and KEEP SMILING :-) – nIcE cOw Oct 11 '13 at 16:29

2 Answers2

4

As I see you didn't define observer for drawImage.

Instead:

g.drawImage(img, 0, 0, null);  

set

g.drawImage(img, 0, 0, this);
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
2

I think the image is not loaded. Check tempImg's width/height. Guess they are 0.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • @haraldK : The way the OP is accessing images is, something I'll keep myself away from. Instead I will use `javax.imageio.ImageIO.read` for this sort of a job at hand (as it will wait for the image to get loaded before proceeding), though `ImageObserver` is in some way the right thingy to do, though it is not the only reason, in my opinion. – nIcE cOw Oct 11 '13 at 14:57
  • @nIcEcOw I was on my way to edit the comment. ;-) Using `ImageIO` or even a `JLabel` like camickr suggests are better options IMO too, just not the exact fix for the problem in the OP's Q. My point was more that this answer contains no attempt at a solution and should be a comment. – Harald K Oct 11 '13 at 15:05
  • @haraldK : LOL :-) Too true, never seen this thingy in that line. Now a days I am mostly into commenting then answering question, so every answer does appears to me as an answer, though YOU ARE RIGHT :-) in pointing out that this should rather be a comment. – nIcE cOw Oct 11 '13 at 15:10
  • @StanislavL You're correct, the image wasn't loaded. The height/width were coming out as -1. – Alex Neves Oct 11 '13 at 16:28