1

My textures have only been working in Eclipse but when I try to export it as a runnable jar ill start it up and there is no textures. I made a resources folder and connected it to the project by making it a class folder but it only works in eclipse. this is how I usually access the textures.

Image something;
public Image getsomethingImg(){

    ImageIcon s=new ImageIcon("res/something.png");
    something=s.getImage();
    return something;
}

Then i draw it.

g2d.drawImage(getsomethingImg(), 0, 0, null);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You should check locations of your textures after build/deploy. It might be worth reviewing those paths, as they may be different then in Eclipse project. "res/..." could work in Eclipse as a base path will be project root, but running project outside might need full path like "C:\...." – FazoM Jul 26 '13 at 09:46
  • 1) `g2d.drawImage(getsomethingImg(), 0, 0, null);` should (in all likelihood) be `g2d.drawImage(getsomethingImg(), 0, 0, this);`. 2) Don't try to load images in the paint methods! 3) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jul 26 '13 at 10:01
  • 1
    Please have a look at this answer, [adding resources to project](http://stackoverflow.com/a/9866659/1057230). Hope it will help you :-) – nIcE cOw Jul 26 '13 at 10:07

1 Answers1

1

How to includes all images in jar file using eclipse

Try this next.

ImageIcon s = new ImageIcon(getClass().getResource("res/something.png"));
Community
  • 1
  • 1
Jayson
  • 940
  • 8
  • 14
  • 1
    It is safer to use `ImageIO` to read images since if the path is wrong, it will throw informative exceptions. – Andrew Thompson Jul 26 '13 at 10:01
  • this is a nooby question but how would I write that? like in a class I keep getting errors. – liam speakman Jul 26 '13 at 10:16
  • @liamspeakman : Please have a look at this [answer](http://stackoverflow.com/a/11372350/1057230), that might can give you this idea :-) Moreover, do not keep the `ImageObserver` part as `null`, in your `g2d.drawImage(getsomethingImage(), 0, 0, compReferenceOnWhichYouDrawing)` – nIcE cOw Jul 26 '13 at 11:57
  • compreference? like what class,what main draw method am i using,or something else? sorry if this a stupid question I am only about 5 months into java coding. – liam speakman Jul 27 '13 at 07:30