1 package devisor2.grid.options; 2 3 import java.awt.*; 4 import devisor2.grid.GUI.framework.*; 5 import devisor2.grid.backend.*; 6 import devisor2.foundation.boundary.*; 7 import java.awt.geom.*; 8 9 /** 10 * This is the convenience class for easier access to transformation options 11 * and everything that affects working with the domain. 12 * For easy access, there is a reference to the most recent instance of this 13 * class in the ControlCenter, which you can access as usual. 14 * 15 * @author Dominik Goeddeke 16 * @version 1.0 17 */ 18 public class GridControl 19 { 20 private ControlCenter cc = ControlCenter.getMyself (); 21 22 public GridToolkit gtk; 23 24 25 /** 26 * The number of the selected boundary, default is the first boundary, 27 * created during domain creation 28 */ 29 public Boundary workingBoundary = null; 30 31 /** 32 * reference to the MainFrame of the application 33 */ 34 public MainFrame mf; 35 36 /** 37 * The grid transformation matrix and its inverse 38 */ 39 public AffineTransform trf; 40 public AffineTransform invtrf; 41 42 /** 43 * snap variables 44 */ 45 public int vsnapx,vsnapy; 46 public int vsnapoffx,vsnapoffy; 47 public boolean snapon; 48 49 /** 50 * the global eps used in various methods 51 */ 52 public int eps; 53 GridControl(MainFrame uf, ControlCenter uc)54 public GridControl (MainFrame uf, ControlCenter uc) 55 { 56 setMainFrame(uf, uc); 57 } 58 59 setMainFrame(MainFrame uf, ControlCenter uc)60 public void setMainFrame(MainFrame uf, ControlCenter uc) 61 { 62 mf=uf; 63 cc=uc; 64 gtk = new GridToolkit (uf); 65 vsnapx = Integer.parseInt (cc.op.get(Options.snap_x)); 66 vsnapy = Integer.parseInt (cc.op.get(Options.snap_y)); 67 snapon = Boolean.valueOf(cc.op.get (Options.snap_enabled)).booleanValue(); 68 int eps = Integer.parseInt (cc.op.get (Options.epsilon)); 69 double epsi = 0.01 + ((double)eps)/100 * 0.99; 70 eps = (int)(epsi*(cc.dc.world_x2 - cc.dc.world_x1)); 71 } 72 runden(double d)73 public double runden(double d) 74 { 75 76 String s = (new Double(d)).toString(); 77 78 StringBuffer ss = new StringBuffer(s); 79 if (ss.indexOf("E-")>=0) 80 { 81 // System.out.println("DIGIMON "+s.substring(0,1)); 82 83 int nk = (new Integer(ss.substring(ss.indexOf("E-")+2))).intValue(); 84 85 // System.out.println("DIGIMON "+nk); 86 87 ss=new StringBuffer("0."); 88 for (int i=0;i<nk-1;i++) ss=ss.append("0"); 89 ss=ss.append(s.substring(0,1)); 90 ss=ss.append("00"); 91 } 92 93 int l=ss.length(); 94 int i; 95 96 int[] digi= new int[l]; 97 98 // System.out.println("DIGI "+ss); 99 100 101 for(i=0; i<ss.length(); i++) 102 { 103 String s1 = ss.substring(i,i+1); 104 105 // System.out.println("DIGI "+s1); 106 107 if (s1.equals(".")) 108 digi[i]=-1; 109 else 110 digi[i]=(new Integer(s1)).intValue(); 111 112 } 113 114 115 for(i=l-1; i>1;i--) 116 { 117 if (digi[i]==0) break; 118 119 // if (digi[i]<=4) 120 // { 121 // digi[i]=0; 122 // digi[i-1]--; 123 // } 124 125 if (digi[i]==10) 126 { 127 digi[i]=0; 128 digi[i-1]++; 129 } 130 else 131 { 132 if (digi[i]>4) 133 { 134 digi[i]=0; 135 digi[i-1]++; 136 } 137 } 138 139 } 140 141 int flag = 0; 142 143 for(i=0; i<l-1;i++) 144 { 145 // System.out.println("FLAGGG "+flag); 146 147 if ((flag==0) && (digi[i]==-1)) { flag=1; continue;} 148 149 if ((flag==1) && (digi[i]==0) && (digi[i+1]!=0)) { flag=2; continue; } 150 151 if (flag==2) { flag=3; continue; } 152 // if (flag==3) { flag=4; continue; } 153 154 if (flag==3) digi[i]=0; 155 156 157 } 158 159 digi[l-1]=0; 160 161 ss = new StringBuffer(); 162 163 for(i=0; i<l; i++) 164 { 165 if (digi[i]==-1) 166 ss=ss.append("."); 167 else 168 ss=ss.append((new Integer(digi[i])).toString()); 169 170 } 171 172 s=new String(ss); 173 174 double r = (new Double(s)).doubleValue(); 175 // System.out.println("GERUNDET : von "+d+" zu "+r); 176 177 return r; 178 } 179 180 181 setTransformation()182 public void setTransformation() 183 { 184 if (mf!=null) 185 { 186 cc.dc.screen_width=mf.getDrawingArea().getWidth(); 187 cc.dc.screen_height=mf.getDrawingArea().getHeight(); 188 189 //runden(1.2343546); 190 //runden(1.23499999998); 191 //runden(1.2349939999); 192 //runden(1.2349949999); 193 //runden(0.00234995999); 194 //runden(0.00234993999); 195 //runden(0.00234994999); 196 197 198 int iff = cc.globalscale2; 199 int tv = ((int)(cc.dc.act_x1/cc.globalscale2))*cc.globalscale2; 200 while (tv==0) 201 { 202 iff=iff/10; 203 tv = ((int)(cc.dc.act_x1/iff))*iff; 204 } 205 cc.dc.act_x1=tv; 206 207 208 iff = cc.globalscale2; 209 tv = ((int)(cc.dc.act_y1/cc.globalscale2))*cc.globalscale2; 210 while (tv==0) 211 { 212 iff=iff/10; 213 tv = ((int)(cc.dc.act_y1/iff))*iff; 214 } 215 cc.dc.act_y1=tv; 216 217 iff = cc.globalscale2; 218 tv = ((int)(cc.dc.act_x2/cc.globalscale2))*cc.globalscale2; 219 while (tv==0) 220 { 221 iff=iff/10; 222 tv = ((int)(cc.dc.act_x2/iff))*iff; 223 } 224 cc.dc.act_x2=tv; 225 226 iff = cc.globalscale2; 227 tv = ((int)(cc.dc.act_y2/cc.globalscale2))*cc.globalscale2; 228 while (tv==0) 229 { 230 iff=iff/10; 231 tv = ((int)(cc.dc.act_y2/iff))*iff; 232 } 233 cc.dc.act_y2=tv; 234 235 236 //System.out.println("setTransfo "+cc.dc.screen_width); 237 //System.out.println("setTransfo "+cc.dc.screen_height); 238 239 double kx = (double)cc.dc.screen_width/(double)(cc.dc.act_x2-cc.dc.act_x1); 240 double ky = (double)cc.dc.screen_height/(double)(cc.dc.act_y2-cc.dc.act_y1); 241 242 // double k = Math.round(Math.min(kx,ky)*100000000)/100000000.0; 243 double k = runden(Math.min(kx,ky)); 244 245 //System.out.println("KMXK "+k); 246 247 //if ((k>0.0020) && (k<0.0021)) k=0.002; 248 249 //System.out.println("KMX "+k); 250 251 cc.dc.used_screen_width=(int)(cc.dc.screen_width*k/kx); 252 cc.dc.used_screen_height=(int)(cc.dc.screen_height*k/ky); 253 254 kx = runden((double)cc.dc.used_screen_width/(double)(cc.dc.act_x2-cc.dc.act_x1)); 255 ky = runden((double)cc.dc.used_screen_height/(double)(cc.dc.act_y2-cc.dc.act_y1)); 256 257 kx=k; 258 ky=k; 259 // if ((k>0.0019) && (k<0.0021)) kx=0.002; 260 // if ((k>0.0019) && (k<0.0021)) ky=0.002; 261 // System.out.println("KMK "+kx+" "+ky); 262 263 trf = new AffineTransform(); 264 trf.concatenate(new AffineTransform(1.,0.,0.,-1.,0.,(double)cc.dc.used_screen_height)); 265 trf.scale(kx,ky); 266 267 // System.out.println("DRMS "+(-(double)cc.dc.act_x1)+" "+(-(double)cc.dc.act_y1)); 268 269 trf.translate(-(double)cc.dc.act_x1,-(double)cc.dc.act_y1); 270 271 try 272 { 273 invtrf = trf.createInverse(); 274 } 275 catch (NoninvertibleTransformException ex) 276 { 277 System.err.println("Fehler"); 278 } 279 } 280 } 281 boxFromScreenToWorld(Rectangle box)282 public Rectangle boxFromScreenToWorld(Rectangle box) 283 { 284 int[] xy = fromScreenToWorld(box.x,box.y); 285 286 Rectangle ret = new Rectangle(); 287 288 ret.width = SingleFromScreenToWorld(box.width); 289 ret.height = SingleFromScreenToWorld(box.height); 290 291 ret.x=xy[0]; 292 ret.y=xy[1]; 293 294 return ret; 295 296 } 297 fromWorldToUser(int x)298 public double fromWorldToUser(int x) 299 { 300 return (double)x/cc.globalscale; 301 } 302 303 fromUserToWorld(double x)304 public int fromUserToWorld(double x) 305 { 306 return (int)(x*cc.globalscale); 307 } 308 309 fromScreenToWorld(int x, int y)310 public int[] fromScreenToWorld(int x, int y) 311 { 312 int[] tcoord = new int[2]; 313 314 double[] scr = {x,y}; 315 double[] user = new double[2]; 316 317 invtrf.transform(scr,0,user,0,1); 318 319 tcoord[0]=(int)user[0]; 320 tcoord[1]=(int)user[1]; 321 322 return tcoord; 323 } 324 fromWorldToScreen(int x, int y)325 public int[] fromWorldToScreen(int x, int y) 326 { 327 int[] tcoord = new int[2]; 328 329 double[] scr = {x,y}; 330 double[] user = new double[2]; 331 332 trf.transform(scr,0,user,0,1); 333 334 tcoord[0]=(int)user[0]; 335 tcoord[1]=(int)user[1]; 336 337 return tcoord; 338 } 339 getScaleX()340 public double getScaleX () 341 { 342 return trf.getScaleX(); 343 } 344 getScaleY()345 public double getScaleY () 346 { 347 return trf.getScaleY(); 348 } 349 /** 350 * a single value representing some length or some distance is 351 * transformed into world space 352 */ SingleFromScreenToWorld(int value)353 public int SingleFromScreenToWorld (int value) 354 { 355 return (int)(value / getScaleX()); 356 } 357 358 /** 359 * a single value representing some length or some distance is 360 * transformed from world space 361 */ SingleFromWorldToScreen(int value)362 public int SingleFromWorldToScreen (int value) 363 { 364 return (int)(value * getScaleX()); 365 } 366 367 368 } 369 370