/** Sets the cursor based on the current tool and cursor location. */ public void setCursor(int sx, int sy, int ox, int oy) { xMouse = ox; yMouse = oy; mouseExited = false; Roi roi = imp.getRoi(); ImageWindow win = imp.getWindow(); if (win == null) return; if (IJ.spaceBarDown()) { setCursor(handCursor); return; } int id = Toolbar.getToolId(); switch (Toolbar.getToolId()) { case Toolbar.MAGNIFIER: setCursor(moveCursor); break; case Toolbar.HAND: setCursor(handCursor); break; default: // selection tool if (id == Toolbar.SPARE1 || id >= Toolbar.SPARE2) { if (Prefs.usePointerCursor) setCursor(defaultCursor); else setCursor(crosshairCursor); } else if (roi != null && roi.getState() != roi.CONSTRUCTING && roi.isHandle(sx, sy) >= 0) setCursor(handCursor); else if (Prefs.usePointerCursor || (roi != null && roi.getState() != roi.CONSTRUCTING && roi.contains(ox, oy))) setCursor(defaultCursor); else setCursor(crosshairCursor); } }
public void mouseMoved(MouseEvent e) { // if (ij==null) return; int sx = e.getX(); int sy = e.getY(); int ox = offScreenX(sx); int oy = offScreenY(sy); flags = e.getModifiers(); setCursor(sx, sy, ox, oy); IJ.setInputEvent(e); Roi roi = imp.getRoi(); if (roi != null && (roi.getType() == Roi.POLYGON || roi.getType() == Roi.POLYLINE || roi.getType() == Roi.ANGLE) && roi.getState() == roi.CONSTRUCTING) { PolygonRoi pRoi = (PolygonRoi) roi; pRoi.handleMouseMove(ox, oy); } else { if (ox < imageWidth && oy < imageHeight) { ImageWindow win = imp.getWindow(); // Cursor must move at least 12 pixels before text // displayed using IJ.showStatus() is overwritten. if ((sx - sx2) * (sx - sx2) + (sy - sy2) * (sy - sy2) > 144) showCursorStatus = true; if (win != null && showCursorStatus) win.mouseMoved(ox, oy); } else IJ.showStatus(""); } }
/** * Zooms out by making the source rectangle (srcRect) larger and centering it on (x,y). If we * can't make it larger, then make the window smaller. */ public void zoomOut(int x, int y) { if (magnification <= 0.03125) return; double oldMag = magnification; double newMag = getLowerZoomLevel(magnification); double srcRatio = (double) srcRect.width / srcRect.height; double imageRatio = (double) imageWidth / imageHeight; double initialMag = imp.getWindow().getInitialMagnification(); if (Math.abs(srcRatio - imageRatio) > 0.05) { double scale = oldMag / newMag; int newSrcWidth = (int) Math.round(srcRect.width * scale); int newSrcHeight = (int) Math.round(srcRect.height * scale); if (newSrcWidth > imageWidth) newSrcWidth = imageWidth; if (newSrcHeight > imageHeight) newSrcHeight = imageHeight; int newSrcX = srcRect.x - (newSrcWidth - srcRect.width) / 2; int newSrcY = srcRect.y - (newSrcHeight - srcRect.height) / 2; if (newSrcX < 0) newSrcX = 0; if (newSrcY < 0) newSrcY = 0; srcRect = new Rectangle(newSrcX, newSrcY, newSrcWidth, newSrcHeight); // IJ.log(newMag+" "+srcRect+" "+dstWidth+" "+dstHeight); int newDstWidth = (int) (srcRect.width * newMag); int newDstHeight = (int) (srcRect.height * newMag); setMagnification(newMag); setMaxBounds(); // IJ.log(newDstWidth+" "+dstWidth+" "+newDstHeight+" "+dstHeight); if (newDstWidth < dstWidth || newDstHeight < dstHeight) { // IJ.log("pack"); setDrawingSize(newDstWidth, newDstHeight); imp.getWindow().pack(); } else repaint(); return; } if (imageWidth * newMag > dstWidth) { int w = (int) Math.round(dstWidth / newMag); if (w * newMag < dstWidth) w++; int h = (int) Math.round(dstHeight / newMag); if (h * newMag < dstHeight) h++; x = offScreenX(x); y = offScreenY(y); Rectangle r = new Rectangle(x - w / 2, y - h / 2, w, h); if (r.x < 0) r.x = 0; if (r.y < 0) r.y = 0; if (r.x + w > imageWidth) r.x = imageWidth - w; if (r.y + h > imageHeight) r.y = imageHeight - h; srcRect = r; } else { srcRect = new Rectangle(0, 0, imageWidth, imageHeight); setDrawingSize((int) (imageWidth * newMag), (int) (imageHeight * newMag)); // setDrawingSize(dstWidth/2, dstHeight/2); imp.getWindow().pack(); } // IJ.write(newMag + " " + srcRect.x+" "+srcRect.y+" "+srcRect.width+" "+srcRect.height+" // "+dstWidth + " " + dstHeight); setMagnification(newMag); // IJ.write(srcRect.x + " " + srcRect.width + " " + dstWidth); setMaxBounds(); repaint(); }
void updateImage(ImagePlus imp) { this.imp = imp; int width = imp.getWidth(); int height = imp.getHeight(); imageWidth = width; imageHeight = height; srcRect = new Rectangle(0, 0, imageWidth, imageHeight); setDrawingSize(imageWidth, (int) imageHeight); magnification = 1.0; }
/** Sets the color used used for the ROI Manager "Show All" mode. */ public static void setShowAllColor(Color c) { if (c == null) return; showAllColor = c; labelColor = null; ImagePlus img = WindowManager.getCurrentImage(); if (img != null) { ImageCanvas ic = img.getCanvas(); if (ic != null && ic.getShowAllROIs()) img.draw(); } }
/** Implements the Image/Zoom/Original Scale command. */ public void unzoom() { double imag = imp.getWindow().getInitialMagnification(); if (magnification == imag) return; srcRect = new Rectangle(0, 0, imageWidth, imageHeight); ImageWindow win = imp.getWindow(); setDrawingSize((int) (imageWidth * imag), (int) (imageHeight * imag)); setMagnification(imag); setMaxBounds(); win.pack(); setMaxBounds(); repaint(); }
private boolean ignoreArrowKeys(ImagePlus imp) { Frame frame = WindowManager.getFrontWindow(); String title = frame.getTitle(); if (title != null && title.equals("ROI Manager")) return true; // Control Panel? if (frame != null && frame instanceof javax.swing.JFrame) return true; ImageWindow win = imp.getWindow(); // LOCI Data Browser window? if (imp.getStackSize() > 1 && win != null && win.getClass().getName().startsWith("loci")) return true; return false; }
public ImageCanvas(ImagePlus imp) { this.imp = imp; ij = IJ.getInstance(); int width = imp.getWidth(); int height = imp.getHeight(); imageWidth = width; imageHeight = height; srcRect = new Rectangle(0, 0, imageWidth, imageHeight); setDrawingSize(imageWidth, (int) (imageHeight)); magnification = 1.0; addMouseListener(this); addMouseMotionListener(this); addKeyListener(ij); // ImageJ handles keyboard shortcuts setFocusTraversalKeysEnabled(false); }
/** Enlarge the canvas if the user enlarges the window. */ void resizeCanvas(int width, int height) { ImageWindow win = imp.getWindow(); // IJ.log("resizeCanvas: "+srcRect+" "+imageWidth+" "+imageHeight+" "+width+" "+height+" // "+dstWidth+" "+dstHeight+" "+win.maxBounds); if (!maxBoundsReset && (width > dstWidth || height > dstHeight) && win != null && win.maxBounds != null && width != win.maxBounds.width - 10) { if (resetMaxBoundsCount != 0) resetMaxBounds(); // Works around problem that prevented window from being larger than // maximized size resetMaxBoundsCount++; } if (IJ.altKeyDown()) { fitToWindow(); return; } if (srcRect.width < imageWidth || srcRect.height < imageHeight) { if (width > imageWidth * magnification) width = (int) (imageWidth * magnification); if (height > imageHeight * magnification) height = (int) (imageHeight * magnification); setDrawingSize(width, height); srcRect.width = (int) (dstWidth / magnification); srcRect.height = (int) (dstHeight / magnification); if ((srcRect.x + srcRect.width) > imageWidth) srcRect.x = imageWidth - srcRect.width; if ((srcRect.y + srcRect.height) > imageHeight) srcRect.y = imageHeight - srcRect.height; repaint(); } // IJ.log("resizeCanvas2: "+srcRect+" "+dstWidth+" "+dstHeight+" "+width+" "+height); }
void resetMaxBounds() { ImageWindow win = imp.getWindow(); if (win != null && (System.currentTimeMillis() - win.setMaxBoundsTime) > 500L) { win.setMaximizedBounds(win.maxWindowBounds); maxBoundsReset = true; } }
public void mouseExited(MouseEvent e) { // autoScroll(e); ImageWindow win = imp.getWindow(); if (win != null) setCursor(defaultCursor); IJ.showStatus(""); mouseExited = true; }
protected void handleRoiMouseDown(MouseEvent e) { int sx = e.getX(); int sy = e.getY(); int ox = offScreenX(sx); int oy = offScreenY(sy); Roi roi = imp.getRoi(); int handle = roi != null ? roi.isHandle(sx, sy) : -1; boolean multiPointMode = roi != null && (roi instanceof PointRoi) && handle == -1 && Toolbar.getToolId() == Toolbar.POINT && Toolbar.getMultiPointMode(); if (multiPointMode) { imp.setRoi(((PointRoi) roi).addPoint(ox, oy)); return; } setRoiModState(e, roi, handle); if (roi != null) { if (handle >= 0) { roi.mouseDownInHandle(handle, sx, sy); return; } Rectangle r = roi.getBounds(); int type = roi.getType(); if (type == Roi.RECTANGLE && r.width == imp.getWidth() && r.height == imp.getHeight() && roi.getPasteMode() == Roi.NOT_PASTING && !(roi instanceof ImageRoi)) { imp.killRoi(); return; } if (roi.contains(ox, oy)) { if (roi.modState == Roi.NO_MODS) roi.handleMouseDown(sx, sy); else { imp.killRoi(); imp.createNewRoi(sx, sy); } return; } if ((type == Roi.POLYGON || type == Roi.POLYLINE || type == Roi.ANGLE) && roi.getState() == roi.CONSTRUCTING) return; int tool = Toolbar.getToolId(); if ((tool == Toolbar.POLYGON || tool == Toolbar.POLYLINE || tool == Toolbar.ANGLE) && !(IJ.shiftKeyDown() || IJ.altKeyDown())) { imp.killRoi(); return; } } imp.createNewRoi(sx, sy); }
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(); } } }
// Use double buffer to reduce flicker when drawing complex ROIs. // Author: Erik Meijering void paintDoubleBuffered(Graphics g) { final int srcRectWidthMag = (int) (srcRect.width * magnification); final int srcRectHeightMag = (int) (srcRect.height * magnification); if (offScreenImage == null || offScreenWidth != srcRectWidthMag || offScreenHeight != srcRectHeightMag) { offScreenImage = createImage(srcRectWidthMag, srcRectHeightMag); offScreenWidth = srcRectWidthMag; offScreenHeight = srcRectHeightMag; } Roi roi = imp.getRoi(); try { if (imageUpdated) { imageUpdated = false; imp.updateImage(); } Graphics offScreenGraphics = offScreenImage.getGraphics(); Java2.setBilinearInterpolation(offScreenGraphics, Prefs.interpolateScaledImages); Image img = imp.getImage(); if (img != null) offScreenGraphics.drawImage( img, 0, 0, srcRectWidthMag, srcRectHeightMag, srcRect.x, srcRect.y, srcRect.x + srcRect.width, srcRect.y + srcRect.height, null); if (overlay != null) drawOverlay(offScreenGraphics); if (showAllROIs) drawAllROIs(offScreenGraphics); if (roi != null) drawRoi(roi, offScreenGraphics); if (srcRect.width < imageWidth || srcRect.height < imageHeight) drawZoomIndicator(offScreenGraphics); if (IJ.debugMode) showFrameRate(offScreenGraphics); g.drawImage(offScreenImage, 0, 0, null); } catch (OutOfMemoryError e) { IJ.outOfMemory("Paint"); } }
public void mouseReleased(MouseEvent e) { flags = e.getModifiers(); flags &= ~InputEvent.BUTTON1_MASK; // make sure button 1 bit is not set flags &= ~InputEvent.BUTTON2_MASK; // make sure button 2 bit is not set flags &= ~InputEvent.BUTTON3_MASK; // make sure button 3 bit is not set Roi roi = imp.getRoi(); if (roi != null) { Rectangle r = roi.getBounds(); int type = roi.getType(); if ((r.width == 0 || r.height == 0) && !(type == Roi.POLYGON || type == Roi.POLYLINE || type == Roi.ANGLE || type == Roi.LINE) && !(roi instanceof TextRoi) && roi.getState() == roi.CONSTRUCTING && type != roi.POINT) imp.killRoi(); else { roi.handleMouseUp(e.getX(), e.getY()); if (roi.getType() == Roi.LINE && roi.getLength() == 0.0) imp.killRoi(); } } }
private boolean deleteOverlayRoi(ImagePlus imp) { if (imp == null) return false; Overlay overlay = null; ImageCanvas ic = imp.getCanvas(); if (ic != null) overlay = ic.getShowAllList(); if (overlay == null) overlay = imp.getOverlay(); if (overlay == null) return false; Roi roi = imp.getRoi(); for (int i = 0; i < overlay.size(); i++) { Roi roi2 = overlay.get(i); if (roi2 == roi) { overlay.remove(i); imp.deleteRoi(); ic = imp.getCanvas(); if (ic != null) ic.roiManagerSelect(roi, true); return true; } } return false; }
void drawAllROIs(Graphics g) { RoiManager rm = RoiManager.getInstance(); if (rm == null) { rm = Interpreter.getBatchModeRoiManager(); if (rm != null && rm.getList().getItemCount() == 0) rm = null; } if (rm == null) { // if (showAllList!=null) // overlay = showAllList; showAllROIs = false; repaint(); return; } initGraphics(g, null, showAllColor); Hashtable rois = rm.getROIs(); java.awt.List list = rm.getList(); boolean drawLabels = rm.getDrawLabels(); currentRoi = null; int n = list.getItemCount(); if (IJ.debugMode) IJ.log("paint: drawing " + n + " \"Show All\" ROIs"); if (labelRects == null || labelRects.length != n) labelRects = new Rectangle[n]; if (!drawLabels) showAllList = new Overlay(); else showAllList = null; if (imp == null) return; int currentImage = imp.getCurrentSlice(); int channel = 0, slice = 0, frame = 0; boolean hyperstack = imp.isHyperStack(); if (hyperstack) { channel = imp.getChannel(); slice = imp.getSlice(); frame = imp.getFrame(); } drawNames = Prefs.useNamesAsLabels; for (int i = 0; i < n; i++) { String label = list.getItem(i); Roi roi = (Roi) rois.get(label); if (roi == null) continue; if (showAllList != null) showAllList.add(roi); if (i < 200 && drawLabels && roi == imp.getRoi()) currentRoi = roi; if (Prefs.showAllSliceOnly && imp.getStackSize() > 1) { if (hyperstack && roi.getPosition() == 0) { int c = roi.getCPosition(); int z = roi.getZPosition(); int t = roi.getTPosition(); if ((c == 0 || c == channel) && (z == 0 || z == slice) && (t == 0 || t == frame)) drawRoi(g, roi, drawLabels ? i : -1); } else { int position = roi.getPosition(); if (position == 0) position = getSliceNumber(roi.getName()); if (position == 0 || position == currentImage) drawRoi(g, roi, drawLabels ? i : -1); } } else drawRoi(g, roi, drawLabels ? i : -1); } ((Graphics2D) g).setStroke(Roi.onePixelWide); drawNames = false; }
protected void setDrawingColor(int ox, int oy, boolean setBackground) { // IJ.log("setDrawingColor: "+setBackground+this); int type = imp.getType(); int[] v = imp.getPixel(ox, oy); switch (type) { case ImagePlus.GRAY8: { if (setBackground) setBackgroundColor(getColor(v[0])); else setForegroundColor(getColor(v[0])); break; } case ImagePlus.GRAY16: case ImagePlus.GRAY32: { double min = imp.getProcessor().getMin(); double max = imp.getProcessor().getMax(); double value = (type == ImagePlus.GRAY32) ? Float.intBitsToFloat(v[0]) : v[0]; int index = (int) (255.0 * ((value - min) / (max - min))); if (index < 0) index = 0; if (index > 255) index = 255; if (setBackground) setBackgroundColor(getColor(index)); else setForegroundColor(getColor(index)); break; } case ImagePlus.COLOR_RGB: case ImagePlus.COLOR_256: { Color c = new Color(v[0], v[1], v[2]); if (setBackground) setBackgroundColor(c); else setForegroundColor(c); break; } } Color c; if (setBackground) c = Toolbar.getBackgroundColor(); else { c = Toolbar.getForegroundColor(); imp.setColor(c); } IJ.showStatus("(" + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() + ")"); }
void abortPluginOrMacro(ImagePlus imp) { if (imp != null) { ImageWindow win = imp.getWindow(); if (win != null) { win.running = false; win.running2 = false; } } Macro.abort(); Interpreter.abort(); if (Interpreter.getInstance() != null) IJ.beep(); }
public void paint(Graphics g) { Roi roi = imp.getRoi(); if (roi != null || showAllROIs || overlay != null) { if (roi != null) roi.updatePaste(); if (!IJ.isMacOSX() && imageWidth != 0) { paintDoubleBuffered(g); return; } } try { if (imageUpdated) { imageUpdated = false; imp.updateImage(); } Java2.setBilinearInterpolation(g, Prefs.interpolateScaledImages); Image img = imp.getImage(); if (img != null) g.drawImage( img, 0, 0, (int) (srcRect.width * magnification), (int) (srcRect.height * magnification), srcRect.x, srcRect.y, srcRect.x + srcRect.width, srcRect.y + srcRect.height, null); if (overlay != null) drawOverlay(g); if (showAllROIs) drawAllROIs(g); if (roi != null) drawRoi(roi, g); if (srcRect.width < imageWidth || srcRect.height < imageHeight) drawZoomIndicator(g); if (IJ.debugMode) showFrameRate(g); } catch (OutOfMemoryError e) { IJ.outOfMemory("Paint"); } }
boolean roiManagerSelect(int x, int y) { RoiManager rm = RoiManager.getInstance(); if (rm == null) return false; Hashtable rois = rm.getROIs(); java.awt.List list = rm.getList(); int n = list.getItemCount(); if (labelRects == null || labelRects.length != n) return false; boolean stackMode = imp != null && imp.getStackSize() > 1 && Prefs.showAllSliceOnly; for (int i = 0; i < n; i++) { if (labelRects[i] != null && labelRects[i].contains(x, y)) { if (stackMode) { int slice = getSliceNumber(list.getItem(i)); if (slice != imp.getCurrentSlice()) continue; } // rm.select(i); // this needs to run on a separate thread, at least on OS X // "update2" does not clone the ROI so the "Show All" // outline moves as the user moves the RO. new ij.macro.MacroRunner("roiManager('select', " + i + "); roiManager('update2');"); return true; } } return false; }
public void mouseDragged(MouseEvent e) { int x = e.getX(); int y = e.getY(); xMouse = offScreenX(x); yMouse = offScreenY(y); flags = e.getModifiers(); // IJ.log("mouseDragged: "+flags); if (flags == 0) // workaround for Mac OS 9 bug flags = InputEvent.BUTTON1_MASK; if (Toolbar.getToolId() == Toolbar.HAND || IJ.spaceBarDown()) scroll(x, y); else { IJ.setInputEvent(e); Roi roi = imp.getRoi(); if (roi != null) roi.handleMouseDrag(x, y, flags); } }
public void fitToWindow() { ImageWindow win = imp.getWindow(); if (win == null) return; Rectangle bounds = win.getBounds(); Insets insets = win.getInsets(); int sliderHeight = (win instanceof StackWindow) ? 20 : 0; double xmag = (double) (bounds.width - 10) / srcRect.width; double ymag = (double) (bounds.height - (10 + insets.top + sliderHeight)) / srcRect.height; setMagnification(Math.min(xmag, ymag)); int width = (int) (imageWidth * magnification); int height = (int) (imageHeight * magnification); if (width == dstWidth && height == dstHeight) return; srcRect = new Rectangle(0, 0, imageWidth, imageHeight); setDrawingSize(width, height); getParent().doLayout(); }
protected void scroll(int sx, int sy) { int ox = xSrcStart + (int) (sx / magnification); // convert to offscreen coordinates int oy = ySrcStart + (int) (sy / magnification); // IJ.log("scroll: "+ox+" "+oy+" "+xMouseStart+" "+yMouseStart); int newx = xSrcStart + (xMouseStart - ox); int newy = ySrcStart + (yMouseStart - oy); if (newx < 0) newx = 0; if (newy < 0) newy = 0; if ((newx + srcRect.width) > imageWidth) newx = imageWidth - srcRect.width; if ((newy + srcRect.height) > imageHeight) newy = imageHeight - srcRect.height; srcRect.x = newx; srcRect.y = newy; // IJ.log(sx+" "+sy+" "+newx+" "+newy+" "+srcRect); imp.draw(); Thread.yield(); }
/** * Zooms in by making the window bigger. If it can't be made bigger, then make the source * rectangle (srcRect) smaller and center it at (sx,sy). Note that sx and sy are screen * coordinates. */ public void zoomIn(int sx, int sy) { if (magnification >= 32) return; double newMag = getHigherZoomLevel(magnification); int newWidth = (int) (imageWidth * newMag); int newHeight = (int) (imageHeight * newMag); Dimension newSize = canEnlarge(newWidth, newHeight); if (newSize != null) { setDrawingSize(newSize.width, newSize.height); if (newSize.width != newWidth || newSize.height != newHeight) adjustSourceRect(newMag, sx, sy); else setMagnification(newMag); imp.getWindow().pack(); } else adjustSourceRect(newMag, sx, sy); repaint(); if (srcRect.width < imageWidth || srcRect.height < imageHeight) resetMaxBounds(); }
/** Implements the Image/Zoom/View 100% command. */ public void zoom100Percent() { if (magnification == 1.0) return; double imag = imp.getWindow().getInitialMagnification(); if (magnification != imag) unzoom(); if (magnification == 1.0) return; if (magnification < 1.0) { while (magnification < 1.0) zoomIn(imageWidth / 2, imageHeight / 2); } else if (magnification > 1.0) { while (magnification > 1.0) zoomOut(imageWidth / 2, imageHeight / 2); } else return; int x = xMouse, y = yMouse; if (mouseExited) { x = imageWidth / 2; y = imageHeight / 2; } int sx = screenX(x); int sy = screenY(y); adjustSourceRect(1.0, sx, sy); repaint(); }
protected void handlePopupMenu(MouseEvent e) { if (disablePopupMenu) return; if (IJ.debugMode) IJ.log("show popup: " + (e.isPopupTrigger() ? "true" : "false")); int x = e.getX(); int y = e.getY(); Roi roi = imp.getRoi(); if (roi != null && (roi.getType() == Roi.POLYGON || roi.getType() == Roi.POLYLINE || roi.getType() == Roi.ANGLE) && roi.getState() == roi.CONSTRUCTING) { roi.handleMouseUp(x, y); // simulate double-click to finalize roi.handleMouseUp(x, y); // polygon or polyline selection return; } PopupMenu popup = Menus.getPopupMenu(); if (popup != null) { add(popup); if (IJ.isMacOSX()) IJ.wait(10); popup.show(this, x, y); } }
protected Dimension canEnlarge(int newWidth, int newHeight) { // if ((flags&Event.CTRL_MASK)!=0 || IJ.controlKeyDown()) return null; ImageWindow win = imp.getWindow(); if (win == null) return null; Rectangle r1 = win.getBounds(); Insets insets = win.getInsets(); Point loc = getLocation(); if (loc.x > insets.left + 5 || loc.y > insets.top + 5) { r1.width = newWidth + insets.left + insets.right + 10; r1.height = newHeight + insets.top + insets.bottom + 10; if (win instanceof StackWindow) r1.height += 20; } else { r1.width = r1.width - dstWidth + newWidth + 10; r1.height = r1.height - dstHeight + newHeight + 10; } Rectangle max = win.getMaxWindow(r1.x, r1.y); boolean fitsHorizontally = r1.x + r1.width < max.x + max.width; boolean fitsVertically = r1.y + r1.height < max.y + max.height; if (fitsHorizontally && fitsVertically) return new Dimension(newWidth, newHeight); else if (fitsVertically && newHeight < dstWidth) return new Dimension(dstWidth, newHeight); else if (fitsHorizontally && newWidth < dstHeight) return new Dimension(newWidth, dstHeight); else return null; }
void drawOverlay(Graphics g) { if (imp != null && imp.getHideOverlay()) return; Color labelColor = overlay.getLabelColor(); if (labelColor == null) labelColor = Color.white; initGraphics(g, labelColor, Roi.getColor()); int n = overlay.size(); if (IJ.debugMode) IJ.log("paint: drawing " + n + " ROI display list"); int currentImage = imp != null ? imp.getCurrentSlice() : -1; if (imp.getStackSize() == 1) currentImage = -1; int channel = 0, slice = 0, frame = 0; boolean hyperstack = imp.isHyperStack(); if (hyperstack) { channel = imp.getChannel(); slice = imp.getSlice(); frame = imp.getFrame(); } drawNames = overlay.getDrawNames(); boolean drawLabels = drawNames || overlay.getDrawLabels(); for (int i = 0; i < n; i++) { if (overlay == null) break; Roi roi = overlay.get(i); if (hyperstack && roi.getPosition() == 0) { int c = roi.getCPosition(); int z = roi.getZPosition(); int t = roi.getTPosition(); if ((c == 0 || c == channel) && (z == 0 || z == slice) && (t == 0 || t == frame)) drawRoi(g, roi, drawLabels ? i + LIST_OFFSET : -1); } else { int position = roi.getPosition(); if (position == 0 || position == currentImage) drawRoi(g, roi, drawLabels ? i + LIST_OFFSET : -1); } } ((Graphics2D) g).setStroke(Roi.onePixelWide); drawNames = false; }
public void keyPressed(KeyEvent e) { // if (e.isConsumed()) return; int keyCode = e.getKeyCode(); IJ.setKeyDown(keyCode); hotkey = false; if (keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_SHIFT) return; char keyChar = e.getKeyChar(); int flags = e.getModifiers(); if (IJ.debugMode) IJ.log( "keyPressed: code=" + keyCode + " (" + KeyEvent.getKeyText(keyCode) + "), char=\"" + keyChar + "\" (" + (int) keyChar + "), flags=" + KeyEvent.getKeyModifiersText(flags)); boolean shift = (flags & KeyEvent.SHIFT_MASK) != 0; boolean control = (flags & KeyEvent.CTRL_MASK) != 0; boolean alt = (flags & KeyEvent.ALT_MASK) != 0; boolean meta = (flags & KeyEvent.META_MASK) != 0; String cmd = null; ImagePlus imp = WindowManager.getCurrentImage(); boolean isStack = (imp != null) && (imp.getStackSize() > 1); if (imp != null && !control && ((keyChar >= 32 && keyChar <= 255) || keyChar == '\b' || keyChar == '\n')) { Roi roi = imp.getRoi(); if (roi instanceof TextRoi) { if ((flags & KeyEvent.META_MASK) != 0 && IJ.isMacOSX()) return; if (alt) { switch (keyChar) { case 'u': case 'm': keyChar = IJ.micronSymbol; break; case 'A': keyChar = IJ.angstromSymbol; break; default: } } ((TextRoi) roi).addChar(keyChar); return; } } // Handle one character macro shortcuts if (!control && !meta) { Hashtable macroShortcuts = Menus.getMacroShortcuts(); if (macroShortcuts.size() > 0) { if (shift) cmd = (String) macroShortcuts.get(new Integer(keyCode + 200)); else cmd = (String) macroShortcuts.get(new Integer(keyCode)); if (cmd != null) { // MacroInstaller.runMacroCommand(cmd); commandName = cmd; MacroInstaller.runMacroShortcut(cmd); return; } } } if ((!Prefs.requireControlKey || control || meta) && keyChar != '+') { Hashtable shortcuts = Menus.getShortcuts(); if (shift) cmd = (String) shortcuts.get(new Integer(keyCode + 200)); else cmd = (String) shortcuts.get(new Integer(keyCode)); } if (cmd == null) { switch (keyChar) { case '<': case ',': if (isStack) cmd = "Previous Slice [<]"; break; case '>': case '.': case ';': if (isStack) cmd = "Next Slice [>]"; break; case '+': case '=': cmd = "In [+]"; break; case '-': cmd = "Out [-]"; break; case '/': cmd = "Reslice [/]..."; break; default: } } if (cmd == null) { switch (keyCode) { case KeyEvent.VK_TAB: WindowManager.putBehind(); return; case KeyEvent.VK_BACK_SPACE: // delete if (deleteOverlayRoi(imp)) return; cmd = "Clear"; hotkey = true; break; // case KeyEvent.VK_BACK_SLASH: cmd=IJ.altKeyDown()?"Animation Options...":"Start // Animation"; break; case KeyEvent.VK_EQUALS: cmd = "In [+]"; break; case KeyEvent.VK_MINUS: cmd = "Out [-]"; break; case KeyEvent.VK_SLASH: case 0xbf: cmd = "Reslice [/]..."; break; case KeyEvent.VK_COMMA: case 0xbc: if (isStack) cmd = "Previous Slice [<]"; break; case KeyEvent.VK_PERIOD: case 0xbe: if (isStack) cmd = "Next Slice [>]"; break; case KeyEvent.VK_LEFT: case KeyEvent.VK_RIGHT: case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: // arrow keys if (imp == null) return; Roi roi = imp.getRoi(); if (IJ.shiftKeyDown() && imp == Orthogonal_Views.getImage()) return; boolean stackKey = imp.getStackSize() > 1 && (roi == null || IJ.shiftKeyDown()); boolean zoomKey = roi == null || IJ.shiftKeyDown() || IJ.controlKeyDown(); if (stackKey && keyCode == KeyEvent.VK_RIGHT) cmd = "Next Slice [>]"; else if (stackKey && keyCode == KeyEvent.VK_LEFT) cmd = "Previous Slice [<]"; else if (zoomKey && keyCode == KeyEvent.VK_DOWN && !ignoreArrowKeys(imp) && Toolbar.getToolId() < Toolbar.SPARE6) cmd = "Out [-]"; else if (zoomKey && keyCode == KeyEvent.VK_UP && !ignoreArrowKeys(imp) && Toolbar.getToolId() < Toolbar.SPARE6) cmd = "In [+]"; else if (roi != null) { if ((flags & KeyEvent.ALT_MASK) != 0) roi.nudgeCorner(keyCode); else roi.nudge(keyCode); return; } break; case KeyEvent.VK_ESCAPE: abortPluginOrMacro(imp); return; case KeyEvent.VK_ENTER: WindowManager.toFront(this); return; default: break; } } if (cmd != null && !cmd.equals("")) { commandName = cmd; if (cmd.equals("Fill") || cmd.equals("Draw")) hotkey = true; if (cmd.charAt(0) == MacroInstaller.commandPrefix) MacroInstaller.runMacroShortcut(cmd); else { doCommand(cmd); keyPressedTime = System.currentTimeMillis(); lastKeyCommand = cmd; } } }