Page 3 of 3 FirstFirst 123
Results 41 to 60 of 60
  1. #41
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by rajkobie View Post
    I tried to load images and sound using class.getResourceAsStream ( ... ) but i cant put it into an imageicon and draw on the screen.

    How should i fix this?
    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.

  2. #42
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Again, are the sound files and images in the jar file? Is the folder that holds them in the jar file?
    There is no folder that holds sound and images and they are all in one big folder

  3. #43
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    You could try the ImageIO class. It has a method that reads from an InputStream.

  4. #44
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    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.
    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

  5. #45
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    when i do that and create a jar file it wont even run inside the file
    Have you checked that the path in the code and the path in the jar file match?
    Do you get execution errors?

  6. #46
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by rajkobie View Post
    now i load the images like this:

    Image myImg = new ImageIcon ( getClass( ).getResource ( "filename" ) );

    is that what you mean?
    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:

    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);
    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);
    Image image = ImageIO.read(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 be

    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);
    In this case the images (and other resources) are jarred into a folder so they can be easily located at runtime.

  7. #47
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    Yes now i use this to load images

    Java Code:
    Image myImage = ImageIO.read ( getClass( ).getResourceAsInputStream ( "filename" ) );
    now i load to sounds as

    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 );
    is this better?

    i tried using


    Java Code:
    Image myImage = ImageIO.read ( Class.getResourceAsInputStream ( "filename" ) );
    but it gives me an error saying non static context cant be references from a static context.

    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.

  8. #48
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    What class is the method: getResourceAsInputStream in? I can't find it.

  9. #49
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    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 ( .. )

  10. #50
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    so i was trying combinations

    i did this

    i load images as

    Java Code:
    Image myImage = new ImageIcon ( filename ).getImage( );
    and sounds as

    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 );
    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.

    Outside is just sound and gray jframe

  11. #51
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Check the contents of the jar file and make sure the files are there and on the correct path.

  12. #52
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    how do i check? I am so sorry

  13. #53
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    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

  14. #54
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.

  15. #55
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    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

  16. #56
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The command to run the application is

    Java Code:
    java -jar myLast.jar
    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.

    For details, see the java man page.

  17. #57
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    It doesnt give any errors. when i do that it just runs the jar file and gives a gray background with nothing on it.

  18. #58
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    It doesnt even work in the bluej folder when i dont have

    Java Code:
    Image myImage = new ImageIcon ( ... ).getImage( )

  19. #59
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by rajkobie View Post
    It doesnt give any errors.
    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.

  20. #60
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    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:

    Java Code:
    ImageIcon myImage = new ImageIcon ( getClass( ).getResource ( "filename" ) );
    instead of

    Java Code:
    Image myImage = new ImageIcon ( getClass( ).getResource ( "filename" ) ).getImage( );
    i worked ( atleast for the time being )

    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

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Why isnt this working?
    By GoingThroAPhase in forum New To Java
    Replies: 4
    Last Post: 04-03-2010, 02:36 AM
  2. My rotate 2d pos method isnt working correctly..
    By Addez in forum New To Java
    Replies: 5
    Last Post: 12-01-2009, 09:04 AM
  3. Replies: 6
    Last Post: 11-12-2009, 10:50 AM
  4. BlueJ arrow missing betwen two classes
    By jboy in forum New To Java
    Replies: 2
    Last Post: 10-29-2009, 12:42 PM
  5. Replies: 1
    Last Post: 04-17-2008, 02:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •