2

I am having issues with adding an Image to a screen, I have the following code:

  try {
    Image cima = Image.createImage("/resources/cross.png");
  } catch (IOException ex){
    System.out.println("ERROR");
  }

However it always hits up the exception. I don't know what's wrong as I already put in the image in the correct folder

enter image description here

Also would it be possible to combine two layout settings? Say I want top right corner? I want to combine two layout..is this possible?

UPDATE:

With regards to the layout I want to do something like:

form.append(new ImageItem("Top Right", cima, ImageItem.LAYOUT_TOP, null));

but this only gives me the image on TOP.

adit
  • 32,574
  • 72
  • 229
  • 373

2 Answers2

3

Obviously the URL is wrong. See my answer to this related question:

As for your second question, it is possible to combine multiple layout managers, although each container is limited to exactly one. For more information, see Laying Out Components Within a Container.

And what exactly do you want to set in the top right corner? I'm sure this can be done using a single layout manager.

As for placing the image in the top-right corner of the container, there are many ways to achieve this. One way is to simply use a JLabel as seen in the answer provided by @camickr in this question.

Community
  • 1
  • 1
mre
  • 43,520
  • 33
  • 120
  • 170
  • I tried Image cima = Image.createImage("../res/cross.png"); and it didn't work as well – adit Sep 09 '11 at 18:04
  • @adit, Are you talking about [this](http://download.oracle.com/javase/6/docs/api/java/awt/Image.html) Image class? If so, there is no `createImage` method. Also, you clearly didn't thoroughly read my example since that's not what I told you to do. – mre Sep 09 '11 at 18:06
  • I am talking about http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Image.html – adit Sep 09 '11 at 18:07
  • @adit, Oh well it's still a _path_ problem. Look at my sample code again. – mre Sep 09 '11 at 18:08
  • I saw you have a resources folder, I don't know how to create that in Eclipse – adit Sep 09 '11 at 18:13
  • now I did create a resource folder, tried everything starting from ../HW1.resources/cross.png and resources/cross.png, etc.. Nothing works – adit Sep 09 '11 at 18:31
  • 1
    @adit, Sorry, I've been busy. I see that you've accepted my answer. Did you finally get it working? – mre Sep 09 '11 at 22:01
1

The package isn't named "res", but "resources" as we can see from your snapshot.

Image cima = ImageIO.read(YourClass.class.getResource("/resources/cross.png"));

Change the catch body to this:

} catch (IOException ex){
  System.out.println("ERROR");
  ex.printStackTrace();
}

And tell us what exception you've got.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287