1 package JSci.instruments; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.awt.geom.*; 6 7 9 10 public class RectangularROI extends ROIAdapter { 11 12 private static final int LW = 3; 13 private int minx,maxx,miny,maxy; 14 15 public RectangularROI(int x0,int y0,int x1,int y1) { 16 minx=x0; 17 maxx=x1; 18 miny=y0; 19 maxy=y1; 20 } 21 22 public Shape getShape() { 23 return new Rectangle(minx,miny,maxx-minx+1,maxy-miny+1); 24 } 25 26 private Component comp = null; 27 public void setComponent(Component c) { 28 if (comp!=null) { 29 comp.removeMouseListener((MouseListener)this); 30 comp.removeMouseMotionListener((MouseMotionListener)this); 31 } 32 comp=c; 33 if (comp!=null) { 34 comp.addMouseListener((MouseListener)this); 35 comp.addMouseMotionListener((MouseMotionListener)this); 36 } 37 } 38 39 public void paint(Graphics g) { 40 g.setColor(Color.GREEN); 41 ((Graphics2D)g).draw(getShape()); 42 } 43 44 private int region(Point p) { 45 if (p.x<minx || p.x>maxx || p.y<miny || p.y>maxy) return 0; 46 if (p.y<=miny+LW) { 47 if (p.x<=minx+LW) return 1; 48 if (p.x<maxx-LW) return 2; 49 return 3; 50 } 51 if (p.y<maxy-LW) { 52 if (p.x<=minx+LW) return 4; 53 if (p.x<maxx-LW) return 5; 54 return 6; 55 } 56 if (p.x<=minx+LW) return 7; 57 if (p.x<maxx-LW) return 8; 58 return 9; 59 } 60 61 private int state,iX,iY,vminx,vmaxx,vminy,vmaxy; 62 63 public void mousePressed(MouseEvent event) { 64 state = region(event.getPoint()); 65 iX=event.getX(); 66 iY=event.getY(); 67 vminx=minx; 68 vmaxx=maxx; 69 vminy=miny; 70 vmaxy=maxy; 71 comp.repaint(); 72 } 73 74 public void mouseReleased(MouseEvent event) { 75 state = 0; 76 } 77 78 public void mouseMoved(MouseEvent event) { 79 switch (region(event.getPoint())) { 80 case 0:comp.setCursor(Cursor.getDefaultCursor());break; 81 case 1:comp.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));break; 82 case 2:comp.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));break; 83 case 3:comp.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));break; 84 case 4:comp.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));break; 85 case 5:comp.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));break; 86 case 6:comp.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));break; 87 case 7:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));break; 88 case 8:comp.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));break; 89 case 9:comp.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));break; 90 } 91 } 92 93 private int clip(int min,int x,int max) { 94 if (x<min) return min; 95 if (x>max) return max; 96 return x; 97 } 98 99 public void mouseDragged(MouseEvent event) { 100 int fX,fY,dX,dY; 101 fX=event.getX(); 102 fY=event.getY();; 103 dX=fX-iX; 104 dY=fY-iY; 105 switch (state) { 106 case 0: 107 minx=Math.min(fX,iX); 108 maxx=Math.max(fX,iX); 109 miny=Math.min(fY,iY); 110 maxy=Math.max(fY,iY); 111 break; 112 case 1: 113 minx=clip(0,vminx+dX,maxx); 114 miny=clip(0,vminy+dY,maxy); 115 break; 116 case 2: 117 miny=clip(0,vminy+dY,maxy); 118 break; 119 case 3: 120 maxx=clip(minx,vmaxx+dX,comp.getWidth()-1); 121 miny=clip(0,vminy+dY,maxy); 122 break; 123 case 4: 124 minx=clip(0,vminx+dX,maxx); 125 break; 126 case 5: 127 if (vminx+dX<0) dX=-vminx; 128 if (vmaxx+dX>=comp.getWidth()) dX=comp.getWidth()-vmaxx-1; 129 if (vminy+dY<0) dY=-vminy; 130 if (vmaxy+dY>=comp.getHeight()) dY=comp.getHeight()-vmaxy-1; 131 minx=vminx+dX; 132 maxx=vmaxx+dX; 133 miny=vminy+dY; 134 maxy=vmaxy+dY; 135 break; 136 case 6: 137 maxx=clip(minx,vmaxx+dX,comp.getWidth()-1); 138 break; 139 case 7: 140 minx=clip(0,vminx+dX,maxx); 141 maxy=clip(miny,vmaxy+dY,comp.getHeight()-1); 142 break; 143 case 8: 144 maxy=clip(miny,vmaxy+dY,comp.getHeight()-1); 145 break; 146 case 9: 147 maxx=clip(minx,vmaxx+dX,comp.getWidth()-1); 148 maxy=clip(miny,vmaxy+dY,comp.getHeight()-1); 149 break; 150 151 } 152 comp.repaint(); 153 } 154 155 } 156 157 | Popular Tags |