FileDocCategorySizeDatePackage
ImageSizer.javaAPI DocExample4650Thu Apr 03 15:17:22 BST 1997None

ImageSizer

public class ImageSizer extends Frame

Fields Summary
TextField
theURL
TextArea
theOutput
Button
getURL
Constructors Summary
public ImageSizer()

  
    setLayout(new BorderLayout());
    theURL = new TextField(40);
    add("North", theURL);
    theOutput = new TextArea(80, 40);
    add("Center", theOutput);
    add("South", new Button("Get URL"));
  
  
Methods Summary
public booleanaction(java.awt.Event e, java.lang.Object o)

    if (e.target instanceof Button) {
      fixThePage(theURL.getText());
      return true;
    }
    return false;
  
  
public voidfixThePage(java.lang.String thePage)


    String thisLine;
    URL root;

    if  (thePage != null) {

      //Open the URL for reading
      try {
        if (thePage.indexOf(":") != -1) {
          root = new URL(thePage);
        }
        else {
          root = new URL("http://" + thePage);
        }
        sizeAPage(root);
      }
      catch (MalformedURLException e) {
        System.err.println(thePage + " is not a parseable URL");
        System.err.println(e);
      }
    } //  end if

  
public booleanimageUpdate(java.awt.Image img, int infoflags, int x, int y, int width, int height)

    if ((infoflags & ImageObserver.HEIGHT) == ImageObserver.HEIGHT  &&
        (infoflags & ImageObserver.WIDTH) == ImageObserver.WIDTH)  {
        return false;
    }
    else {
      return true;
    }
  
  
public static voidmain(java.lang.String[] args)

  
    ImageSizer is = new ImageSizer();
    is.resize(300, 200);
    is.show();
  
  
public java.lang.StringreadTag(java.io.DataInputStream is)

  
    StringBuffer theTag = new StringBuffer("<");
    char theChar = '<";
  
    try {
       while (theChar != '>") {
         theChar = (char) is.readByte();
         theTag.append(theChar);
       } // end while
     }  // end try
     catch (EOFException e) {
       // Done with the Stream
     }
     catch (Exception e) {
        System.err.println(e);
     }     

     return theTag.toString();
  
  
public voidsizeAPage(java.net.URL u)

  
   char thisChar;
   String theTag;
  
   try {
     DataInputStream theHTML = new DataInputStream(u.openStream());
     try {
       while (true) {
         thisChar = (char) theHTML.readByte();
         if (thisChar == '<") {
           theTag = readTag(theHTML);
           if (theTag.startsWith("<IMG")) {
             theTag = sizeImage(u, theTag);
           }
           theOutput.appendText(theTag);
         }
         else {
           theOutput.appendText(String.valueOf(thisChar));
         }
       } // end while 
     }  // end try
     catch (EOFException e) {
       // Done with the Stream
     }
   }  // end try
   catch (IOException e) {
     System.err.println(e);
   }

  
public java.lang.StringsizeImage(java.net.URL u, java.lang.String tag)

  
    String s1 = tag.toUpperCase();

    boolean hasHeightTag = s1.indexOf("HEIGHT") != -1;
    boolean hasWidthTag = s1.indexOf("WIDTH") != -1;

    if (hasHeightTag && hasWidthTag) {
      return tag;
    }
    else {
      String newTag;
      Image thePicture;
      int p1, p2, p3, p4;
      p1 = s1.indexOf("SRC");
      p2 = s1.indexOf ("=", p1);
      p3 = s1.indexOf("\"", p2);
      p4 = s1.indexOf("\"", p3+1);
      String theURL = tag.substring(p3+1, p4);
      URL thePictureURL;
      try {
        if (theURL.indexOf(":")  == -1) {
          // it's not an absolute URL
          thePictureURL = new URL(u, theURL);
        }  // end if   
        else {
          thePictureURL = new URL(theURL);
        }
        Toolkit t = Toolkit.getDefaultToolkit();
        thePicture = t.getImage(thePictureURL);
        thePicture.getHeight(this);
        int last = tag.indexOf(">");
        newTag = tag.substring(0,last);
        if (!hasWidthTag) {
          while (thePicture.getWidth(this) == -1) {
            try {
              Thread.currentThread().sleep(100);
            }
            catch (InterruptedException e) {
            }
          }
          newTag = newTag + " width=" + thePicture.getWidth(this);
        }
        if (!hasHeightTag) {
          while (thePicture.getHeight(this) == -1) {
            try {
              Thread.currentThread().sleep(100);
            }
            catch (InterruptedException e) {
            }
          }
          newTag = newTag + " height=" + thePicture.getHeight(this);
        }
        newTag = newTag +  ">";
      }
      catch (MalformedURLException e) {
        newTag = tag;
      }
      return newTag;    
    }