可能又是STATIC闯的货,请帮忙看看,请请大家了。
ztpia 2006-01-03 09:16:28 下面有程序A和B两个程序,主要功能都是读取一个名叫test.bmp的位图,并输出其高与宽。
在程序A中,通过调用静态方法getImage()得到了BufferedImage对象,能正确输出结果。
在程序B中,将BufferedImage 对象放到main中来得到,编译时,提示不能找到src对象,
我个人认为因为main是静态方法,里面调用了非静态的方法,但是就在这分析不下去了,请大家帮帮忙。
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
程序A:
public class IMGTest{
public static BufferedImage getImage(String filename){
BufferedImage bi = null;
try{
File _file = new File(filename);
bi = ImageIO.read(_file);
}catch(IOException e){
System.out.println(e);
}
return bi;
}
public static void main(String [] args){
BufferedImage src = IMGTest.getImage("test.bmp");
int wideth = src.getWidth();//得到源图宽
int height = src.getHeight();
System.out.println(wideth);
System.out.println(height);
}
}
程序B:
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
public class IMGTest{
public static void main(String [] args){
try{
File _file = new File("test.bmp");//读入文件
BufferedImage src = ImageIO.read(new File("test.bmp"));//构造Image对象
}catch(IOException e){
System.out.println(e);
}
int wideth = src.getWidth();//得到源图宽
int height = src.getHeight();
System.out.println(wideth);
System.out.println(height);
}
}