home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / com / next / gt / RCS / DirtyRectSet.java,v < prev    next >
Text File  |  1998-04-15  |  2KB  |  144 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @# @;
  6.  
  7.  
  8. 1.1
  9. date    98.04.10.17.53.01;    author jgh8962;    state Exp;
  10. branches;
  11. next    ;
  12.  
  13.  
  14. desc
  15. @@
  16.  
  17.  
  18. 1.1
  19. log
  20. @Initial revision
  21. @
  22. text
  23. @/**
  24.  *
  25.  * DirtyRectSet.java
  26.  * Mar 15/1996
  27.  *
  28.  *
  29. */
  30.  
  31.  
  32. package com.next.gt;
  33.  
  34. import java.util.Vector;
  35. import java.awt.Rectangle;
  36.  
  37.  
  38. public class DirtyRectSet extends java.lang.Object {
  39.  
  40.    private Vector rects;
  41.  
  42.    public DirtyRectSet() { rects = new Vector(); }
  43.    public DirtyRectSet(int i) { rects = new Vector(i); } 
  44.  
  45.    public void addRect (Rectangle r)
  46.    {
  47.           int size = rects.size ();
  48.  
  49.         for (int index = 0; index < size; index++)
  50.         {
  51.             Rectangle curr = (Rectangle)rects.elementAt (index);
  52.  
  53.             if (r.x > curr.x)
  54.             {
  55.                 rects.insertElementAt (r, index);
  56.                 return;
  57.             } 
  58.         }
  59.         
  60.         rects.addElement (r);
  61.  
  62.    }
  63.  
  64.    final int GLUE = 64;
  65.  
  66.    final private boolean closeEnough (Rectangle r1, Rectangle r2)
  67.    {
  68.          boolean result;
  69.  
  70.       r1.width += GLUE;
  71.       r1.height += GLUE;
  72.       r2.width += GLUE;
  73.       r2.height += GLUE;
  74.  
  75.       result = r1.intersects (r2);
  76.  
  77.       r1.width -= GLUE;
  78.       r1.height -= GLUE;
  79.       r2.width -= GLUE;
  80.       r2.height -= GLUE;
  81.  
  82.       return result;
  83.    }
  84.  
  85.    private void collapse ()
  86.    {
  87.         int index = 0;
  88.  
  89.         if (rects.size () < 2)
  90.             return;
  91.  
  92.         Rectangle r1 = (Rectangle)rects.elementAt (index);
  93.         Rectangle r2 = (Rectangle)rects.elementAt (index+1);
  94.  
  95.         while (true)
  96.         {
  97.             // collapse R1 and R2
  98.             if (closeEnough (r1, r2))
  99.             {
  100.                 r1 = r1.union (r2);
  101.  
  102.                 rects.setElementAt (r1, index);
  103.                 rects.removeElementAt (index+1);
  104.  
  105.                 if (index+1 < rects.size ())
  106.                     r2 = (Rectangle)rects.elementAt (index+1);
  107.                 else
  108.                     return;
  109.             }
  110.  
  111.             // go to next pair
  112.             else if (index+2 < rects.size ())
  113.             {
  114.                 r1 = r2;
  115.                 r2 = (Rectangle)rects.elementAt (index+2);
  116.                 index += 1;
  117.             }
  118.  
  119.             // done
  120.             else
  121.             {
  122.                 return;
  123.             }
  124.         }
  125.     }
  126.  
  127.  
  128.     public void drawImage (java.awt.Graphics g, java.awt.Image img,
  129.                     Gamelication owner)
  130.     {
  131.         collapse ();
  132.  
  133.         for (int i= 0; i< rects.size (); i++) {
  134.             Rectangle r = (Rectangle)rects.elementAt(i);
  135.               java.awt.Graphics g2 = g.create (r.x, r.y, r.width, r.height);
  136.               g2.drawImage(img, -r.x, -r.y, owner);
  137.               g2.dispose ();
  138.  
  139.         }
  140.     }
  141.  
  142. }
  143. @
  144.