简体   繁体   中英

Java Netbeans: Bufferedimage can't read input file

I'm trying to make a class which sets an image as its variable and tells the main class what image it is, then the main class paints it. Basically have an amount of objects that print themselves while they exist. I achieved this using ImageIcon but BufferedImage seems better for drawing many images at a time or images that move and so on, so I'd like to avoid using ImageIcon . Main class:

package handBasket;

import java.awt.*;
import javax.swing.*;
public class AppStarter  extends JPanel
{
static ImageDisplayerTest idt;
public void paintComponent(Graphics g)
{
    g.drawImage(idt.demoPann, 0, 0, null);
}
public static void main(String[] args)
{        
    JFrame frame = new JFrame();
    frame.setSize(800,600);
    frame.getContentPane().add(new AppStarter());
    frame.setVisible(true);
    frame.setTitle("The Great Graphical Achievement");
    frame.setResizable(false);
    int w = frame.getSize().width;
    int h = frame.getSize().height;
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;
    frame.setLocation(x,y);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    idt = new ImageDisplayerTest();
    System.out.println(idt.demoPann);
}
}

This all worked fine while the image was an ImageIcon . As for the object that has the image to be drawn, it creates a File, reads the filename in to that file and tries to pass that file to the BufferedImage but that's where I get the NullPointerException saying that it cannot read the file. Tried with multiple files and filetypes, no change.

package handBasket;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ImageDisplayerTest
{
BufferedImage demoPann;
File loadThis;
public ImageDisplayerTest()
        {
            loadThis = new File("hp_small.jpg");
            try 
            {
            demoPann = ImageIO.read(loadThis); 
            } 
            catch (IOException e) 
            {
            System.out.println("wtf");
            }
        }
}

Any suggestions and advice appreciated.

-----Edit:

When doing printStackTrace() I get the following lines, note that some of these are displayed anyway so I'm not sure what printStackTrace() is supposed to do.

As for folder structure, everything is in the src/handBasket folder which is in the folder that is contained by my projects folder.

run:
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at handBasket.ImageDisplayerTest.<init>(ImageDisplayerTest.java:17)
    at handBasket.AppStarter.main(AppStarter.java:31)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at handBasket.AppStarter.paintComponent(AppStarter.java:12)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    at javax.swing.JComponent.paint(JComponent.java:1040)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    at java.awt.Container.paint(Container.java:1967)
    at java.awt.Window.paint(Window.java:3877)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 15 seconds)

You need to reference the image in relation to the classpath. This usually involves fetching it's URI as a resource:

URI imageurl = getClass().getResource("hp_small.jpg").toURI();

So for your test above, something like:

public ImageDisplayedTest() {
    try {
        URI imageurl = getClass().getResource("hp_small.jpg").toURI();
        loadThis = new File(imageurl);

        demoPann = ImageIO.read(loadThis);
        System.out.println("Success");
    } catch (IOException e) {
        System.out.println(e);
    } catch (URISyntaxException e) {
        System.out.println(e);
    }
}

Should work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM