예제 #1
0
 void addToRoiManager(ImagePlus imp) {
   if (IJ.macroRunning() && Interpreter.isBatchModeRoiManager())
     IJ.error("run(\"Add to Manager\") may not work in batch mode macros");
   Frame frame = WindowManager.getFrame("ROI Manager");
   if (frame == null) IJ.run("ROI Manager...");
   if (imp == null) return;
   Roi roi = imp.getRoi();
   if (roi == null) return;
   frame = WindowManager.getFrame("ROI Manager");
   if (frame == null || !(frame instanceof RoiManager)) IJ.error("ROI Manager not found");
   RoiManager rm = (RoiManager) frame;
   boolean altDown = IJ.altKeyDown();
   IJ.setKeyUp(IJ.ALL_KEYS);
   if (altDown && !IJ.macroRunning()) IJ.setKeyDown(KeyEvent.VK_SHIFT);
   if (roi.getState() == Roi.CONSTRUCTING) { // wait (up to 2 sec.) until ROI finished
     long start = System.currentTimeMillis();
     while (true) {
       IJ.wait(10);
       if (roi.getState() != Roi.CONSTRUCTING) break;
       if ((System.currentTimeMillis() - start) > 2000) {
         IJ.beep();
         IJ.error("Add to Manager", "Selection is not complete");
         return;
       }
     }
   }
   rm.runCommand("add");
   IJ.setKeyUp(IJ.ALL_KEYS);
 }
예제 #2
0
 /**
  * With segmented selections, ignore first mouse up and finalize when user double-clicks,
  * control-clicks or clicks in start box.
  */
 protected void handleMouseUp(int sx, int sy) {
   if (state == MOVING) {
     state = NORMAL;
     return;
   }
   if (state == MOVING_HANDLE) {
     cachedMask = null; // mask is no longer valid
     state = NORMAL;
     updateClipRect();
     oldX = x;
     oldY = y;
     oldWidth = width;
     oldHeight = height;
     return;
   }
   if (state != CONSTRUCTING) return;
   if (IJ.spaceBarDown()) // is user scrolling image?
   return;
   boolean samePoint = false;
   if (xpf != null)
     samePoint = (xpf[nPoints - 2] == xpf[nPoints - 1] && ypf[nPoints - 2] == ypf[nPoints - 1]);
   else samePoint = (xp[nPoints - 2] == xp[nPoints - 1] && yp[nPoints - 2] == yp[nPoints - 1]);
   Rectangle biggerStartBox =
       new Rectangle(ic.screenXD(startXD) - 5, ic.screenYD(startYD) - 5, 10, 10);
   if (nPoints > 2
       && (biggerStartBox.contains(sx, sy)
           || (ic.offScreenXD(sx) == startXD && ic.offScreenYD(sy) == startYD)
           || (samePoint && (System.currentTimeMillis() - mouseUpTime) <= 500))) {
     nPoints--;
     addOffset();
     finishPolygon();
     return;
   } else if (!samePoint) {
     mouseUpTime = System.currentTimeMillis();
     if (type == ANGLE && nPoints == 3) {
       addOffset();
       finishPolygon();
       return;
     }
     // add point to polygon
     if (xpf != null) {
       xpf[nPoints] = xpf[nPoints - 1];
       ypf[nPoints] = ypf[nPoints - 1];
       nPoints++;
       if (nPoints == xpf.length) enlargeArrays();
     } else {
       xp[nPoints] = xp[nPoints - 1];
       yp[nPoints] = yp[nPoints - 1];
       nPoints++;
       if (nPoints == xp.length) enlargeArrays();
     }
     // if (lineWidth>1) fitSpline();
   }
 }
예제 #3
0
 void showFrameRate(Graphics g) {
   frames++;
   if (System.currentTimeMillis() > firstFrame + 1000) {
     firstFrame = System.currentTimeMillis();
     fps = frames;
     frames = 0;
   }
   g.setColor(Color.white);
   g.fillRect(10, 12, 50, 15);
   g.setColor(Color.black);
   g.drawString((int) (fps + 0.5) + " fps", 10, 25);
 }
예제 #4
0
 void resetMaxBounds() {
   ImageWindow win = imp.getWindow();
   if (win != null && (System.currentTimeMillis() - win.setMaxBoundsTime) > 500L) {
     win.setMaximizedBounds(win.maxWindowBounds);
     maxBoundsReset = true;
   }
 }
예제 #5
0
 void setMaxBounds() {
   if (maxBoundsReset) {
     maxBoundsReset = false;
     ImageWindow win = imp.getWindow();
     if (win != null && !IJ.isLinux() && win.maxBounds != null) {
       win.setMaximizedBounds(win.maxBounds);
       win.setMaxBoundsTime = System.currentTimeMillis();
     }
   }
 }
예제 #6
0
 void enlargeArrays() {
   if (xp != null) {
     int[] xptemp = new int[maxPoints * 2];
     int[] yptemp = new int[maxPoints * 2];
     System.arraycopy(xp, 0, xptemp, 0, maxPoints);
     System.arraycopy(yp, 0, yptemp, 0, maxPoints);
     xp = xptemp;
     yp = yptemp;
   }
   if (xpf != null) {
     float[] xpftemp = new float[maxPoints * 2];
     float[] ypftemp = new float[maxPoints * 2];
     System.arraycopy(xpf, 0, xpftemp, 0, maxPoints);
     System.arraycopy(ypf, 0, ypftemp, 0, maxPoints);
     xpf = xpftemp;
     ypf = ypftemp;
   }
   int[] xp2temp = new int[maxPoints * 2];
   int[] yp2temp = new int[maxPoints * 2];
   System.arraycopy(xp2, 0, xp2temp, 0, maxPoints);
   System.arraycopy(yp2, 0, yp2temp, 0, maxPoints);
   xp2 = xp2temp;
   yp2 = yp2temp;
   if (IJ.debugMode) IJ.log("PolygonRoi: " + maxPoints + " points");
   maxPoints *= 2;
 }
예제 #7
0
public class Grid_ implements PlugIn, DialogListener {
  private static String[] colors = {
    "Red", "Green", "Blue", "Magenta", "Cyan", "Yellow", "Orange", "Black", "White"
  };
  private static String color = "Cyan";
  private static String[] types = {"Lines", "Crosses", "Points", "None"};
  private static String type = types[0];
  private static double areaPerPoint;
  private static boolean randomOffset;
  private Random random = new Random(System.currentTimeMillis());
  private ImagePlus imp;
  private double tileWidth, tileHeight;
  private int xstart, ystart;
  private int linesV, linesH;
  private double pixelWidth = 1.0, pixelHeight = 1.0;
  private String units = "pixels";

  public void run(String arg) {
    imp = IJ.getImage();
    showDialog();
  }

  void drawPoints() {
    int one = 1;
    int two = 2;
    GeneralPath path = new GeneralPath();
    for (int h = 0; h < linesV; h++) {
      for (int v = 0; v < linesH; v++) {
        float x = (float) (xstart + h * tileWidth);
        float y = (float) (ystart + v * tileHeight);
        path.moveTo(x - two, y - one);
        path.lineTo(x - two, y + one);
        path.moveTo(x + two, y - one);
        path.lineTo(x + two, y + one);
        path.moveTo(x - one, y - two);
        path.lineTo(x + one, y - two);
        path.moveTo(x - one, y + two);
        path.lineTo(x + one, y + two);
      }
    }
    showGrid(path);
  }

  void drawCrosses() {
    GeneralPath path = new GeneralPath();
    float arm = 5;
    for (int h = 0; h < linesV; h++) {
      for (int v = 0; v < linesH; v++) {
        float x = (float) (xstart + h * tileWidth);
        float y = (float) (ystart + v * tileHeight);
        path.moveTo(x - arm, y);
        path.lineTo(x + arm, y);
        path.moveTo(x, y - arm);
        path.lineTo(x, y + arm);
      }
    }
    showGrid(path);
  }

  void showGrid(Shape shape) {
    ImageCanvas ic = imp.getCanvas();
    if (ic == null) return;
    if (shape == null) ic.setDisplayList(null);
    else ic.setDisplayList(shape, getColor(), null);
  }

  void drawLines() {
    GeneralPath path = new GeneralPath();
    int width = imp.getWidth();
    int height = imp.getHeight();
    for (int i = 0; i < linesV; i++) {
      float xoff = (float) (xstart + i * tileWidth);
      path.moveTo(xoff, 0f);
      path.lineTo(xoff, height);
    }
    for (int i = 0; i < linesH; i++) {
      float yoff = (float) (ystart + i * tileHeight);
      path.moveTo(0f, yoff);
      path.lineTo(width, yoff);
    }
    showGrid(path);
  }

  void showDialog() {
    int width = imp.getWidth();
    int height = imp.getHeight();
    Calibration cal = imp.getCalibration();
    int places;
    if (cal.scaled()) {
      pixelWidth = cal.pixelWidth;
      pixelHeight = cal.pixelHeight;
      units = cal.getUnits();
      places = 2;
    } else {
      pixelWidth = 1.0;
      pixelHeight = 1.0;
      units = "pixels";
      places = 0;
    }
    if (areaPerPoint == 0.0)
      areaPerPoint =
          (width * cal.pixelWidth * height * cal.pixelHeight) / 81.0; // default to 9x9 grid
    ImageWindow win = imp.getWindow();
    GenericDialog gd = new GenericDialog("Grid...");
    gd.addChoice("Grid Type:", types, type);
    gd.addNumericField("Area per Point:", areaPerPoint, places, 6, units + "^2");
    gd.addChoice("Color:", colors, color);
    gd.addCheckbox("Random Offset", randomOffset);
    gd.addDialogListener(this);
    gd.showDialog();
    if (gd.wasCanceled()) showGrid(null);
  }

  public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
    int width = imp.getWidth();
    int height = imp.getHeight();
    type = gd.getNextChoice();
    areaPerPoint = gd.getNextNumber();
    color = gd.getNextChoice();
    randomOffset = gd.getNextBoolean();

    double minArea = (width * height) / 50000.0;
    if (type.equals(types[1]) && minArea < 144.0) minArea = 144.0;
    else if (minArea < 16) minArea = 16.0;
    if (areaPerPoint / (pixelWidth * pixelHeight) < minArea) {
      String err = "\"Area per Point\" too small";
      if (gd.wasOKed()) IJ.error("Grid", err);
      else IJ.showStatus(err);
      return true;
    }
    double tileSize = Math.sqrt(areaPerPoint);
    tileWidth = tileSize / pixelWidth;
    tileHeight = tileSize / pixelHeight;
    if (randomOffset) {
      xstart = (int) (random.nextDouble() * tileWidth);
      ystart = (int) (random.nextDouble() * tileHeight);
    } else {
      xstart = (int) (tileWidth / 2.0 + 0.5);
      ystart = (int) (tileHeight / 2.0 + 0.5);
    }
    linesV = (int) ((width - xstart) / tileWidth) + 1;
    linesH = (int) ((height - ystart) / tileHeight) + 1;
    if (gd.invalidNumber()) return true;
    if (type.equals(types[0])) drawLines();
    else if (type.equals(types[1])) drawCrosses();
    else if (type.equals(types[2])) drawPoints();
    else showGrid(null);
    return true;
  }

  Color getColor() {
    Color c = Color.cyan;
    if (color.equals(colors[0])) c = Color.red;
    else if (color.equals(colors[1])) c = Color.green;
    else if (color.equals(colors[2])) c = Color.blue;
    else if (color.equals(colors[3])) c = Color.magenta;
    else if (color.equals(colors[4])) c = Color.cyan;
    else if (color.equals(colors[5])) c = Color.yellow;
    else if (color.equals(colors[6])) c = Color.orange;
    else if (color.equals(colors[7])) c = Color.black;
    else if (color.equals(colors[8])) c = Color.white;
    return c;
  }
}