我的图片没有显示在我的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)
答案 0 :(得分:1)
我不明白getImage()方法是什么,但通常你会导入这样的图像:
try {
globe = ImageIO.read(new URL("URL_OF_FILE.png"));
} catch (IOException e) {
e.printStackTrace();
}
答案 1 :(得分:1)
Image globe = getImage (getCodeBase(), "relative/path/to/globe.jpg");
对于applet应该可以正常工作。如果applet与HTML位于同一目录中,则更容易。
Image globe = getImage (getDocumentBase(), "globe.jpg");