Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Rep Power

    Default first time GUI'r... trying to reference jpg

    this is my first time trying to get an image onto a JFrame... not sure where exactly to save my jpg to in my source folder, and how exactly to reference it in my ImageIcon constructor in order to get to it... any help?

    the package7/arnold-deadlift.jpg is my attempt to get to a picture called "arnold... .jpg" which is in package 7, in my src folder, in my java project... if that helps at all on what that folder hierarchy looks like

    import javax.swing.*;
    import java.awt.*;
    public class Gui{
    private Image icon;

    public static void main(String[]args){
    Gui gui = new Gui();;
    public void run(){

    JFrame f = new JFrame();

    NewPanel panel = new NewPanel();



    public class NewPanel extends JPanel{
    public void paintComponent(Graphics g){
    icon = new ImageIcon("package7/arnold-deadlift.jpg").getImage();


  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power

  3. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Amsterdam, the Netherlands
    Rep Power


    First: use code-tags to make your code readable.

    Supposing you use Eclipse and ant:
    Where you save your jpg isn't that important.
    - If you place it under your src-directory it is copied by Eclipse to your bin-tree when building the project.
    - You can also place it in a parallel directory to src and bin, recources for example. Then you must remember to reference it in your ant-script when making a jar.

    You can use a small diagnostic method like
    Java Code:
    	public static void testFile( String filePathAndName){
    		File file =new File( filePathAndName);
    		String path = file.getAbsolutePath();
    		System.out.println( "File: " + filePathAndName + "\n" + "AbsPath: " + path );
    (Remove the "static" if you don't use it from a lib but from the class itself)
    If you make an ImageIcon (or some other resource) you can call this with the same path to see where the program is looking for it.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. reference issue
    By Stephen Douglas in forum New To Java
    Replies: 29
    Last Post: 07-04-2010, 05:12 PM
  3. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  4. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM
  5. creating a reference to a value in a map
    By kellykellykelly in forum Advanced Java
    Replies: 1
    Last Post: 02-28-2009, 08:59 AM

Posting Permissions

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