Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (710)
Games in Android Showcase (212)
games submitted by our members
Games in WIP (784)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Res Folder  (Read 1660 times)
0 Members and 1 Guest are viewing this topic.
Offline ISeeTheTruth

Senior Newbie

« Posted 2013-07-07 05:24:28 »

Hello I have really just started Coding in Java a few weeks ago and I thought my first project would be an MP3Player. Then code more and more to get a better grasp. One question I would like to ask is how do I like setup my res folder in my project, then load pics from it for my JFrame's background or a buttons background? Thanks for any help guys. I have my JFrame's background set and my custom buttons but I can't jar it for friends to test until I get this problem fixed.
Offline Z-Man
« Reply #1 - Posted 2013-07-07 05:33:34 »

It has a little bit to do with how your project is setup in whatever IDE you're using. It also has to do with how you're loading the images, as some methods do not work in jar files. However as long as the resources are in your classpath (and thus included in your jar file) something like this should load the image both in a jar and simply running it from the class files.
BufferedImage img ="path/to/image.png");

I set up most of my projects in Eclipse using a top level folder named src that contains a folder for code and a folder for resources. That way both the code and resources get included in the classpath and jar file. Roughly like this:
src (code)
Offline ISeeTheTruth

Senior Newbie

« Reply #2 - Posted 2013-07-07 05:36:19 »

Can I make a whole seperate class for all these images, then call them into my JFrames class? Like

Close.setIcon(new ImageIcon("C:/Users/Tony/Desktop/res/Title Bar/close/button_CLOSE_DEMO.png"));

this is my JButton's default look. Is there anyway I can just write

Close.setIcon(new ImageIcon("/res/button_CLOSE_DEMO.png"));

and have it load from my res like that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-07-07 05:40:33 »

add the "res" folder to the build path (properties->java build path->add folder) and use

URL url = this.getClass().getResource("/button_CLOSE_DEMO.png");
Close.setIcon(new ImageIcon( url );

Offline ISeeTheTruth

Senior Newbie

« Reply #4 - Posted 2013-07-07 05:49:05 »

"Cannot use this in a static context" :/
Offline Troncoso

JGO Coder

Medals: 20

« Reply #5 - Posted 2013-07-07 05:50:43 »

You are trying to use it in a static method. If you want to do that do

URL url = <Class name>.class.getResource("/button_CLOSE_DEMO.png");
Close.setIcon(new ImageIcon( url );

Where <Class name> is the name of the class that this code is in.
Offline sproingie

JGO Kernel

Medals: 202

« Reply #6 - Posted 2013-07-07 05:53:07 »

Then don't use it in a static context.  If you want to get anywhere, you have to learn what error messages like that mean: go through the java tutorials, learn about OOP, learn what 'this' is and when you can use it.  Don't just react helplessly or randomly to error messages like they're something out of your control.

Offline ISeeTheTruth

Senior Newbie

« Reply #7 - Posted 2013-07-07 08:15:19 »

Thank you for your help. You guys have fixed it for me! Oh also, I know. I don't approve of other people helping my troubles but I'm VERY new to Java. Anyway, guys I have one more question. I made my own custom button Layout:

      JButton Close = new JButton();
      Close.setIcon(new ImageIcon(bCD));
      Close.setSelectedIcon(new ImageIcon(bCCD));
      Close.setDisabledIcon(new ImageIcon(bCH));
      Close.setDisabledSelectedIcon(new ImageIcon(bCH));
      Close.setPressedIcon(new ImageIcon(bCCD));
      Close.setRolloverIcon(new ImageIcon(bCH));
      Close.setRolloverSelectedIcon(new ImageIcon(bCH));

and when I go to another Class for a PatchNotes GUI, I try the same code and the


code comes with "The method "isDefaultLookAndFeelDecorated() is undefined for the type PatchNotesGUI. Any ideas?"
Offline Mac70
« Reply #8 - Posted 2013-07-07 11:28:27 »

Use built-in GUI designer in your IDE - it will make your life much easier.

If you really have to program this manually, make sure that "isDefaultLookAndFeelDecorated()" is defined in your class (any IDE should mark this as error if it is not). I would suggest using true or false instead.

I would suggest reading this article - it should be useful for you. Wink
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

numerical (45 views)
2017-02-21 07:32:16

numerical (44 views)
2017-02-21 07:31:46

theagentd (151 views)
2017-02-18 13:42:33

theagentd (154 views)
2017-02-18 13:35:16

h.pernpeintner (1318 views)
2017-01-24 22:39:11

h.pernpeintner (1306 views)
2017-01-24 22:38:32

Galdo (1866 views)
2017-01-12 13:44:09

Archive (1960 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2499 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2306 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!