Results 41 to 60 of 60
- 05-22-2011, 02:24 AM #41
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Did you use the ImageIcon constructor suggested before (#7)? ImageIcon requires a URL not a stream, so getResource() would seem the appropriate Class method to use.
-----
In fact the suggestion I made earlier about posting brief runnable code would make things a lot clearer. I realise that this means stepping away from your immediate problem to construct code that deals with resources (and nothing else). But such a detour is the most direct way of dealing with the specific aspect of your program that is causing problems.
I would recommend a simple class that initialises the sort of resources you are going to deal with (an image, an ImageIcon, an AudioClip etc) and then System.out.println()s them - and other information like image size - to see if they have been loaded OK without runtime error.
- 05-22-2011, 02:25 AM #42
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
- 05-22-2011, 02:30 AM #43
You could try the ImageIO class. It has a method that reads from an InputStream.
- 05-22-2011, 02:31 AM #44
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
now i load the images like this:
Image myImg = new ImageIcon ( getClass( ).getResource ( "filename" ) );
is that what you mean?
However, when i do that and create a jar file it wont even run inside the file consisting java files, images and audio. It doesnt work anywhere. whereas if i do without the getClass( ).getResource( ) it atleast works within the folder that has images and java files
- 05-22-2011, 02:37 AM #45
Have you checked that the path in the code and the path in the jar file match?when i do that and create a jar file it wont even run inside the file
Do you get execution errors?
- 05-22-2011, 02:46 AM #46
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Yes that's what I mean. (except that the ImageIcon constructor returns an ImageIcon not an Image, so, as written, it won't compile.)
Notice that this code assumes that filename is located along side the .class entry in the jar archive. You can see this with a little debugging code:
A two-step process is possible (although if you're after an icon rather than an image it's hard to see the benefit.)Java Code:URL url = getClass.getResource("filename"); System.out.println("The image for the icon is being looked for here: " + url); ImageIcon icon = new ImageIcon(url);
Or again, as has been mentioned, use the Class method to get the stream rather than the url and use the ImageIO method to read the image from that. (even more complicated, I think). But, in any case, things will go wrong if the image entry in the jar archive is not in the place you look for it. That is why actually checking the URL you are using is so important. Both getResource() and getResourceAsStream() allow for the resource to be located within the jar file. See the API docs for details, but a common idiom might beJava Code:URL url = getClass.getResource("filename"); System.out.println("The image for the icon is being looked for here: " + url); Image image = ImageIO.read(url); ImageIcon icon = new ImageIcon(url);
In this case the images (and other resources) are jarred into a folder so they can be easily located at runtime.Java Code:URL url = getClass.getResource([color=blue]"/resources/"[/color] + "filename"); System.out.println("The image for the icon is being looked for here: " + url); ImageIcon icon = new ImageIcon(url);
- 05-22-2011, 02:57 AM #47
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
Yes now i use this to load images
now i load to sounds asJava Code:Image myImage = ImageIO.read ( getClass( ).getResourceAsInputStream ( "filename" ) );
is this better?Java Code:private AudioInputStream audIn; private Clip clpMyBackgroundMusic; private URL flSoundURL; flSoundURL = getClass( ).getResource ( "BackgroundMusic.wav" ); audIn = AudioSystem.getAudioInputStream ( flSoundURL ); clpMyBackgroundMusic = AudioSystem.getClip ( ); clpMyBackgroundMusic.open ( audIn );
i tried using
but it gives me an error saying non static context cant be references from a static context.Java Code:Image myImage = ImageIO.read ( Class.getResourceAsInputStream ( "filename" ) );
are those ways equal? is it possible to use the first way?
UPDATE:
I did using this way and i made a jar file. i works inside the bluej folders (wherethere is java files jpgs etc.. ) AND OUTSIDE IN THE DESKTOP. however, there are not images - nothing. Its just a plane gray color jframe. I dont think i am loading the images correctly because in the plane JFrame the sounds are working. i can hear the background music.
(I am so happy, this is a big step for me since it works outside of the bluej files in the desktop. Finally something to motivate me )Last edited by rajkobie; 05-22-2011 at 03:01 AM.
- 05-22-2011, 03:05 AM #48
What class is the method: getResourceAsInputStream in? I can't find it.
- 05-22-2011, 03:19 AM #49
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
what? what do you mean.
I am sorry i made a typo up there. I get the static non static error when i say
Class.getResourceAsSteam ( .. )
- 05-22-2011, 03:28 AM #50
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
so i was trying combinations
i did this
i load images as
and sounds asJava Code:Image myImage = new ImageIcon ( filename ).getImage( );
now it works fine with images, sounds and everything inside the bluej folder and it works OUTSIDE the bluej folder without images. Sounds work fine.Java Code:private AudioInputStream audIn; private Clip clpMyBackgroundMusic; private URL flSoundURL; flSoundURL = getClass( ).getResource ( "BackgroundMusic.wav" ); audIn = AudioSystem.getAudioInputStream ( flSoundURL ); clpMyBackgroundMusic = AudioSystem.getClip ( ); clpMyBackgroundMusic.open ( audIn );
Outside is just sound and gray jframe
- 05-22-2011, 03:31 AM #51
Check the contents of the jar file and make sure the files are there and on the correct path.
- 05-22-2011, 03:37 AM #52
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
how do i check? I am so sorry
- 05-22-2011, 03:46 AM #53
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
oops sorry
I opened up my archieve using 7-zip
It has all the classes java files jpgs, pngs, wav files gif files and everything needed
however, when i clickon those. it says picture or video not selected
- 05-22-2011, 04:02 AM #54
Are the paths to the image files the same as they are referenced in the code?
Execute the java -jar command in a console window and see if there are any error messages.
- 05-22-2011, 06:40 AM #55
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
I get this error when i run
java myLast.jar in command prompt
Exception in thread "main" java.lang.NoClassDefFoundError: myLast/jar
Caused by: java.lang.ClassNotFoundException: myLast.jar
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: myLast.jar. Program will exit.
i dont understand
- 05-22-2011, 07:17 AM #56
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
The command to run the application is
assuming that file jar archive is called myLast.jar and that it is located in the same directory (folder) from where you run this command.Java Code:java -jar myLast.jar
For details, see the java man page.
- 05-22-2011, 07:43 AM #57
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
It doesnt give any errors. when i do that it just runs the jar file and gives a gray background with nothing on it.
- 05-22-2011, 07:52 AM #58
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
It doesnt even work in the bluej folder when i dont have
Java Code:Image myImage = new ImageIcon ( ... ).getImage( )
- 05-22-2011, 08:25 AM #59
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Then you need to change your code so that it does give some errors. See #41 and #46 above: small, runnable code which loads resources and does lots of println()ing about what its doing and what it gets as a result.
These "errors" (basically URLs that point to the wrong place, variables that end up null) are what you need. Just running the thing, observing that there are no images and concluding "it doesn't wrok" won't lead to any progress.
- 05-22-2011, 08:37 AM #60
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
thank you so much guys for ur help.
i fixed it.
I found the error. It is actually a very simple one
All i had to do was change all the images to:
instead ofJava Code:ImageIcon myImage = new ImageIcon ( getClass( ).getResource ( "filename" ) );
i worked ( atleast for the time being )Java Code:Image myImage = new ImageIcon ( getClass( ).getResource ( "filename" ) ).getImage( );
I bet u guys could have fixed it in less than 5 minutes if i posted for my code. Its my fault. thank you for the effort.
I will post if it doesnt work in the future
Similar Threads
-
Why isnt this working?
By GoingThroAPhase in forum New To JavaReplies: 4Last Post: 04-03-2010, 02:36 AM -
My rotate 2d pos method isnt working correctly..
By Addez in forum New To JavaReplies: 5Last Post: 12-01-2009, 09:04 AM -
The code isnt working unless I add print statements at diffrent points!:confused:
By Addez in forum New To JavaReplies: 6Last Post: 11-12-2009, 10:50 AM -
BlueJ arrow missing betwen two classes
By jboy in forum New To JavaReplies: 2Last Post: 10-29-2009, 12:42 PM -
Help with testing methods and classes in JUnit, using BlueJ
By Jonasse in forum New To JavaReplies: 1Last Post: 04-17-2008, 02:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks