我的意思是drawImage()方法对我不起作用。人们说要做的事情
g.drawImage(image, 0, 0, null);
但是日食实际上否认了它。它说:
Graphics类型中的drawImage(Image,int,int,ImageObserver)方法不适用于参数(Image,int,int,null)
这实际上让我很困惑,我已经从其他人那里读到了一些答案,他们总是说这些答案是空的。 我试图运行它,它给了我这个:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The method drawImage(Image, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (Image, int, int, null)
at me.danielshe.graphics.DrawComponent.paintComponent(DrawComponent.java:29)
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.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._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(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)
任何人都可以帮助我,或者这是一种放置东西而不是“null”的方法吗?我试图把“这个”,甚至实现ImageObserver。但它们都不起作用。
感谢。
答案 0 :(得分:0)
您需要一个实现ImageObserver接口的对象。一般 这将是与图形对象相关联的组件。
答案 1 :(得分:0)
使用null作为ImageObserver没错。等效于此:
ImageObserver obs = null;
drawImage(img,x,y,obs);
在可以使用引用(对象)的任何地方都可以使用Null。当您需要使用此引用/对象时,就会出现问题,因为对象为空,因此您将获得运行时异常。在您的示例中,只要您在调用drawImage()时图像已完全加载,就不会有问题,因此,如果您确定图像已完全加载,则只应在drawImage()上使用null。现在我在编译器上还没有遇到这个问题,但是我没有使用eclipse,只是使用了文本编辑器和命令行。尝试使用命令行进行编译,或者在Eclipse上使用上面两行代码。 仅当您使用drawImage的类实现了ImageObserver接口或扩展了实现该类的类(例如,例如Jcomponent)时,“ this”才起作用。
致谢