发布于2022-12-05 23:34 阅读(808) 评论(0) 点赞(4) 收藏(4)
我有一个图像displayStartText
并originalStartText
因此初始化:
Image originalStartText = new ImageIcon(getClass().getClassLoader().getResource( "GAME/resources/ready_set_fight.gif" )).getImage();
Image displayStartText = originalStartText;
我尝试使用以下方法绘制以我的背景为中心的 displayStartText:
g2d.drawImage( displayStartText, (int)((bgDispWidth-displayStartText.getWidth())/2), (int)((bgDispHeight-displayStartText.getHeight())/2), null );
但是出现了一个我不明白的编译器错误
Gameplay.java:694: error: method getWidth in class Image cannot be applied to given types;
g2d.drawImage( displayStartText, (int)((bgDispWidth-displayStartText.getWidth())/2), (int)((bgDispHeight-displayStartText.getHeight())/2), null );
^
required: ImageObserver
found: no arguments
reason: actual and formal argument lists differ in length
关于为什么我可能会遇到这个问题的任何见解?
Image
没有getWidth()
方法,如果你看一下JavaDocs你会发现它需要一个参数ImageObserver
,因为当你请求值时图像可能没有加载(因为它的工作方式),你可以使用一个ImageObserver
是当数据可用时通知。
所有 Swing 组件都实现了 ,因此如果该类是基于 Swing 的对象或者您没有ImageObserver
,您可以(理论上)使用。this
null
如果您正在使用null
,那么您应该确保首先加载图像,例如使用MediaTracker
,否则可能getWidth(ImageObsever)
会返回-1
更好的解决方案(恕我直言)是使用ImageIO.read
which 将返回一个BufferedImage
which is gurenteed to be loaded when it returns 并为您节省一些这些问题。
查看阅读/加载图像以获取更多详细信息
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/583881/44555ce6efac3c5c875e/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!