1 27 package org.htmlparser.lexerapplications.thumbelina; 28 29 import java.awt.Rectangle ; 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 33 36 public class TileSet 37 45 { 46 49 protected Vector mRegions; 50 51 54 public TileSet () 55 { 56 mRegions = new Vector (); 57 } 58 59 65 public int getSize () 66 { 67 return (mRegions.size ()); 68 } 69 70 74 public Enumeration getPictures () 75 { 76 return (mRegions.elements ()); 77 } 78 79 83 public void add (final Picture r) 84 { 85 Vector regions; Enumeration e; 87 Picture rover; 88 Rectangle intersection; 89 Vector splits; 90 Enumeration frags; 91 92 regions = new Vector (); 93 for (e = getPictures (); e.hasMoreElements (); ) 94 { 95 rover = (Picture)e.nextElement (); 96 if (rover.intersects (r)) 97 { 98 intersection = rover.intersection (r); 99 if (!intersection.equals (rover)) 100 { 101 splits = split (r, rover, false); 104 for (frags = splits.elements (); frags.hasMoreElements (); ) 105 regions.addElement (frags.nextElement ()); 106 } 107 else 108 rover.setImage (null); 111 } 112 else 113 regions.addElement (rover); 115 } 116 regions.addElement (r); 117 mRegions = regions; 118 } 119 120 131 private Vector split ( 132 final Picture small, 133 final Picture large, 134 final boolean keep) 135 { 136 Picture m; 137 Vector ret; 138 139 ret = new Vector (); 140 141 if (large.intersects (small)) 142 { 143 Rectangle intersection = large.intersection (small); 144 145 if ((intersection.y + intersection.height) 147 != (large.y + large.height)) 148 { 149 m = new Picture (large); 150 m.y = (intersection.y + intersection.height); 151 m.height = (large.y + large.height) - m.y; 152 ret.addElement (m); 153 } 154 155 if (intersection.x != large.x) 157 { 158 m = new Picture (large); 159 m.y = intersection.y; 160 m.width = intersection.x - large.x; 161 m.height = intersection.height; 162 ret.addElement (m); 163 } 164 165 if (keep) 167 { 168 m = new Picture (large); 169 m.x = intersection.x; 170 m.y = intersection.y; 171 m.width = intersection.width; 172 m.height = intersection.height; 173 ret.addElement (m); 174 } 175 176 if ((intersection.x + intersection.width) 178 != (large.x + large.width)) 179 { 180 m = new Picture (large); 181 m.x = intersection.x + intersection.width; 182 m.y = intersection.y; 183 m.width = (large.x + large.width) - m.x; 184 m.height = intersection.height; 185 ret.addElement (m); 186 } 187 188 if (intersection.y != large.y) 190 { 191 m = new Picture (large); 192 m.height = (intersection.y - large.y); 193 ret.addElement (m); 194 } 195 } 196 197 return (ret); 198 } 199 200 207 public Picture pictureAt (final int x, final int y) 208 { 209 Picture m; 210 Picture ret; 211 212 ret = null; 213 214 for (int i = 0; (null == ret) && (i < mRegions.size ()); i++) 215 { 216 m = (Picture)mRegions.elementAt (i); 217 if (m.contains (x, y)) 218 ret = m; 219 } 220 221 return (ret); 222 } 223 224 228 public void bringToTop (final Picture picture) 229 { 230 Picture m; 231 Picture ret; 232 233 ret = null; 234 235 for (int i = 0; (null == ret) && (i < mRegions.size ()); ) 236 { 237 m = (Picture)mRegions.elementAt (i); 238 if (picture.same (m)) 239 mRegions.removeElementAt (i); 240 else 241 i++; 242 } 243 add (picture); 244 245 } 246 247 } 537 538 554 | Popular Tags |