Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Add Image to JPanel

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Add Image to JPanel

    What is the best way to simply add an image in the local directory to a JPanel? The JPanel fills the entire JFrame.

    I have been trying multiple methods and none of them have worked. I, for some reason, just can't get an image to be displayed.

    Again I am looking for a simple way to briefly display a large .jpg image filling the whole JFrame.

    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Add Image to JPanel

    Override the JPanel's paintComponent method and draw the image there.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Add Image to JPanel

    Yeah I got that to work but do you know of a way to make a general path identification for a file (not system specific) or better yet, a way to include a file (as in picture) in a package?

  4. #4
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Add Image to JPanel

    look at the format here and see if this helps you out.
    [java-swing-how-to-add-an-image-to-a-jpanel

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Add Image to JPanel

    a way to include a file (as in picture) in a package?
    Not sure what you mean by package? Are you referring to a jar file?
    Your first post stated: image in the local directory

  6. #6
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Add Image to JPanel

    Quote Originally Posted by Norm View Post
    Not sure what you mean by package? Are you referring to a jar file?
    Your first post stated: image in the local directory
    Right now the path has to be specified exactly like so:
    //code back here...
    Image iScreen = new ImageIcon("C:\\Java Workspace\\JavaCalcIntroScreen.jpg");

    I just want to tell java to look in the local directory for the .jpg file or, when a jar is made with all the classes, have it find the jpeg in the jar (I would store the jpeg in the jar with the class files).

    The only reason I mentioned packages is because I've seen a lot of source code where the programmer had pictures inside of the jar within folders that the program used. I don't know if those count as packages or not... doesn't matter. That would be great if I knew how to do that.

    Clarification:
    -I DON'T want the JVM trying to find the file based on a specific path for my system only.
    -I WANT the JVM to either look in the local directory for the file, or, most ideally, look for it within the executable jar.
    -Alternatively, I would certainly accept a simple method of typing a file name that specifies the local location (i.e. some sort of wildcard or something like in batch files when you use %USERPROFILE%\<rest of path>

    Thanks again!

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Add Image to JPanel

    I haven't checked back on my previous programs (I will if this doesn't work), but I seem to remember doing this:
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("yourFileInJarOrLocalPath.file");

    Since jars are compressed folders (essentially), you have to access files within them with Streams instead of standard file IO. I believe the above thing will work the same for local files.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. The Following User Says Thank You to aussiemcgr For This Useful Post:

    bgroenks96 (June 16th, 2011)

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Add Image to JPanel

    In addition to Norm's suggestion (which is probably the way I would go), you could also add the Icon to a JLabel and add that to the JPanel.

    Recommended reading: How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) That also contains some information on loading an icon.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Add Image to JPanel

    Quote Originally Posted by aussiemcgr View Post
    I haven't checked back on my previous programs (I will if this doesn't work), but I seem to remember doing this:
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("yourFileInJarOrLocalPath.file");

    Since jars are compressed folders (essentially), you have to access files within them with Streams instead of standard file IO. I believe the above thing will work the same for local files.
    Should I return that to a BufferedImage variable?

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Add Image to JPanel

    You need to find a constructor or method that takes an input stream that is reading an image and returns an image object of some kind.

  12. #11
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Add Image to JPanel

    Quote Originally Posted by Norm View Post
    You need to find a constructor or method that takes an input stream that is reading an image and returns an image object of some kind.
    Hmm. Would that be in javax.imageio maybe? I will go check.

Similar Threads

  1. Save JPanel as image
    By fahien in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2011, 08:20 AM
  2. [SOLVED] Can't Import AWT Image into custom JPanel
    By JavaSquared in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2011, 12:59 AM
  3. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 07:28 PM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM