home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / commlet / draw / gopolygon.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.1 KB  |  110 lines

  1.  
  2. package como.commlet.draw;
  3.  
  4. import como.io.*;
  5. import como.util.*;
  6. import java.awt.*;
  7. import java.io.*;
  8. import java.util.*; import java.lang.*;
  9.  
  10. /******
  11. * GOPolygon
  12. */
  13. public class GOPolygon extends GraphicsObject
  14. {
  15.     Polygon pol;
  16.     boolean forcedcut = false;
  17.  
  18.     public GOPolygon()
  19.     {
  20.         super();
  21.         pol = new Polygon();
  22.         filled = true;
  23.         forcedcut = false;
  24.     }        
  25.     public GraphicsObject getNew ()  {
  26.         GOPolygon go = new GOPolygon();
  27.         go.color = color;
  28.         go.name = name;
  29.         go.x = x;
  30.         go.y = y;
  31.         go.filled = filled;
  32.         go.pol = new Polygon();
  33.         if (forcedcut) go.addPoint(x,y);
  34.         go.forcedcut = false;
  35.         forcedcut = false;
  36.         return go;
  37.     }
  38.  
  39.     public void addPoint(int x, int y) {
  40.         pol.addPoint(x,y);
  41.     }
  42.     public void draw(Graphics g,Color c)
  43.     {
  44.         lastgraphics = g;
  45.         g.setColor(c);
  46.         if (pol.npoints>0) {
  47.             if (filled) g.fillPolygon(pol);
  48.             else g.drawPolygon(pol);
  49.         }
  50.     }        
  51.     public boolean handleEvent(Event evt)
  52.     {
  53.         if (evt.id == Event.MOUSE_DRAG)
  54.         {
  55.             addPoint(evt.x,evt.y);
  56.             if (pol.npoints > 30)    {
  57.                 evt.id = DrawCanvas.EVENT_ACCEPT;
  58.                 forcedcut = true;
  59.                 x = evt.x;
  60.                 y = evt.y;
  61.             }
  62.             return true;
  63.         }
  64.         else if (evt.id == Event.MOUSE_UP)
  65.         {
  66.             addPoint(evt.x,evt.y);
  67.             evt.id = DrawCanvas.EVENT_ACCEPT;
  68.             return true;
  69.         }
  70.         else if (evt.id == Event.MOUSE_DOWN)
  71.         {
  72.             addPoint(evt.x,evt.y);
  73.             return true;
  74.         }
  75.         return false;
  76.     }
  77.     public String toString()
  78.     {
  79.         StringBuffer s = new StringBuffer();            
  80.         if (filled) s.append("F "); else s.append("N ");
  81.         s.append(pol.npoints+" ");
  82.         int i;
  83.         for (i=0;i<pol.npoints;i++)
  84.         s.append(pol.xpoints[i]+" "+pol.ypoints[i]+" ");
  85.         return super.toString()+s.toString();
  86.     }
  87.     public void save(ObjectOutputStream s) throws IOException {
  88.         super.save(s);
  89.         s.writeInt(pol.npoints);
  90.         int i;
  91.         for (i=0;i<pol.npoints;i++) {
  92.             s.writeShort(pol.xpoints[i]);
  93.             s.writeShort(pol.ypoints[i]);
  94.         }
  95.             
  96.     }
  97.     public void load(ObjectInputStream s) throws IOException {
  98.         super.load(s);
  99.         pol.npoints = s.readInt();
  100.         pol.xpoints = new int[pol.npoints];
  101.         pol.ypoints = new int[pol.npoints];
  102.         int i;
  103.         for(i=0;i<pol.npoints;i++)
  104.         {
  105.             pol.xpoints[i] = s.readShort();
  106.             pol.ypoints[i] = s.readShort();
  107.         }
  108.     }
  109. }
  110.