1 package JSci.instruments; 2 3 import java.awt.*; 4 import java.awt.geom.*; 5 import java.awt.event.*; 6 import java.util.*; 7 import java.awt.geom.*; 8 import javax.swing.*; 9 import javax.swing.event.*; 10 11 14 public class TwoROI extends ROIAdapter implements Control { 15 private Rectangle template = new Rectangle(0,0,-2,-2); 16 private Rectangle search = new Rectangle(0,0,-2,-2); 17 private int factor = 1; 18 private boolean useFactor = true; 19 private static final int LW = 3; 20 private int state; 21 private int iX; 22 private int iY; 23 24 25 private Component comp = null; 26 public void setComponent(Component c) { 27 if (comp!=null) { 28 comp.removeMouseListener((MouseListener)this); 29 comp.removeMouseMotionListener((MouseMotionListener)this); 30 } 31 comp=c; 32 if (comp!=null) { 33 comp.addMouseListener((MouseListener)this); 34 comp.addMouseMotionListener((MouseMotionListener)this); 35 } 36 } 37 38 39 42 public void paint(Graphics g) { 43 Graphics2D g2 = (Graphics2D)g; 44 g2.setPaint(Color.GREEN); 45 g2.draw(search); 46 g2.setPaint(Color.MAGENTA); 47 g2.draw(template); 48 } 49 50 54 public void setUseFactor(boolean u) { 55 useFactor = u; 56 if (useFactor) search.setFrame( 57 template.getMinX()-factor*template.getWidth(), 58 template.getMinY()-factor*template.getHeight(), 59 (2*factor+1)*template.getWidth(), 60 (2*factor+1)*template.getHeight()); 61 if (comp!=null) comp.repaint(); 62 } 63 64 65 69 public void setFactor(int f) { 70 factor = f; 71 if (useFactor) search.setFrame( 72 template.getMinX()-factor*template.getWidth(), 73 template.getMinY()-factor*template.getHeight(), 74 (2*factor+1)*template.getWidth(), 75 (2*factor+1)*template.getHeight()); 76 if (comp!=null) comp.repaint(); 77 } 78 79 80 83 public Shape getShape() { 84 return (Rectangle)template.clone(); 85 } 86 87 90 public Rectangle getOuterRectangle() { return (Rectangle)search.clone(); } 91 92 95 public static void main(String [] args) { 96 final ROI r = new TwoROI(); 97 Player p = new Player(); 98 JSci.instruments.ImageSource fs = new TestImageSource(); 99 ImageFilter filter = new ImageFilterAdapter() { 100 public void filter(Image f) {} 101 public Component getFilterControlComponent() { return ((TwoROI)r).getControlComponent(); } 102 }; 103 fs.setSink(filter); 104 filter.setSink(p); 105 p.addROI(r); 106 p.start(); 107 } 108 109 110 JPanel ccomp=null; 111 112 public Component getControlComponent() { 113 if (ccomp!=null) return ccomp; 114 ccomp=new JPanel(); 115 ccomp.setLayout(new BorderLayout()); 116 final JCheckBox modifySearchArea; 117 final JSlider factor; 118 modifySearchArea = new JCheckBox("Modify search area",false); 119 modifySearchArea.addActionListener(new ActionListener() { 120 public void actionPerformed(ActionEvent e) { 121 setUseFactor(!modifySearchArea.isSelected()); 122 } 123 }); 124 factor = new JSlider(1,5,1); 125 setFactor(1); 126 factor.addChangeListener(new ChangeListener() { 127 public void stateChanged(ChangeEvent e) { 128 setFactor(factor.getValue()); 129 } 130 }); 131 ccomp.add(BorderLayout.NORTH,modifySearchArea); 132 ccomp.add(BorderLayout.SOUTH,factor); 133 return ccomp; 134 } 135 136 private int region(Point2D p) { 137 if (p.getX()>=template.getMinX()-LW && 138 p.getX()<=template.getMaxX()+LW && 139 p.getY()>=template.getMinY()-LW && 140 p.getY()<=template.getMaxY()+LW ) { 141 if (p.getX()<=template.getMinX()) { 142 if (p.getY()<=template.getMinY()) return 10; 143 if (p.getY()<template.getMaxY()) return 17; 144 return 16; 145 } 146 if (p.getX()<template.getMaxX()) { 147 if (p.getY()<=template.getMinY()) return 11; 148 if (p.getY()<template.getMaxY()) return 1; 149 return 15; 150 } 151 if (p.getY()<=template.getMinY()) return 12; 152 if (p.getY()<template.getMaxY()) return 13; 153 return 14; 154 } 155 if (useFactor) { 156 if (search.contains(p)) return 1; 157 else return 0; 158 } 159 else { 160 if (p.getX()>=search.getMinX()-LW && 161 p.getX()<=search.getMaxX()+LW && 162 p.getY()>=search.getMinY()-LW && 163 p.getY()<=search.getMaxY()+LW ) { 164 if (p.getX()<=search.getMinX()) { 165 if (p.getY()<=search.getMinY()) return 2; 166 if (p.getY()<search.getMaxY()) return 9; 167 return 8; 168 } 169 if (p.getX()<search.getMaxX()) { 170 if (p.getY()<=search.getMinY()) return 3; 171 if (p.getY()<search.getMaxY()) return 1; 172 return 7; 173 } 174 if (p.getY()<=search.getMinY()) return 4; 175 if (p.getY()<search.getMaxY()) return 5; 176 return 6; 177 } 178 else 179 return 0; 180 } 181 } 182 183 public void mousePressed(MouseEvent event) { 184 state = region(event.getPoint()); 185 iX=event.getX(); 186 iY=event.getY(); 187 if (state==0) { 188 template.setFrame(iX,iY,0,0); 189 search.setFrame(iX,iY,0,0); 190 } 191 if (comp!=null) comp.repaint(); 192 } 193 194 public void mouseReleased(MouseEvent event) { 195 state = 0; 196 } 197 198 public void mouseMoved(MouseEvent event) { 199 switch (region(event.getPoint())) { 200 case 0:comp.setCursor(Cursor.getDefaultCursor());break; 201 case 1:comp.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));break; 202 case 2:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));break; 203 204 case 3:comp.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));break; 205 206 case 4:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));break; 207 208 case 5:comp.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));break; 209 210 case 6:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));break; 211 212 case 7:comp.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));break; 213 214 case 8:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));break; 215 216 case 9:comp.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));break; 217 218 case 10:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));break; 219 220 case 11:comp.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));break; 221 222 case 12:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));break; 223 224 case 13:comp.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));break; 225 226 case 14:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));break; 227 228 case 15:comp.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));break; 229 230 case 16:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));break; 231 232 case 17:comp.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));break; 233 234 } 235 } 236 public void mouseDragged(MouseEvent event) { 237 int dX,dY,minX,minY; 238 if (useFactor) switch (state) { 239 case 0: 240 minX=(iX<event.getX())?iX:event.getX(); 241 minY=(iY<event.getY())?iY:event.getY(); 242 dX=iX-event.getX();if (dX<0) dX=-dX; 243 dY=iY-event.getY();if (dY<0) dY=-dY; 244 template.setFrame(minX,minY,dX,dY); 245 break; 246 case 1: 247 template.setFrame(template.getMinX()+event.getX()-iX,template.getMinY()+event.getY()-iY,template.getWidth(),template.getHeight()); 248 iX=event.getX(); 249 iY=event.getY(); 250 break; 251 case 10: 252 dX=event.getX()-iX; 253 dY=event.getY()-iY; 254 if (template.getMinX()+dX>template.getMaxX()) 255 dX=(int)(template.getMaxX()-template.getMinX()); 256 if (template.getMinY()+dY>template.getMaxY()) 257 dY=(int)(template.getMaxY()-template.getMinY()); 258 template.setFrame(template.getMinX()+dX,template.getMinY()+dY,template.getWidth()-dX,template.getHeight()-dY); 259 iX=event.getX(); 260 iY=event.getY(); 261 break; 262 case 11: 263 dY=event.getY()-iY; 264 if (template.getMinY()+dY>template.getMaxY()) 265 dY=(int)(template.getMaxY()-template.getMinY()); 266 template.setFrame(template.getMinX(),template.getMinY()+dY,template.getWidth(),template.getHeight()-dY); 267 iY=event.getY(); 268 break; 269 case 12: 270 dX=event.getX()-iX; 271 dY=event.getY()-iY; 272 if (template.getMaxX()+dX<template.getMinX()) 273 dX=(int)(template.getMinX()-template.getMaxX()); 274 if (template.getMinY()+dY>template.getMaxY()) 275 dY=(int)(template.getMaxY()-template.getMinY()); 276 template.setFrame(template.getMinX(),template.getMinY()+dY,template.getWidth()+dX,template.getHeight()-dY); 277 iX=event.getX(); 278 iY=event.getY(); 279 break; 280 case 13: 281 dX=event.getX()-iX; 282 if (template.getMaxX()+dX<template.getMinX()) 283 dX=(int)(template.getMinX()-template.getMaxX()); 284 template.setFrame(template.getMinX(),template.getMinY(),template.getWidth()+dX,template.getHeight()); 285 iX=event.getX(); 286 break; 287 case 14: 288 dX=event.getX()-iX; 289 dY=event.getY()-iY; 290 if (template.getMaxX()+dX<template.getMinX()) 291 dX=(int)(template.getMinX()-template.getMaxX()); 292 if (template.getMaxY()+dY<template.getMinY()) 293 dY=(int)(template.getMinY()-template.getMaxY()); 294 template.setFrame(template.getMinX(),template.getMinY(),template.getWidth()+dX,template.getHeight()+dY); 295 iX=event.getX(); 296 iY=event.getY(); 297 break; 298 case 15: 299 dY=event.getY()-iY; 300 if (template.getMaxY()+dY<template.getMinY()) 301 dY=(int)(template.getMinY()-template.getMaxY()); 302 template.setFrame(template.getMinX(),template.getMinY(),template.getWidth(),template.getHeight()+dY); 303 iY=event.getY(); 304 break; 305 case 16: 306 dY=event.getY()-iY; 307 dX=event.getX()-iX; 308 if (template.getMinX()+dX>template.getMaxX()) 309 dX=(int)(template.getMaxX()-template.getMinX()); 310 if (template.getMaxY()+dY<template.getMinY()) 311 dY=(int)(template.getMinY()-template.getMaxY()); 312 template.setFrame(template.getMinX()+dX,template.getMinY(),template.getWidth()-dX,template.getHeight()+dY); 313 iX=event.getX(); 314 iY=event.getY(); 315 break; 316 case 17: 317 dX=event.getX()-iX; 318 if (template.getMinX()+dX>template.getMaxX()) 319 dX=(int)(template.getMaxX()-template.getMinX()); 320 template.setFrame(template.getMinX()+dX,template.getMinY(),template.getWidth()-dX,template.getHeight()); 321 iX=event.getX(); 322 break; 323 324 } 325 if (!useFactor) switch (state) { 326 case 0: 327 minX=(iX<event.getX())?iX:event.getX(); 328 minY=(iY<event.getY())?iY:event.getY(); 329 dX=iX-event.getX();if (dX<0) dX=-dX; 330 dY=iY-event.getY();if (dY<0) dY=-dY; 331 template.setFrame(minX,minY,dX,dY); 332 search.setFrame(minX-factor*dX,minY-factor*dY,(2*factor+1)*dX,(2*factor+1)*dY); 333 break; 334 case 1: 335 template.setFrame(template.getMinX()+event.getX()-iX,template.getMinY()+event.getY()-iY,template.getWidth(),template.getHeight()); 336 search.setFrame(search.getMinX()+event.getX()-iX,search.getMinY()+event.getY()-iY,search.getWidth(),search.getHeight()); 337 iX=event.getX(); 338 iY=event.getY(); 339 break; 340 case 2: 341 dX=event.getX()-iX; 342 dY=event.getY()-iY; 343 if (search.getMinX()+dX>template.getMinX()) 344 dX=(int)(template.getMinX()-search.getMinX()); 345 if (search.getMinY()+dY>template.getMinY()) 346 dY=(int)(template.getMinY()-search.getMinY()); 347 search.setFrame(search.getMinX()+dX,search.getMinY()+dY,search.getWidth()-dX,search.getHeight()-dY); 348 iX=event.getX(); 349 iY=event.getY(); 350 break; 351 case 3: 352 dY=event.getY()-iY; 353 if (search.getMinY()+dY>template.getMinY()) 354 dY=(int)(template.getMinY()-search.getMinY()); 355 search.setFrame(search.getMinX(),search.getMinY()+dY,search.getWidth(),search.getHeight()-dY); 356 iY=event.getY(); 357 break; 358 case 4: 359 dX=event.getX()-iX; 360 dY=event.getY()-iY; 361 if (search.getMinY()+dY>template.getMinY()) 362 dY=(int)(template.getMinY()-search.getMinY()); 363 if (search.getMaxX()+dX<template.getMaxX()) 364 dX=(int)(template.getMaxX()-search.getMaxX()); 365 search.setFrame(search.getMinX(),search.getMinY()+dY,search.getWidth()+dX,search.getHeight()-dY); 366 iX=event.getX(); 367 iY=event.getY(); 368 break; 369 case 5: 370 dX=event.getX()-iX; 371 if (search.getMaxX()+dX<template.getMaxX()) 372 dX=(int)(template.getMaxX()-search.getMaxX()); 373 search.setFrame(search.getMinX(),search.getMinY(),search.getWidth()+dX,search.getHeight()); 374 iX=event.getX(); 375 break; 376 case 6: 377 dX=event.getX()-iX; 378 dY=event.getY()-iY; 379 if (search.getMaxX()+dX<template.getMaxX()) 380 dX=(int)(template.getMaxX()-search.getMaxX()); 381 if (search.getMaxY()+dY<template.getMaxY()) 382 dY=(int)(template.getMaxY()-search.getMaxY()); 383 search.setFrame(search.getMinX(),search.getMinY(),search.getWidth()+dX,search.getHeight()+dY); 384 iX=event.getX(); 385 iY=event.getY(); 386 break; 387 case 7: 388 dY=event.getY()-iY; 389 if (search.getMaxY()+dY<template.getMaxY()) 390 dY=(int)(template.getMaxY()-search.getMaxY()); 391 search.setFrame(search.getMinX(),search.getMinY(),search.getWidth(),search.getHeight()+dY); 392 iY=event.getY(); 393 break; 394 case 8: 395 dY=event.getY()-iY; 396 dX=event.getX()-iX; 397 if (search.getMaxY()+dY<template.getMaxY()) 398 dY=(int)(template.getMaxY()-search.getMaxY()); 399 if (search.getMinX()+dX>template.getMinX()) 400 dX=(int)(template.getMinX()-search.getMinX()); 401 search.setFrame(search.getMinX()+dX,search.getMinY(),search.getWidth()-dX,search.getHeight()+dY); 402 iX=event.getX(); 403 iY=event.getY(); 404 break; 405 case 9: 406 dX=event.getX()-iX; 407 if (search.getMinX()+dX>template.getMinX()) 408 dX=(int)(template.getMinX()-search.getMinX()); 409 search.setFrame(search.getMinX()+dX,search.getMinY(),search.getWidth()-dX,search.getHeight()); 410 iX=event.getX(); 411 break; 412 case 10: 413 dX=event.getX()-iX; 414 dY=event.getY()-iY; 415 if (template.getMinX()+dX>template.getMaxX()) 416 dX=(int)(template.getMaxX()-template.getMinX()); 417 if (template.getMinX()+dX<search.getMinX()) 418 dX=(int)(search.getMinX()-template.getMinX()); 419 if (template.getMinY()+dY>template.getMaxY()) 420 dY=(int)(template.getMaxY()-template.getMinY()); 421 if (template.getMinY()+dY<search.getMinY()) 422 dY=(int)(search.getMinY()-template.getMinY()); 423 template.setFrame(template.getMinX()+dX,template.getMinY()+dY,template.getWidth()-dX,template.getHeight()-dY); 424 iX=event.getX(); 425 iY=event.getY(); 426 break; 427 case 11: 428 dY=event.getY()-iY; 429 if (template.getMinY()+dY>template.getMaxY()) 430 dY=(int)(template.getMaxY()-template.getMinY()); 431 if (template.getMinY()+dY<search.getMinY()) 432 dY=(int)(search.getMinY()-template.getMinY()); 433 template.setFrame(template.getMinX(),template.getMinY()+dY,template.getWidth(),template.getHeight()-dY); 434 iY=event.getY(); 435 break; 436 case 12: 437 dX=event.getX()-iX; 438 dY=event.getY()-iY; 439 if (template.getMaxX()+dX<template.getMinX()) 440 dX=(int)(template.getMinX()-template.getMaxX()); 441 if (template.getMaxX()+dX>search.getMaxX()) 442 dX=(int)(search.getMaxX()-template.getMaxX()); 443 if (template.getMinY()+dY>template.getMaxY()) 444 dY=(int)(template.getMaxY()-template.getMinY()); 445 if (template.getMinY()+dY<search.getMinY()) 446 dY=(int)(search.getMinY()-template.getMinY()); 447 template.setFrame(template.getMinX(),template.getMinY()+dY,template.getWidth()+dX,template.getHeight()-dY); 448 iX=event.getX(); 449 iY=event.getY(); 450 break; 451 case 13: 452 dX=event.getX()-iX; 453 if (template.getMaxX()+dX<template.getMinX()) 454 dX=(int)(template.getMinX()-template.getMaxX()); 455 if (template.getMaxX()+dX>search.getMaxX()) 456 dX=(int)(search.getMaxX()-template.getMaxX()); 457 template.setFrame(template.getMinX(),template.getMinY(),template.getWidth()+dX,template.getHeight()); 458 iX=event.getX(); 459 break; 460 case 14: 461 dX=event.getX()-iX; 462 dY=event.getY()-iY; 463 if (template.getMaxX()+dX<template.getMinX()) 464 dX=(int)(template.getMinX()-template.getMaxX()); 465 if (template.getMaxX()+dX>search.getMaxX()) 466 dX=(int)(search.getMaxX()-template.getMaxX()); 467 if (template.getMaxY()+dY<template.getMinY()) 468 dY=(int)(template.getMinY()-template.getMaxY()); 469 if (template.getMaxY()+dY>search.getMaxY()) 470 dY=(int)(search.getMaxY()-template.getMaxY()); 471 template.setFrame(template.getMinX(),template.getMinY(),template.getWidth()+dX,template.getHeight()+dY); 472 iX=event.getX(); 473 iY=event.getY(); 474 break; 475 case 15: 476 dY=event.getY()-iY; 477 if (template.getMaxY()+dY<template.getMinY()) 478 dY=(int)(template.getMinY()-template.getMaxY()); 479 if (template.getMaxY()+dY>search.getMaxY()) 480 dY=(int)(search.getMaxY()-template.getMaxY()); 481 template.setFrame(template.getMinX(),template.getMinY(),template.getWidth(),template.getHeight()+dY); 482 iY=event.getY(); 483 break; 484 case 16: 485 dY=event.getY()-iY; 486 dX=event.getX()-iX; 487 if (template.getMinX()+dX>template.getMaxX()) 488 dX=(int)(template.getMaxX()-template.getMinX()); 489 if (template.getMinX()+dX<search.getMinX()) 490 dX=(int)(search.getMinX()-template.getMinX()); 491 if (template.getMaxY()+dY<template.getMinY()) 492 dY=(int)(template.getMinY()-template.getMaxY()); 493 if (template.getMaxY()+dY>search.getMaxY()) 494 dY=(int)(search.getMaxY()-template.getMaxY()); 495 template.setFrame(template.getMinX()+dX,template.getMinY(),template.getWidth()-dX,template.getHeight()+dY); 496 iX=event.getX(); 497 iY=event.getY(); 498 break; 499 case 17: 500 dX=event.getX()-iX; 501 if (template.getMinX()+dX>template.getMaxX()) 502 dX=(int)(template.getMaxX()-template.getMinX()); 503 if (template.getMinX()+dX<search.getMinX()) 504 dX=(int)(search.getMinX()-template.getMinX()); 505 template.setFrame(template.getMinX()+dX,template.getMinY(),template.getWidth()-dX,template.getHeight()); 506 iX=event.getX(); 507 break; 508 509 } 510 if (useFactor) search.setFrame( 511 template.getMinX()-factor*template.getWidth(), 512 template.getMinY()-factor*template.getHeight(), 513 (2*factor+1)*template.getWidth(), 514 (2*factor+1)*template.getHeight()); 515 if (comp!=null) comp.repaint(); 516 } 517 518 } 519 | Popular Tags |