扩展canvas的类时paint方法出错

时间:2017-01-27 19:27:38

标签: java swing canvas nullpointerexception desktop

有人可以帮我吗?我已经完成了这个

  

线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException   在Frame.Painter.paint(Main.java:399)at   sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)at   sun.awt.RepaintArea.paint(RepaintArea.java:240)at   sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:358)at at   java.awt.Component.dispatchEventImpl(Component.java:4965)at   java.awt.Component.dispatchEvent(Component.java:4711)at   java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)at at   java.awt.EventQueue.access $ 500(EventQueue.java:97)at   java.awt.EventQueue $ 3.run(EventQueue.java:709)at   java.awt.EventQueue $ 3.run(EventQueue.java:703)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)   在   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)   在java.awt.EventQueue $ 4.run(EventQueue.java:731)at   java.awt.EventQueue $ 4.run(EventQueue.java:729)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)   在java.awt.EventQueue.dispatchEvent(EventQueue.java:728)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)   在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)   在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)   在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)   在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)   在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

并且第399行中的代码是:

class Painter extends Canvas{
    Image image;
    private URL url;
    public void setImage(String file) {
        url = null;
        try {
            url = new File(file).toURI().toURL();
        } catch (MalformedURLException ex) {
            System.out.println(ex.toString());
        }
        image = getToolkit().getImage(url);
        repaint();
    }
    public void paint(Graphics g) {
        double d = image.getHeight(this) / this.getHeight();
        double w = image.getWidth(this) / d;
        double x = this.getWidth() / 2 - w / 2;
        g.drawImage(image, (int) x, 0, (int) (w), this.getHeight(), this);
    }
}

我不确切知道那里发生了什么。我的程序工作正常,但每次运行都会对此错误感到不安。请告诉我该怎么做。

1 个答案:

答案 0 :(得分:-2)

我假设你有以下代码:

class Painter extends Canvas{
    Image image;
    private URL url;
    public void setImage(String file) {
        image = Toolkit.getDefaultToolkit().getImage(file);
        repaint();
    }
    public void paint(Graphics g) {
      if(image!=null) {
        double d = image.getHeight(this) / this.getHeight();
        double w = image.getWidth(this) / d;
        double x = this.getWidth() / 2 - w / 2;
        g.drawImage(image, (int) x, 0, (int) (w), this.getHeight(), this);
      }
    }
}

然后你只需要调用setImage(“c:/ yourfilepath / yourfilename”)