Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Understanding Images

    So i've been trying to make a basic class to draw an image onscreen, and given that i could never make it work i had assumed that the issue was with the code until i coded the absolute path to the image resource and then it worked just fine. That being the case i think my issue is with how to utilize resources that have been imported into the project file. What i did was to go into the properties of the image and then copy/paste the path into the parameters of an ImageIcon. I can't understand for the life of my why this doesn't work and the absolute path does. Any thoughts?

    Note my main method does nothing but call this. I'm using Eclipse so i can just drag and drop resources into the project. I've tried the path both with and without the preceding '/'. I've also tried both using and not using a sub-folder to house the files. Am i missing something? Thanks in advance.
    Java Code:
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    
    import java.awt.*;
    
    public class Screen extends JFrame {
    
    	Image img;
    	ImageIcon i = new ImageIcon("ImageTests/images/SpriteSheet.png");
    	
    	public Screen(){
    		this.setSize(640, 480);
    		this.setTitle("My UI");
    		this.setVisible(true);
    	}
    	
    	public void paint(Graphics g){
    		g.drawRect(10, 10, 50, 50);
    		img = i.getImage();
    		g.drawImage(img, 100, 100, null);
    		
    	}
    }
    Last edited by TheUncertainyAnswer; 07-28-2013 at 10:55 PM.

  2. #2
    AlexHail is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: Understanding Images

    Hi. Try using the awt Toolkit to retrieve images from the local file system.

    Toolkit (Java Platform SE 7 )

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Understanding Images

    No, that won't help; you need to have the correct path to the resource.

    Go through Loading Images Using getResource

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 11-14-2012, 11:15 AM
  2. Need some understanding and help!
    By Kevinius in forum New To Java
    Replies: 8
    Last Post: 05-14-2011, 05:50 AM
  3. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  4. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  5. Help on understanding a program
    By newbie225 in forum New To Java
    Replies: 1
    Last Post: 11-10-2009, 12:53 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
  •