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

    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();
    gui.run();
    }
    public void run(){

    JFrame f = new JFrame();
    f.setSize(600,600);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    NewPanel panel = new NewPanel();

    f.add(panel);

    }

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

    }

  2. #2
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

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

    Default

    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, 07: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
  •