Image files not displaying

Hi there, so i've just downloaded the intelliJ IDE and i have an app which is working fine in DrJAVA which i've just switched from after realising it was kinda garbage.

I have a very simple java app which needs to display a PNG image. Currently the image is located in the same directory as my source code in the project directory. When i run the app the image is not displayed.

It displays fine when run with DrJava i have not tried it in netbeans yet although that is what i will do next, but surely there must be some easier fix for this than changing ide's...a plugin or setting or something like that?

as a test i've already copied some example code From Oracle which does the same thing as my app, and this example code still has the same issue.--the image will not display/is not painted.

6 comments
Comment actions Permalink

oh and il add that i am pretty sure this is because the variable which i'm storing the image in is not being initialised, which  means it could be the path name but im pretty sure its not.

0
Comment actions Permalink

Please make sure:

1. The image is copied to output path when compiling the project.
2. The Working directory field in the run configuration you start has proper value.

Regards,
Alexander.

0
Comment actions Permalink

Hi Alexander thanks for the reply.

The images are being copied to the output directory, they are present under the 'out" folder hierachy and im also sure that the working directory in the run configuration is correct also. The App does compile and run successfully but the image is not displayed.

heres the Oracle example code which replicates the issue:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class LoadImageApp extends Component {

    BufferedImage img;

    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null);
    }

    public LoadImageApp() {
        try {
            img = ImageIO.read(new File("fist.jpg"));
        } catch (IOException e) {
        }

    }

    public Dimension getPreferredSize() {
        if (img == null) {
            return new Dimension(100,100);
        } else {
            return new Dimension(img.getWidth(null), img.getHeight(null));
        }
    }

    public static void main(String[] args) {

        JFrame f = new JFrame("Load Image Sample");

        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        f.add(new LoadImageApp());
        f.pack();
        f.setVisible(true);
    }
}

0
Comment actions Permalink

Got it. Relative path in File(String) constructor is applied to working directory. This way, the application should work, if the image file path is actually in .../working-directory/fist.jpg.

If the file path is actually .../working-directory/source-root/fist.jpg, then you have to refer to it as "new File("source-root/fist.jpg");" For me it works.

Making catch clause not empty could help to analyze the problem.

Also please notice this is not a question on IDEA functionality.

Regards,
Alexander.

0
Comment actions Permalink

Also notice that you can use different path to load file, e.g. .../output-directory/fist.jpg, or use different way to load the resource. I just don't insist on any particular way.

Regards,
Alexander.

0
Comment actions Permalink

Solved thanks Alexander :) , not an IDE issue but i thought it might have been.

0

Please sign in to leave a comment.