Java 图像的文件路径

Java 图像的文件路径,java,image,file,Java,Image,File,我知道我所有的代码都是正确的,除了路径。我一直在使用C:/Users/Julian Jacobs/Pictures/Saved Pictures/spaceship/spaceship.png 我试过: 使用\\而不是/ 使用u而不是空格 有人能解释一下正确的格式以及我需要在路径中更改什么吗 import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; imp

我知道我所有的代码都是正确的,除了路径。我一直在使用C:/Users/Julian Jacobs/Pictures/Saved Pictures/spaceship/spaceship.png

我试过:


使用\\而不是/ 使用u而不是空格 有人能解释一下正确的格式以及我需要在路径中更改什么吗

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Content extends JPanel {
int radius = 50;
private Image spaceship;
    public Content() {
        super.setDoubleBuffered(true);
    }

    public void paintComponent(Graphics g){
        ImageIcon Ship = new ImageIcon(this.getClass().getResource("C:/Users/Julian Jacobs/Pictures/Saved Pictures/spaceship/spaceship.png"));
        spaceship = Ship.getImage();
        Graphics2D g2d= (Graphics2D)g;
        g2d.drawImage(spaceship, 100, 100, this);
        g2d.setColor(Color.DARK_GRAY );
        g2d.fillOval(100, 100, radius, radius);

    }
}
引发异常:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at Content.paintComponent(Content.java:17)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1200(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

您需要使用\\。这是因为\用于指定转义序列。例如\n是新行。\\实际上给了你一个\。因此,Java程序中的字符串C:\\Users\\Julian Jacobs\\Pictures\\Saved Pictures\\spaceship\\spaceship.png为您提供了:C:\Users\Julian Jacobs\Pictures\Saved Pictures\spaceship\spaceship.png。您可以使用System.out.printlnC:\\Users\\Julian Jacobs\\Pictures\\Saved Pictures\\spaceship\\spaceship.png

\\n而不是/问题是什么,您无法访问该文件吗?是,它必须是地址。你能显示你正在使用的代码吗?加载文件?如果你要提供图像的完整路径,那么只需使用:ImageIcon Ship=new ImageIconC:/Users/Julian Jacobs/Pictures/Saved Pictures/spaceship/spaceship.png;因为它显然不是一种资源。@JulianJacobs你试过了,但它不起作用,但它仍然是正确的,因为问题不在路径上。这正确地回答了有关如何设置路径格式的问题。