Ashwin Gupta
Ashwin Gupta

Reputation: 2197

Where do I put images for java program to input?

I've been following a tutorial to learn graphics and in one program the author uses images to make texture paints. I have copied his code however I dont know where to actually put the images for it to read. I have tried making a resources folder in eclipse and setting it as a source folder build path but this didnt work. The code is below:

EDIT: Okay, I figured out that it is taking images from the source of the class. However, lets say I wanted to pull an image from my desktop, or some other location on my hard drive, how would i do this?

class Surface extends JPanel {

    private BufferedImage slate;
    private BufferedImage java;
    private BufferedImage pane;
    private TexturePaint slatetp;
    private TexturePaint javatp;
    private TexturePaint panetp;

    public Surface() {

        loadImages();
    }

    private void loadImages() {

        try {

            slate = ImageIO.read(new File("slate.png"));
            java = ImageIO.read(new File("java.png"));
            pane = ImageIO.read(new File("pane.png"));



 } catch (IOException ex) {

        Logger.getLogger(Surface.class.getName()).log(
                Level.SEVERE, null, ex);
    }
}

private void doDrawing(Graphics g) {

    Graphics2D g2d = (Graphics2D) g.create();

    slatetp = new TexturePaint(slate, new Rectangle(0, 0, 90, 60));
    javatp = new TexturePaint(java, new Rectangle(0, 0, 90, 60));
    panetp = new TexturePaint(pane, new Rectangle(0, 0, 90, 60));

    g2d.setPaint(slatetp);
    g2d.fillRect(10, 15, 90, 60);

    g2d.setPaint(javatp);
    g2d.fillRect(130, 15, 90, 60);

    g2d.setPaint(panetp);
    g2d.fillRect(250, 15, 90, 60);

    g2d.dispose();
}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doDrawing(g);
}

}

Upvotes: 1

Views: 607

Answers (1)

magic-sudo
magic-sudo

Reputation: 1246

This can be helpful.
Or just use absolute PATH to file. linux: /home/user/... widndows: C:/Users/..

Upvotes: 3

Related Questions