home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 May / PCO_5_97.ISO / FilesBBS / OS2 / CSIME.ARJ / CSIME.ZIP / csime / Poly.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-02-09  |  3.6 KB  |  187 lines

  1. import java.awt.Graphics;
  2. import java.awt.Polygon;
  3. import java.util.NoSuchElementException;
  4. import java.util.StringTokenizer;
  5.  
  6. public class Poly extends Area {
  7.    private static final int MINDIST = 8;
  8.    private int _npts;
  9.    private int[] _xpt = new int[16];
  10.    private int[] _ypt = new int[16];
  11.    private int _grabIndex;
  12.  
  13.    public Poly(int var1, int var2) {
  14.       this.addPoint(var1, var2);
  15.       this._grabIndex = this._npts;
  16.    }
  17.  
  18.    public Poly(String var1) {
  19.       StringTokenizer var2 = new StringTokenizer(var1, ", \t\n");
  20.  
  21.       try {
  22.          while(var2.hasMoreTokens()) {
  23.             int var3 = Integer.parseInt(var2.nextToken());
  24.             int var4 = Integer.parseInt(var2.nextToken());
  25.             this.addPoint(var3, var4);
  26.          }
  27.       } catch (NoSuchElementException var5) {
  28.       } catch (NumberFormatException var6) {
  29.       }
  30.  
  31.       this.close();
  32.    }
  33.  
  34.    private Poly() {
  35.    }
  36.  
  37.    public void resize(int var1, int var2) {
  38.       if (this._grabIndex == this._npts) {
  39.          this.addPoint(var1, var2);
  40.       } else if (this._grabIndex == 0) {
  41.          this.setPoint(0, var1, var2);
  42.          this.setPoint(this._npts - 1, var1, var2);
  43.       } else if (this._grabIndex == this._npts - 1) {
  44.          if (Math.abs(var1 - this._xpt[0]) <= 8 && Math.abs(var2 - this._ypt[0]) <= 8) {
  45.             this.setPoint(this._npts - 1, this._xpt[0], this._ypt[0]);
  46.          } else {
  47.             this.setPoint(this._npts - 1, var1, var2);
  48.          }
  49.       } else {
  50.          this.setPoint(this._grabIndex, var1, var2);
  51.       }
  52.    }
  53.  
  54.    public void translate(int var1, int var2) {
  55.       for(int var3 = 0; var3 < this._npts; ++var3) {
  56.          int[] var10000 = this._xpt;
  57.          var10000[var3] += var1;
  58.          var10000 = this._ypt;
  59.          var10000[var3] += var2;
  60.       }
  61.  
  62.    }
  63.  
  64.    public boolean isGrab(int var1, int var2) {
  65.       for(int var3 = 0; var3 < this._npts - 1; ++var3) {
  66.          if (((Area)this).grabPoint(this._xpt[var3], this._ypt[var3], var1, var2)) {
  67.             this._grabIndex = var3;
  68.             return true;
  69.          }
  70.       }
  71.  
  72.       return false;
  73.    }
  74.  
  75.    public boolean isSelect(int var1, int var2) {
  76.       for(int var3 = 0; var3 < this._npts - 1; ++var3) {
  77.          if (((Area)this).grabLine(this._xpt[var3], this._ypt[var3], this._xpt[var3 + 1], this._ypt[var3 + 1], var1, var2)) {
  78.             return true;
  79.          }
  80.       }
  81.  
  82.       return false;
  83.    }
  84.  
  85.    public boolean inside(int var1, int var2) {
  86.       Polygon var3 = new Polygon(this._xpt, this._ypt, this._npts);
  87.       return var3.inside(var1, var2);
  88.    }
  89.  
  90.    public boolean isValid() {
  91.       return !this.isClosed() || this._npts >= 4;
  92.    }
  93.  
  94.    public boolean isComplete() {
  95.       return this.isValid() && this.isClosed();
  96.    }
  97.  
  98.    public void advance() {
  99.       this._grabIndex = this._npts;
  100.    }
  101.  
  102.    public void draw(Graphics var1) {
  103.       if (this.isValid()) {
  104.          for(int var2 = 1; var2 < this._npts; ++var2) {
  105.             var1.drawLine(this._xpt[var2 - 1], this._ypt[var2 - 1], this._xpt[var2], this._ypt[var2]);
  106.          }
  107.  
  108.          if (!this.isClosed()) {
  109.             ((Area)this).drawAnchor(var1, this._xpt[0], this._ypt[0]);
  110.          }
  111.  
  112.          if (super._selected) {
  113.             for(int var3 = 1; var3 < this._npts; ++var3) {
  114.                ((Area)this).drawPoint(var1, this._xpt[var3], this._ypt[var3]);
  115.             }
  116.          }
  117.       }
  118.  
  119.    }
  120.  
  121.    public String getShape() {
  122.       return "POLY";
  123.    }
  124.  
  125.    public String getCoords() {
  126.       StringBuffer var1 = new StringBuffer();
  127.  
  128.       for(int var2 = 0; var2 < this._npts; ++var2) {
  129.          if (var2 > 0) {
  130.             var1.append(",");
  131.          }
  132.  
  133.          var1.append(this._xpt[var2] + "," + this._ypt[var2]);
  134.       }
  135.  
  136.       return var1.toString();
  137.    }
  138.  
  139.    public synchronized Object clone() {
  140.       Poly var1 = new Poly();
  141.       var1._npts = this._npts;
  142.       var1._xpt = new int[this._xpt.length];
  143.       var1._ypt = new int[this._ypt.length];
  144.       System.arraycopy(this._xpt, 0, var1._xpt, 0, this._npts);
  145.       System.arraycopy(this._ypt, 0, var1._ypt, 0, this._npts);
  146.       var1._grabIndex = this._grabIndex;
  147.       return var1;
  148.    }
  149.  
  150.    private void addPoint(int var1, int var2) {
  151.       if (this._npts == this._xpt.length) {
  152.          int[] var3 = new int[this._xpt.length * 2];
  153.          System.arraycopy(this._xpt, 0, var3, 0, this._npts);
  154.          this._xpt = var3;
  155.       }
  156.  
  157.       if (this._npts == this._ypt.length) {
  158.          int[] var4 = new int[this._ypt.length * 2];
  159.          System.arraycopy(this._ypt, 0, var4, 0, this._npts);
  160.          this._ypt = var4;
  161.       }
  162.  
  163.       this._xpt[this._npts] = var1;
  164.       this._ypt[this._npts] = var2;
  165.       ++this._npts;
  166.    }
  167.  
  168.    private void setPoint(int var1, int var2, int var3) {
  169.       if (var1 < this._npts) {
  170.          this._xpt[var1] = var2;
  171.          this._ypt[var1] = var3;
  172.       }
  173.  
  174.    }
  175.  
  176.    private boolean isClosed() {
  177.       return this._npts > 1 && this._xpt[0] == this._xpt[this._npts - 1] && this._ypt[0] == this._ypt[this._npts - 1];
  178.    }
  179.  
  180.    public void close() {
  181.       if (!this.isClosed()) {
  182.          this.addPoint(this._xpt[0], this._ypt[0]);
  183.       }
  184.  
  185.    }
  186. }
  187.