在使用awt的image时候,不是能时时获取到图像的宽和高,
GetWidth()函数偶尔得到的值为-1,暂时没有找到解决方法。
代码:
public class picture extends JFrame { ImageIcon icon = new ImageIcon(); Image image;// =Toolkit.getDefaultToolkit().getImage("D:\DataSet\CNN\YoloV1\hunt.jpg"); picture( String imagepath ) {//constructor image = Toolkit.getDefaultToolkit().getImage(imagepath); //ImageObserver obj = new ImageObserver(); int h =image.getHeight( null );//-1 int w =image.getWidth( null ); System.out.print("h:");System.out.print(h);System.out.printf(" "); System.out.print("w:");System.out.print(w);System.out.printf(" "); this.setSize( 800,480 ); this.setSize( w,h ); icon.setImage( image ); JButton button = new JButton(); button.setIcon(icon); this.getContentPane().add(button); this.setVisible(true); } picture( String imagepath ,int w, int h) {//constructor image = Toolkit.getDefaultToolkit().getImage(imagepath); //ImageObserver obj = new ImageObserver(); //int h =image.getHeight( null );//-1 //int w =image.getWidth( null ); System.out.print("h:");System.out.print(h);System.out.printf(" "); System.out.print("w:");System.out.print(w);System.out.printf(" "); //this.setSize( 800,480 ); this.setSize( w,h ); icon.setImage( image ); JButton button = new JButton(); button.setIcon(icon); this.getContentPane().add(button); this.setVisible(true); } }
使用方法:
使用初始化直接载入图像尽心一次显示。怎么刷新呢?