Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 484 times)
0 Members and 1 Guest are viewing this topic.
Offline ISeeTheTruth

Senior Newbie





« Posted 2013-07-07 07: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 07: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.
1  
BufferedImage img = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("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:
1  
2  
3  
4  
src
-org.zman.game (code)
-resources
--images
Offline ISeeTheTruth

Senior Newbie





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

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

1  
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

1  
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 07:40:33 »

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

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

Offline ISeeTheTruth

Senior Newbie





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

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

JGO Coder


Medals: 20



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

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

1  
2  
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
« Reply #6 - Posted 2013-07-07 07: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 10: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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
      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));
      Close.setBorderPainted(isDefaultLookAndFeelDecorated());


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

1  
BUTTONHERE.setBorderPainted(isDefaultLookAndFeelDecorated());


code comes with "The method "isDefaultLookAndFeelDecorated() is undefined for the type PatchNotesGUI. Any ideas?"
Offline Mac70
« Reply #8 - Posted 2013-07-07 13: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

Check out my Devblog! Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org 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‑gaming.org
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!