コード例 #1
0
ファイル: SpecifyROI.java プロジェクト: dannyZhou/imagej
 /**
  * Creates a dialog box, allowing the user to enter the requested width, height, x & y
  * coordinates, slice number for a Region Of Interest, option for oval, and option for whether x &
  * y coordinates to be centered.
  */
 void showDialog() {
   Calibration cal = imp.getCalibration();
   int digits = 0;
   if (scaledUnits && cal.scaled()) digits = 2;
   Roi roi = imp.getRoi();
   if (roi == null) drawRoi();
   GenericDialog gd = new GenericDialog("Specify");
   gd.addNumericField("Width:", width, digits);
   gd.addNumericField("Height:", height, digits);
   gd.addNumericField("X coordinate:", xRoi, digits);
   gd.addNumericField("Y coordinate:", yRoi, digits);
   if (stackSize > 1) gd.addNumericField("Slice:", iSlice, 0);
   gd.addCheckbox("Oval", oval);
   gd.addCheckbox("Constrain square/circle", square);
   gd.addCheckbox("Centered", centered);
   if (cal.scaled()) {
     boolean unitsMatch = cal.getXUnit().equals(cal.getYUnit());
     String units = unitsMatch ? cal.getUnits() : cal.getXUnit() + " x " + cal.getYUnit();
     gd.addCheckbox("Scaled units (" + units + ")", scaledUnits);
   }
   fields = gd.getNumericFields();
   gd.addDialogListener(this);
   gd.showDialog();
   if (gd.wasCanceled()) {
     if (roi == null) imp.deleteRoi();
     else // *ALWAYS* restore initial ROI when cancelled
     imp.setRoi(roi);
   }
 }
コード例 #2
0
ファイル: SpecifyROI.java プロジェクト: dannyZhou/imagej
  public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
    if (IJ.isMacOSX()) IJ.wait(50);
    Calibration cal = imp.getCalibration();
    width = gd.getNextNumber();
    height = gd.getNextNumber();
    xRoi = gd.getNextNumber();
    yRoi = gd.getNextNumber();
    if (stackSize > 1) iSlice = (int) gd.getNextNumber();
    oval = gd.getNextBoolean();
    square = gd.getNextBoolean();
    centered = gd.getNextBoolean();
    if (cal.scaled()) scaledUnits = gd.getNextBoolean();
    if (gd.invalidNumber() || width <= 0 || height <= 0) return false;
    //
    Vector numFields = gd.getNumericFields();
    Vector checkboxes = gd.getCheckboxes();
    boolean newWidth = false, newHeight = false, newXY = false;
    if (e != null && e.getSource() == checkboxes.get(SQUARE) && square) {
      width = 0.5 * (width + height); // make square: same width&height
      height = width;
      newWidth = true;
      newHeight = true;
    }
    if (e != null && e.getSource() == checkboxes.get(CENTERED)) {
      double shiftBy = centered ? 0.5 : -0.5; // 'centered' changed:
      xRoi += shiftBy * width; // shift x, y to keep roi the same
      yRoi += shiftBy * height;
      newXY = true;
    }
    if (square && width != height && e != null) { // in 'square' mode, synchronize width&height
      if (e.getSource() == numFields.get(WIDTH)) {
        height = width;
        newHeight = true;
      } else if (e.getSource() == numFields.get(HEIGHT)) {
        width = height;
        newWidth = true;
      }
    }
    if (e != null && cal.scaled() && e.getSource() == checkboxes.get(SCALED_UNITS)) {
      double xFactor = scaledUnits ? cal.pixelWidth : 1. / cal.pixelWidth;
      double yFactor = scaledUnits ? cal.pixelHeight : 1. / cal.pixelHeight;
      width *= xFactor; // transform everything to keep roi the same
      height *= yFactor;
      xRoi *= xFactor;
      yRoi *= yFactor;
      newWidth = true;
      newHeight = true;
      newXY = true;
    }
    int digits = (scaledUnits || (int) width != width) ? 2 : 0;
    if (newWidth) ((TextField) (numFields.get(WIDTH))).setText(IJ.d2s(width, digits));
    if (newHeight) ((TextField) (numFields.get(HEIGHT))).setText(IJ.d2s(height, digits));
    digits = (scaledUnits || (int) xRoi != xRoi || (int) yRoi != yRoi) ? 2 : 0;
    if (newXY) {
      ((TextField) (numFields.get(X_ROI))).setText(IJ.d2s(xRoi, digits));
      ((TextField) (numFields.get(Y_ROI))).setText(IJ.d2s(yRoi, digits));
    }

    if (stackSize > 1 && iSlice > 0 && iSlice <= stackSize) imp.setSlice(iSlice);
    if (!newWidth && !newHeight && !newXY) // don't draw if an update will come immediately
    drawRoi();
    return true;
  }