Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Line.java
1 package ij.gui;
2 
3 import java.awt.*;
4 import java.awt.image.*;
5 import ij.*;
6 import ij.process.*;
7 import ij.measure.*;
8 import java.awt.event.KeyEvent;
9 
11 public class Line extends Roi {
12 
13  public int x1, y1, x2, y2; // the line
14  private int x1R, y1R, x2R, y2R; // the line, relative to base of bounding rect
15  private static int lineWidth = 1;
16 
19  public Line(int ox1, int oy1, int ox2, int oy2) {
20  this(ox1, oy1, null);
21  grow(ox2, oy2);
22  x1=x+x1R; y1=y+y1R; x2=x+x2R; y2=y+y2R;
23  state = NORMAL;
24  }
25 
30  public Line(int ox, int oy, ImagePlus imp) {
31  super(ox, oy, imp);
32  x1R = 0; y1R = 0;
33  type = LINE;
34  }
35 
37  public Line(int ox1, int oy1, int ox2, int oy2, ImagePlus imp) {
38  this(ox1, oy1, ox2, oy2);
39  setImage(imp);
40  }
41 
42  protected void grow(int xend, int yend) {
43  if (xend<0) xend=0; if (yend<0) yend=0;
44  if (xend>xMax) xend=xMax; if (yend>yMax) yend=yMax;
45  int xstart=x+x1R, ystart=y+y1R;
46  if (constrain) {
47  int dx = Math.abs(xend-xstart);
48  int dy = Math.abs(yend-ystart);
49  if (dx>=dy)
50  yend = ystart;
51  else
52  xend = xstart;
53  }
54  x=Math.min(x+x1R,xend); y=Math.min(y+y1R,yend);
55  x1R=xstart-x; y1R=ystart-y;
56  x2R=xend-x; y2R=yend-y;
57  width=Math.abs(x2R-x1R); height=Math.abs(y2R-y1R);
58  if (width<1) width=1; if (height<1) height=1;
59  updateClipRect();
60  if (imp!=null)
61  imp.draw(clipX, clipY, clipWidth, clipHeight);
62  oldX=x; oldY=y;
63  oldWidth=width; oldHeight=height;
64  }
65 
66  protected void moveHandle(int ox, int oy) {
67  x1=x+x1R; y1=y+y1R; x2=x+x2R; y2=y+y2R;
68  switch (activeHandle) {
69  case 0: x1=ox; y1=oy; break;
70  case 1: x2=ox; y2=oy; break;
71  case 2:
72  int dx = ox-(x1+(x2-x1)/2);
73  int dy = oy-(y1+(y2-y1)/2);
74  x1+=dx; y1+=dy; x2+=dx; y2+=dy;
75  break;
76  }
77  if (constrain) {
78  int dx = Math.abs(x1-x2);
79  int dy = Math.abs(y1-y2);
80  if (activeHandle==0) {
81  if (dx>=dy) y1= y2; else x1 = x2;
82  } else if (activeHandle==1) {
83  if (dx>=dy) y2= y1; else x2 = x1;
84  }
85  }
86  x=Math.min(x1,x2); y=Math.min(y1,y2);
87  x1R=x1-x; y1R=y1-y;
88  x2R=x2-x; y2R=y2-y;
89  width=Math.abs(x2R-x1R); height=Math.abs(y2R-y1R);
90  updateClipRect();
91  imp.draw(clipX, clipY, clipWidth, clipHeight);
92  oldX = x;
93  oldY = y;
94  oldWidth = width;
95  oldHeight = height;
96  }
97 
98  protected void mouseDownInHandle(int handle, int sx, int sy) {
99  state = MOVING_HANDLE;
100  activeHandle = handle;
101  ic.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
102  }
103 
105  public void draw(Graphics g) {
106  g.setColor(ROIColor);
107  x1=x+x1R; y1=y+y1R; x2=x+x2R; y2=y+y2R;
108  int sx1 = ic.screenX(x1);
109  int sy1 = ic.screenY(y1);
110  int sx2 = ic.screenX(x2);
111  int sy2 = ic.screenY(y2);
112  int sx3 = sx1 + (sx2-sx1)/2;
113  int sy3 = sy1 + (sy2-sy1)/2;
114  g.drawLine(sx1, sy1, sx2, sy2);
115  if (state!=CONSTRUCTING) {
116  int size2 = HANDLE_SIZE/2;
117  if (ic!=null) mag = ic.getMagnification();
118  drawHandle(g, sx1-size2, sy1-size2);
119  drawHandle(g, sx2-size2, sy2-size2);
120  drawHandle(g, sx3-size2, sy3-size2);
121  }
122  IJ.showStatus(imp.getLocationAsString(x2,y2)+", angle=" + IJ.d2s(getAngle(x1,y1,x2,y2)) + ", length=" + IJ.d2s(getLength()));
123  if (updateFullWindow)
124  {updateFullWindow = false; imp.draw();}
125  }
126 
128  public double getLength() {
129  Calibration cal = imp.getCalibration();
130  return Math.sqrt((x2-x1)*cal.pixelWidth*(x2-x1)*cal.pixelWidth
131  + (y2-y1)*cal.pixelHeight*(y2-y1)*cal.pixelHeight);
132  }
133 
135  public double getRawLength() {
136  return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
137  }
138 
140  public double[] getPixels() {
141  ImageProcessor ip = imp.getProcessor();
142  double[] line = ip.getLine(x1, y1, x2, y2);
143  return line;
144  }
145 
146  public void drawPixels() {
147  ImageProcessor ip = imp.getProcessor();
148  ip.moveTo(x1, y1);
149  ip.lineTo(x2, y2);
150  if (Line.getWidth()>1)
151  updateFullWindow = true;
152  }
153 
154  public boolean contains(int x, int y) {
155  return false;
156  }
157 
160  public int isHandle(int sx, int sy) {
161  int size = HANDLE_SIZE+5;
162  int halfSize = size/2;
163  int sx1 = ic.screenX(x+x1R) - halfSize;
164  int sy1 = ic.screenY(y+y1R) - halfSize;
165  int sx2 = ic.screenX(x+x2R) - halfSize;
166  int sy2 = ic.screenY(y+y2R) - halfSize;
167  int sx3 = sx1 + (sx2-sx1)/2-1;
168  int sy3 = sy1 + (sy2-sy1)/2-1;
169  if (sx>=sx1&&sx<=sx1+size&&sy>=sy1&&sy<=sy1+size) return 0;
170  if (sx>=sx2&&sx<=sx2+size&&sy>=sy2&&sy<=sy2+size) return 1;
171  if (sx>=sx3&&sx<=sx3+size+2&&sy>=sy3&&sy<=sy3+size+2) return 2;
172  return -1;
173  }
174 
175  public static int getWidth() {
176  return lineWidth;
177  }
178 
179  public static void setWidth(int w) {
180  if (w<1) w = 1;
181  if (w>99) w = 99;
182  lineWidth = w;
183  }
184 
186  public void nudgeCorner(int key) {
187  switch(key) {
188  case KeyEvent.VK_UP: y2R--; break;
189  case KeyEvent.VK_DOWN: y2R++; break;
190  case KeyEvent.VK_LEFT: x2R--; break;
191  case KeyEvent.VK_RIGHT: x2R++; break;
192  }
193  grow(x+x2R,y+y2R);
194  }
195 
196 
197 }