anon
anon

Reputation:

How to place image in source folder and use it in Eclipse

I am teaching myself how to use images in Eclipse.

I am trying to get my code implementing images to compile:

    import java.awt.*;
import javax.swing.JFrame;
import java.io.File;
import javax.imageio.ImageIO;

public class ImageDemo extends Canvas
{
    Image coolFace;

    public ImageDemo() throws Exception
    {
        coolFace = ImageIO.read( new File("jesus.png") );

    }

    public void paint( Graphics g )
    {
        //           Image  , x,  y, this
        g.drawImage(coolFace,100,100,this);

        g.setColor( Color.yellow );
        g.drawOval(88,88,70,25);
    }

    public static void main(String[] args) throws Exception
    {
        JFrame win = new JFrame("Image Demo");
        win.setSize(1024,768);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.add( new ImageDemo() );
        win.setVisible(true);
    }


}

The problem is that I do not know how to get my image in the same source folder of my code in Eclipse.

I think I created one but I do not know how to put an image from my Desktop there.

Any help would be appreciated. Eclipse

Upvotes: 0

Views: 1489

Answers (1)

Jigar Joshi
Jigar Joshi

Reputation: 240996

Generally eclipse starts jvm with current directory from base of the project so place it on base of the project to read it with existing code

Upvotes: 3

Related Questions