繁体   English   中英

将图像添加到Java Applet

[英]Adding Image to Java Applet

我的图像没有显示在我的applet上有问题,这里是代码,所有都与图像有关;

 Image globe;

//Adding Image in init()
Image globe = getImage (getCodeBase (), "C:/Users/Andrew/Downloads/Computer Science/globe.jpg");
//later in code

 public void paint (Graphics g)
    {
        g.drawString ("'The Best Travel Agency in the world' - John Travelta", 400, 675);
        g.drawImage(globe, 0, 100, this);
    } // paint method

    public boolean action (Event e, Object o)
    {
        if (e.target == DomRep)
        {
            String DomRepBox = JOptionPane.showInputDialog ("Please Enter your name: ");
        }
        return true;
    }

这是我收到的错误;

java.lang.NullPointerException
    at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
    at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
    at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
    at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
    at CPT.paint(CPT.java:129)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)

我不明白getImage()方法是什么,但通常你导入这样的图像:

try {
    globe = ImageIO.read(new URL("URL_OF_FILE.png"));
} catch (IOException e) {
    e.printStackTrace();
}
Image globe = getImage (getCodeBase(), "relative/path/to/globe.jpg");

应该适用于小程序。 如果applet与HTML位于同一目录中,则更容易。

Image globe = getImage (getDocumentBase(), "globe.jpg");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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