我试过,
int height = img.getHeight(null);
int width = img.getWidth(null);但返回的是-1,你知道怎么回事吗?

解决方案 »

  1.   

    import javax.swing.*;
    import java.awt.*;public class beyond_xiruo extends JFrame {
    public beyond_xiruo() throws Exception {
        Image img=this.getToolkit().getImage("image.jpg");
        java.awt.MediaTracker mt=new java.awt.MediaTracker(this);
          mt.addImage(img,0);
          mt.waitForAll();
        int width=img.getWidth(null);
        int height=img.getHeight(null);
        System.out.print(width+"*"+height);
    }public static void main(String args[]) {
    try{new beyond_xiruo();}catch(Exception e){System.out.print(e.toString());}
    }
    }
      

  2.   

    ImageObserver再AWT中是一个叫做“图像观察者”的借口,
    而在AWT中,每一个构件都是这个“图像观察者”。
    所以在要取得长和宽,只要用getWidth(this)和getHeight(this)
    就行了!这个“图像观察者”也有一个方法叫做
    public boolean imageUpdate(Image,int,int,int,int,int)的方法!
    他是可以返回图像产生的进展情况的,具体的你去查查吧!good luck!!
      

  3.   

    主要是这里,装载完图片再处理
        java.awt.MediaTracker mt=new java.awt.MediaTracker(this);
          mt.addImage(img,0);
          mt.waitForAll();
    给分
      

  4.   

    我没在构件中使用image对象
    我是从网上下载了一附图存在本地
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image inImg = toolkit.getImage(tempFile.getAbsolutePath());
    int height = img.getHeight(this);
    int width = img.getWidth(this);
      

  5.   

    我是implements ImageObserver
    然后重载了imageUpdate方法
      

  6.   

    use MediaTracker
    http://java.sun.com/docs/books/tutorial/uiswing/painting/loadingImages.html
    http://java.sun.com/j2se/1.4/docs/api/java/awt/MediaTracker.html
      

  7.   

    谢谢zhou1977(短笛)得提示,不过没代码,害我又去写了老半天。所以给60其它人我也谢谢了一人分一点哈哈!!!!真爽!!!