【发布时间】:2012-12-29 09:32:46
【问题描述】:
图片“Pic.jpg”最初位于"C:\Users\qwerty\Documents\NetBeansProjects\SelfTestX\src\Java\image"。
没有用。我阅读了getResource(),它实际上是从 .class 文件的存储位置读取的。
所以,我复制了同一张图片并粘贴到"C:\Users\qwerty\Documents\NetBeansProjects\SelfTestX\build\classes\Java\image"
它也没有用。
非常感谢任何帮助。
我在下面列出了基本代码。希望它足够短。
public void setImage() throws IOException{
URL img=getClass().getResource("image/Penguins3.jpg");
BufferedImage bi=ImageIO.read(img);
int w=bi.getWidth();
int h=bi.getHeight();
int count=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
BufferedImage wi=bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
Image sc=bi.getScaledInstance(puzpiece.getWidth()/3,
puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);
setupImage(count++,sc);
}
}
}
private void setupImage(int a,Image wi) {
button[a]=new JButton(new ImageIcon(wi));
}
【问题讨论】:
-
没用是什么意思?
-
图片加载失败。
-
哦,那我尝试删除更多代码。
-
基本上所有相关代码都是这一行:
ImageIO.read(getClass().getResource("image/Pic.jpg"));你可以删除其他所有内容。
标签: java getresource