0

I have this code:

package test;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SwingSandbox {

    public static void main(String[] args) throws IOException {
        JFrame frame = buildFrame();

        final BufferedImage image = ImageIO.read(new File("C:\\Projects\\Test\\src\\test\\InterestCalcGraphic.jpg"));

        JPanel pane = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, null);
            }
        };


        frame.add(pane);
    }


    private static JFrame buildFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
        return frame;
    }


}

When I run it, I get this error message:

    Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at test.SwingSandbox.main(SwingSandbox.java:17)

The error message leads me to believe that my file path is incorrect, but the image is stored under Test - src - test in my file directory. What am I doing wrong?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Ethan JC Rubik
  • 174
  • 1
  • 1
  • 12

3 Answers3

1

ImageIO hasn't even tried to load your image file. Something is wrong with your file itself. E.g. your path is incorrect. Try to call this

File f = new File("C:\\Projects\\Test\\src\\test\\InterestCalcGraphic.jpg");
boolean value = f.canRead();

What is boolean value of value variable? If it false check your image file path, file name typo etc.

Andriy Kryvtsun
  • 3,220
  • 3
  • 27
  • 41
  • It is false, but I can't figure out why. My file path seems to be correct. – Ethan JC Rubik Feb 25 '16 at 21:18
  • `at javax.imageio.ImageIO.read(ImageIO.java:1301)` seems to suggest otherwise – MadProgrammer Feb 25 '16 at 21:30
  • Try a different image. Also try using forward slashes. I think that works in java. C:/Projects/Test/... – ssimm Feb 25 '16 at 21:44
  • And try a shorter path, like an image sitting in C:/Projects – ssimm Feb 25 '16 at 21:45
  • @EthanJCRubik let's do this step-by-step: 1. move your image file into `C:\\Projects` as you already suggested. Try after this my 2 lines of code. 2. if problem isn't in the path try rename you image to e.g. `a.jpg` and try my 2 lines of code again. 3. let us know :) – Andriy Kryvtsun Feb 25 '16 at 21:52
  • I can't move an image file into projects, the best I can do is move it into projects/test. – Ethan JC Rubik Feb 25 '16 at 21:59
  • It returns false, even when I do this: – Ethan JC Rubik Feb 25 '16 at 22:01
  • `File f = new File("C:\\Projects\\Test\\a.jpg"); boolean value = f.canRead(); System.out.println(value);` – Ethan JC Rubik Feb 25 '16 at 22:01
  • @EthanJCRubik try to move `a.jpg` into `C:\\Projects` or in any another folder. It's only for your troubleshooting for getting `value == true`. Later, after full problem understanding, we'll think how fix it (locate your picture) better. BTW can you share somehow your code? E.g. at GitHub? – Andriy Kryvtsun Feb 25 '16 at 22:07
  • My mouse turns into a little x symbol when I try to drag the file into projects. I literally can't do it. I can only share my code on stackoverflow. – Ethan JC Rubik Feb 25 '16 at 22:10
  • I finally got it to return true by creating a new folder called graphics, and then doing this: `File f = new File("graphics/a.jpg"); boolean value = f.canRead(); System.out.println(value);` – Ethan JC Rubik Feb 25 '16 at 22:20
  • But here's the problem: now that I have the image stored as a bufferedImage, it still isn't showing up on my JPane. – Ethan JC Rubik Feb 25 '16 at 22:21
  • "How do I properly create a BufferedImage from file?" that s your answer dude; give the points and move on – gpasch Feb 25 '16 at 22:25
  • Nevermind, I just hadn't scaled the image properly. Thanks for all your help! (I would vote up your answer, but I just lost 2 reputation for this question being downvoted, and I can't upvote anymore.) – Ethan JC Rubik Feb 25 '16 at 22:25
1

I created a folder called graphics, and then used just the folder name in my path, nothing else. Like this:

final BufferedImage image = ImageIO.read(new File("graphics/a.jpg"));
Ethan JC Rubik
  • 174
  • 1
  • 1
  • 12
0

I think you want to read the file from the classpath instead.

final BufferedImage defaultImage =
    ImageIO.read(
        this.getClass().getResourceAsStream("/graphics/file.png"));

Place the file in src/main/resource/graphics. (for maven defaults)

KarlP
  • 5,149
  • 2
  • 28
  • 41