home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / prmjatgt / objtank.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.4 KB  |  207 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.util.*;
  4. import java.io.*;
  5. import java.net.*;
  6.  
  7. /**
  8.  * A class where save draws and image
  9.  *
  10.  * @version     1.0 03/24/96
  11.  * @author         Kang, Dae Woong (namu@star.elim.net)
  12.  */
  13. class ObjTank // picture and drawings 
  14. {
  15.     Image image = null;   //picture
  16.     Vector obj = null;  //object
  17.  
  18.     int refCount = 0; // referenced Count
  19.     boolean isImage = false;
  20.     String path;      // object's relative location
  21.     Dimension dim = new Dimension(0, 0);
  22.  
  23.     //****************************  constructor  ********************************************
  24.     public ObjTank(String path, boolean isImage)
  25.     {
  26.         this.path = new String(path);
  27.         this.isImage = isImage;
  28.     }
  29.  
  30.     public ObjTank() // for read
  31.     {
  32.     }
  33.  
  34.     //****************************  read/write  *********************************************
  35.  
  36.     public void write(DataOutputStream dos) throws IOException
  37.     {
  38.         
  39.         try
  40.         { 
  41.             dos.writeByte(path.length());
  42.             dos.writeBytes(path);
  43.             dos.writeShort(dim.width);
  44.             dos.writeShort(dim.height);
  45.             dos.writeBoolean(isImage);
  46.         }
  47.         catch (IOException ioe)
  48.         {
  49.             throw ioe;
  50.         }
  51.     }
  52.  
  53.     public void read(DataInputStream dis, Applet applet) throws IOException
  54.     {
  55.         try
  56.         { 
  57.             int len = dis.readByte();
  58.             byte[] buf = new byte[len];
  59.             dis.readFully(buf);
  60.             path = new String(buf, 0);
  61.  
  62.             dim.width = dis.readShort();
  63.             dim.height = dis.readShort();
  64.             
  65.             isImage = dis.readBoolean();
  66.             if (isImage)
  67.                 image = applet.getImage(applet.getDocumentBase(), path);
  68.             else
  69.                 readChart(applet);
  70.         }
  71.         catch (IOException ioe)
  72.         {
  73.             throw ioe;
  74.         }
  75.     }
  76.     
  77.     private boolean readChart(Applet applet)
  78.     {
  79.         URL urlDocument = applet.getDocumentBase();
  80.         URL  url = null;
  81.  
  82.         try
  83.         {
  84.             url =  new URL(urlDocument, path);
  85.         }
  86.         catch (MalformedURLException e)
  87.         {
  88.             System.out.println(path + " document not found" + e);
  89.             return false;
  90.         }
  91.  
  92.         try
  93.         {
  94.             DataInputStream dis = new DataInputStream(url.openStream());
  95.  
  96.             int count;
  97.             int str_len; // string len
  98.             byte[] str_byte; // string buf
  99.  
  100.             Rectangle bound = new Rectangle(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
  101.  
  102.             obj = new Vector();
  103.  
  104.             if (WorkPanel.CHART_ID !=  dis.readShort())
  105.                 throw (new IOException("This is not Chart file"));
  106.  
  107.             if (WorkPanel.CHART_VERSION !=  dis.readShort())
  108.                 throw (new IOException("This is invalid version Chart file"));
  109.  
  110.             //comment
  111.             str_len = dis.readByte();
  112.             str_byte = new byte[str_len];
  113.             dis.readFully(str_byte);
  114.  
  115.             //skip dimension
  116.             dis.readShort();
  117.             dis.readShort();
  118.  
  119.             //skip object tank
  120.             count = dis.readShort();
  121.             for (int i = 0 ; i < count; i++)
  122.             {
  123.                 str_len = dis.readByte();
  124.                 str_byte = new byte[str_len];
  125.                 dis.readFully(str_byte);
  126.                 dis.readShort();
  127.                 dis.readShort();
  128.                 
  129.                 dis.readBoolean();
  130.             }
  131.  
  132.             //read draws
  133.             count = dis.readShort();
  134.             for (int i = 0 ; i < count; i++)
  135.             {
  136.                 Draw dd = new Draw();
  137.                 dd.read(dis, null);
  138.                 if (dd.method != dd.IMAGE && dd.method != dd.DRAW)
  139.                 {
  140.                     bound.x = Math.min(bound.x, dd.x);
  141.                     bound.y = Math.min(bound.y, dd.y);
  142.                     bound.width = Math.max(bound.width, dd.x + dd.width);
  143.                     bound.height = Math.max(bound.height, dd.y + dd.height);
  144.                     obj.addElement(dd);
  145.                 }
  146.             }
  147.             dim.width = bound.width - bound.x + 1;
  148.             dim.height = bound.height - bound.y + 1;
  149.  
  150.             Enumeration enum = obj.elements();
  151.             while (enum.hasMoreElements())
  152.                 ((Draw) enum.nextElement()).transelate(-bound.x, -bound.y);
  153.  
  154.         }
  155.         catch (IOException ioeRead)
  156.         {
  157.             System.out.println(ioeRead);
  158.             return false;
  159.         }
  160.         return true;
  161.     }
  162.  
  163.     //***************************************************************************************
  164.     public void refContent(Draw draw)
  165.     {
  166.         draw.objTank = this;
  167.         draw.width = dim.width;
  168.         draw.height = dim.height;
  169.         refCount++;
  170.     }
  171.  
  172.     synchronized boolean setContent(Applet applet)
  173.     {
  174.         if (isImage)
  175.         {
  176.             image = applet.getImage(applet.getDocumentBase(), path);
  177.             while ((dim.width = image.getWidth(applet)) < 0) {
  178.                 try
  179.                 {
  180.                     wait(100);
  181.                 }
  182.                 catch (InterruptedException e)
  183.                 {
  184.                     return false;
  185.                 }
  186.             }
  187.             while ((dim.height = image.getHeight(applet)) < 0) {
  188.                 try
  189.                 {
  190.                     wait(100);
  191.                 }
  192.                 catch (InterruptedException e)
  193.                 {
  194.                     return false;
  195.                 }
  196.             }
  197.         }
  198.         else //OBJECT
  199.         {
  200.             if (readChart(applet) == false)
  201.                 return false;
  202.         }
  203.  
  204.         return true;
  205.     }
  206. }
  207.