图片的文件路径

时间:2016-12-30 23:02:49

标签: java image file

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

我试过了:

  1. 使用\\代替/
  2. 使用_代替空格
  3. 有人可以解释正确的格式以及我需要在路径中更改的内容吗?

    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)
    

1 个答案:

答案 0 :(得分:2)

您需要使用var x = JSON.stringify(r).replace(/,/g,"\n").replace(/"/g,"").replace(/_/g," ").replace("{",""). replace("}","").replace(/:/g,": "); console.log(x); 。那是因为\\用于指定转义序列。例如,\是换行符。 \n实际上为您提供了\\。因此,Java程序中的字符串\为您提供:"C:\\Users\\Julian Jacobs\\Pictures\\Saved Pictures\\spaceship\\spaceship.png"。您可以使用C:\Users\Julian Jacobs\Pictures\Saved Pictures\spaceship\spaceship.png

进行验证