home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 4.4 KB | 207 lines |
- import java.awt.*;
- import java.applet.*;
- import java.util.*;
- import java.io.*;
- import java.net.*;
-
- /**
- * A class where save draws and image
- *
- * @version 1.0 03/24/96
- * @author Kang, Dae Woong (namu@star.elim.net)
- */
- class ObjTank // picture and drawings
- {
- Image image = null; //picture
- Vector obj = null; //object
-
- int refCount = 0; // referenced Count
- boolean isImage = false;
- String path; // object's relative location
- Dimension dim = new Dimension(0, 0);
-
- //**************************** constructor ********************************************
- public ObjTank(String path, boolean isImage)
- {
- this.path = new String(path);
- this.isImage = isImage;
- }
-
- public ObjTank() // for read
- {
- }
-
- //**************************** read/write *********************************************
-
- public void write(DataOutputStream dos) throws IOException
- {
-
- try
- {
- dos.writeByte(path.length());
- dos.writeBytes(path);
- dos.writeShort(dim.width);
- dos.writeShort(dim.height);
- dos.writeBoolean(isImage);
- }
- catch (IOException ioe)
- {
- throw ioe;
- }
- }
-
- public void read(DataInputStream dis, Applet applet) throws IOException
- {
- try
- {
- int len = dis.readByte();
- byte[] buf = new byte[len];
- dis.readFully(buf);
- path = new String(buf, 0);
-
- dim.width = dis.readShort();
- dim.height = dis.readShort();
-
- isImage = dis.readBoolean();
- if (isImage)
- image = applet.getImage(applet.getDocumentBase(), path);
- else
- readChart(applet);
- }
- catch (IOException ioe)
- {
- throw ioe;
- }
- }
-
- private boolean readChart(Applet applet)
- {
- URL urlDocument = applet.getDocumentBase();
- URL url = null;
-
- try
- {
- url = new URL(urlDocument, path);
- }
- catch (MalformedURLException e)
- {
- System.out.println(path + " document not found" + e);
- return false;
- }
-
- try
- {
- DataInputStream dis = new DataInputStream(url.openStream());
-
- int count;
- int str_len; // string len
- byte[] str_byte; // string buf
-
- Rectangle bound = new Rectangle(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
-
- obj = new Vector();
-
- if (WorkPanel.CHART_ID != dis.readShort())
- throw (new IOException("This is not Chart file"));
-
- if (WorkPanel.CHART_VERSION != dis.readShort())
- throw (new IOException("This is invalid version Chart file"));
-
- //comment
- str_len = dis.readByte();
- str_byte = new byte[str_len];
- dis.readFully(str_byte);
-
- //skip dimension
- dis.readShort();
- dis.readShort();
-
- //skip object tank
- count = dis.readShort();
- for (int i = 0 ; i < count; i++)
- {
- str_len = dis.readByte();
- str_byte = new byte[str_len];
- dis.readFully(str_byte);
- dis.readShort();
- dis.readShort();
-
- dis.readBoolean();
- }
-
- //read draws
- count = dis.readShort();
- for (int i = 0 ; i < count; i++)
- {
- Draw dd = new Draw();
- dd.read(dis, null);
- if (dd.method != dd.IMAGE && dd.method != dd.DRAW)
- {
- bound.x = Math.min(bound.x, dd.x);
- bound.y = Math.min(bound.y, dd.y);
- bound.width = Math.max(bound.width, dd.x + dd.width);
- bound.height = Math.max(bound.height, dd.y + dd.height);
- obj.addElement(dd);
- }
- }
- dim.width = bound.width - bound.x + 1;
- dim.height = bound.height - bound.y + 1;
-
- Enumeration enum = obj.elements();
- while (enum.hasMoreElements())
- ((Draw) enum.nextElement()).transelate(-bound.x, -bound.y);
-
- }
- catch (IOException ioeRead)
- {
- System.out.println(ioeRead);
- return false;
- }
- return true;
- }
-
- //***************************************************************************************
- public void refContent(Draw draw)
- {
- draw.objTank = this;
- draw.width = dim.width;
- draw.height = dim.height;
- refCount++;
- }
-
- synchronized boolean setContent(Applet applet)
- {
- if (isImage)
- {
- image = applet.getImage(applet.getDocumentBase(), path);
- while ((dim.width = image.getWidth(applet)) < 0) {
- try
- {
- wait(100);
- }
- catch (InterruptedException e)
- {
- return false;
- }
- }
- while ((dim.height = image.getHeight(applet)) < 0) {
- try
- {
- wait(100);
- }
- catch (InterruptedException e)
- {
- return false;
- }
- }
- }
- else //OBJECT
- {
- if (readChart(applet) == false)
- return false;
- }
-
- return true;
- }
- }
-