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

  1. import java.awt.*;
  2. /**
  3.  * A class that help to resize the drawing
  4.  *
  5.  * @version     1.0 03/24/96
  6.  * @author         Kang, Dae Woong (namu@star.elim.net)
  7.  */
  8.  
  9. class Tracker 
  10. {
  11.     Draw draw;
  12.     int px, py;
  13.     int ptSelected;
  14.  
  15.     int w, h;
  16.  
  17.     final int size = 8;
  18.  
  19.     int stateSelect = 0;
  20.     final int OFF = 0;
  21.     final int MOVE = 1;
  22.     final int RESIZE = 2;
  23.     final int POINT = 3;
  24.     final int SELECT = 4;
  25.  
  26.     int stateHeight = 0;
  27.     final int TOP = 1;
  28.     final int BOTTOM = 2;
  29.  
  30.     int stateWidth = 0;
  31.     final int LEFT = 1;
  32.     final int RIGHT = 2;
  33.  
  34.     Rectangle rcOut = new Rectangle();
  35.     
  36.     boolean isInTracker(int x, int y)
  37.     {
  38.         if (stateSelect == OFF)
  39.         {
  40.             stateSelect = OFF;
  41.             return false;    
  42.         }
  43.         else 
  44.         {
  45.             if (getClipRect().inside(x, y) || draw.method != Draw.IMAGE)
  46.             {
  47.                 int count = draw.getPointsCount();
  48.                 for (int i = 0; i < count; i++)
  49.                 {
  50.                     if(getPointRect(draw.getPoint(i)).inside(x, y))
  51.                     {
  52.                         stateSelect = POINT;
  53.                         ptSelected = i;
  54.                         px = x;
  55.                         py = y;
  56.                         return true;
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             if (draw.inside(x, y))
  62.             {
  63.                 stateSelect = MOVE;
  64.                 px = x;
  65.                 py = y;
  66.                 return true;
  67.             }
  68.             else if (!draw.isPointsOnlyType()) 
  69.             {
  70.                 for (int xx = 0; xx < 3; xx++)
  71.                 {
  72.                     for (int yy = 0; yy < 3; yy++)
  73.                     {
  74.                         if(getSmallRect(xx, yy).inside(x, y))
  75.                         {
  76.                             stateWidth = xx;        
  77.                             stateHeight = yy;        
  78.                             stateSelect = RESIZE;
  79.                             px = x;
  80.                             py = y;
  81.                             return true;
  82.                         }
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.         stateSelect = OFF;
  88.         return false;    
  89.     }
  90.  
  91.     public boolean  drag(int x, int y)
  92.     {
  93.         w = x - px;
  94.         h = y - py;
  95.  
  96.         px = x;
  97.         py = y;
  98.  
  99.         switch (stateSelect)
  100.         {
  101.         case MOVE:
  102.             return true;
  103.         case RESIZE:
  104.             int xx = 0, yy = 0;
  105.  
  106.             switch (stateHeight)
  107.             {
  108.             case TOP: yy = h; h = -h; break; 
  109.             case BOTTOM: break;
  110.             default: h = 0;
  111.             }
  112.             if (draw.height + h <= 0)
  113.             {
  114.                 h = 0; yy = 0;
  115.             }
  116.         
  117.             switch (stateWidth)
  118.             {
  119.             case LEFT:  xx = w; w = -w; break; 
  120.             case RIGHT: break;
  121.             default: w = 0;
  122.             } 
  123.             if (draw.width + w <= 0)
  124.             {
  125.                 w = 0; xx = 0;
  126.             }
  127.  
  128.             draw.reshape(draw.x + xx, draw.y + yy, draw.width + w, draw.height + h);
  129.             return true;
  130.         case POINT:
  131.             Point pt = draw.getPoint(ptSelected);
  132.             
  133.             draw.setPoint(ptSelected, pt.x + w, pt.y + h);
  134.             if (draw.isPointsOnlyType())
  135.             {
  136.                 draw.setRect(draw.getPolygon().getBoundingBox());
  137.  
  138.                 if (draw.method == Draw.LINE)
  139.                     draw.setLineAngle();
  140.                 if (draw.isEmpty())
  141.                     draw.grow((draw.width == 0) ? 2 : 0, (draw.height == 0) ? 2 : 0);
  142.             }
  143.             px = pt.x + w;
  144.             py = pt.y + h;
  145.  
  146.             return true;
  147.         }        
  148.         return false;
  149.     }
  150.  
  151.     public Rectangle getSmallRect(int xx, int yy)
  152.     {
  153.         int x, y;
  154.         if (xx == 0 &&  yy == 0) //center rect
  155.             return new Rectangle(-1, -1, 0, 0);
  156.         
  157.         switch (xx)
  158.         {
  159.         case  LEFT:  x = draw.x - 1 - size; break;
  160.         case  RIGHT: x = draw.x + draw.width + 1; break;
  161.         default:     x = draw.x  + (draw.width - size)/2; break; // center 
  162.         }
  163.  
  164.         switch (yy)
  165.         {
  166.         case  TOP:    y = draw.y - 1 - size; break;
  167.         case  BOTTOM: y = draw.y + draw.height + 1; break;
  168.         default:      y = draw.y + (draw.height - size)/2; break; // center 
  169.         }
  170.         
  171.         return new Rectangle(x, y, size, size);
  172.     }
  173.  
  174.     public Rectangle getPointRect(Point pt)
  175.     {
  176.         return new Rectangle(pt.x - size/2, pt.y - size/2, size, size);        
  177.     }
  178.  
  179.     public Rectangle getClipRect()
  180.     {
  181.         return (stateSelect == OFF) ? new Rectangle(-1, -1, -1, -1) 
  182.           : new Rectangle(draw.x - size - 1, draw.y - size - 1, draw.width + 2 + size*2, draw.height + 2 + size*2);
  183.     }
  184.  
  185.     public void draw(Graphics g)
  186.     {
  187.         if (stateSelect != OFF)
  188.         {
  189.             g.setColor(Color.white);
  190.             g.setXORMode(Color.black);
  191.             rcOut.reshape(draw.x - 1, draw.y - 1, draw.width + 2, draw.height + 2);
  192.             g.drawRect(rcOut.x, rcOut.y, rcOut.width, rcOut.height);
  193.             if (!draw.isPointsOnlyType() && draw.method != Draw.IMAGE)
  194.             {
  195.                 for (int x = 0; x < 3; x++)
  196.                 {
  197.                     for (int y = 0; y < 3; y++)
  198.                     {
  199.                         Rectangle rc = getSmallRect(x, y);
  200.                         g.fillRect(rc.x, rc.y, rc.width, rc.height);
  201.                     }
  202.                 }
  203.             }
  204.             
  205.             g.setXORMode(Color.red);
  206.             int count = draw.getPointsCount();
  207.             for (int i = 0; i < count; i++)
  208.             {
  209.                 Rectangle rc = getPointRect(draw.getPoint(i));
  210.                 g.fillRect(rc.x, rc.y, rc.width, rc.height);
  211.             }
  212.             g.setPaintMode();
  213.         }
  214.     }
  215.  
  216.  
  217.     public void setDraw(Draw draw, int x, int y)
  218.     {
  219.         this.draw = draw;
  220.         stateSelect = MOVE;
  221.         stateWidth = 0;
  222.         stateHeight = 0;
  223.         px = x;
  224.         py = y;
  225.     }
  226. }
  227.